You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+59-60
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
The ConnectionManager class is an Electric Imp device-side library created to simplify connect and disconnect flows.
4
4
5
-
**Note** If you are using ConnectionManager in your code, you should ensure that you *never* call [**server.connect()**](https://developer.electricimp.com/api/server/connect/) or [**server.disconnect()**](https://developer.electricimp.com/api/server/disconnect/). Instead you should only use ConnectionManager’s *connect()* and *disconnect()* methods.
5
+
**Note** If you are using ConnectionManager in your code, you should ensure that you *never* call [**server.connect()**](https://developer.electricimp.com/api/server/connect) or [**server.disconnect()**](https://developer.electricimp.com/api/server/disconnect). Instead you should only use ConnectionManager’s *connect()* and *disconnect()* methods.
6
6
7
7
**To add this library to your project, add**`#require "ConnectionManager.lib.nut:3.0.0"`**to the top of your device code.**
8
8
@@ -14,10 +14,10 @@ ConnectionManager can be instantiated with an optional table of settings that mo
14
14
15
15
| Key | Default | Notes |
16
16
| --- | --- | --- |
17
-
|*startBehavior*|*START_NO_ACTION*| See below |
17
+
|*startBehavior*|*CM_START_NO_ACTION*| See below |
18
18
|*stayConnected*|`false`| When set to `true`, the device will aggressively attempt to reconnect when disconnected |
19
19
|*retryOnTimeout*|`true`| When set to `true`, the device will attempt to connect again if it times out |
20
-
|*blinkupBehavior*|*BLINK_ON_DISCONNECT*| See below |
20
+
|*blinkupBehavior*|*CM_BLINK_ON_DISCONNECT*| See below |
21
21
|*checkTimeout*| 5 | Changes how often the ConnectionManager checks the connection state (online/offline) |
22
22
|*connectTimeout*| 60 | Float. Maximum time (in seconds) allowed for the imp to connect to the server before timing out |
23
23
|*errorPolicy*|*RETURN_ON_ERROR*| The disconnection handling policy: *SUSPEND_ON_ERROR*, *RETURN_ON_ERROR or *RETURN_ON_ERROR_NO_DISCONNECT*|
@@ -29,10 +29,8 @@ ConnectionManager can be instantiated with an optional table of settings that mo
29
29
30
30
// Instantiate ConnectionManager so BlinkUp is always enabled,
31
31
// and we automatically aggressively try to reconnect on disconnect
32
-
cm <- ConnectionManager({
33
-
"blinkupBehavior": CM_BLINK_ALWAYS,
34
-
"stayConnected": true
35
-
});
32
+
cm <- ConnectionManager({ "blinkupBehavior": CM_BLINK_ALWAYS,
33
+
"stayConnected" : true });
36
34
37
35
// Set the recommended buffer size (see note below)
38
36
imp.setsendbuffersize(8096);
@@ -41,6 +39,7 @@ imp.setsendbuffersize(8096);
41
39
**Note** We’ve found setting the buffer size to 8096 to be very helpful in many applications using ConnectionManager, though your application may require a different buffer size.
42
40
43
41
#### Setting: startBehavior ####
42
+
44
43
The *startBehavior* flag modifies what action ConnectionManager takes when initialized.
45
44
-*CM_START_NO_ACTION* will take no action after being initialized. This is the default value.
46
45
-*CM_START_CONNECTED* will try to connect after being initialized.
@@ -59,7 +58,7 @@ The *blinkupBehavior* flag modifies when ConnectionManager enables the BlinkUp
59
58
60
59
#### Setting: ackTimeout ####
61
60
62
-
This value is passed into the imp API method [**server.setsendtimeoutpolicy()**](https://developer.electricimp.com/api/server/setsendtimeoutpolicy/), overriding any value your code may have already set in a separate call to that method (or overridden by a subsequent call your code makes). We recommend that if you make use of ConnectionManager, you ensure that you **never** call [**server.setsendtimeoutpolicy()**](https://developer.electricimp.com/api/server/setsendtimeoutpolicy/) in your application code.
61
+
This value is passed into the imp API method [**server.setsendtimeoutpolicy()**](https://developer.electricimp.com/api/server/setsendtimeoutpolicy), overriding any value your code may have already set in a separate call to that method (or overridden by a subsequent call your code makes). We recommend that if you make use of ConnectionManager, you ensure that you **never** call [**server.setsendtimeoutpolicy()**](https://developer.electricimp.com/api/server/setsendtimeoutpolicy) in your application code.
63
62
64
63
## Class Methods ##
65
64
@@ -78,11 +77,11 @@ This method returns the value of ConnectionManager’s internal connection state
78
77
79
78
```squirrel
80
79
if (!cm.isConnected()) {
81
-
// If we're not connected, gather some data, then connect
82
-
cm.onNextConnect(function() {
83
-
local data = sensor.read();
84
-
agent.send("data", data);
85
-
}).connect();
80
+
// If we're not connected, gather some data, then connect
81
+
cm.onNextConnect(function() {
82
+
local data = sensor.read();
83
+
agent.send("data", data);
84
+
}).connect();
86
85
}
87
86
```
88
87
@@ -94,13 +93,13 @@ The callback method takes a single parameter, *expected*, which is `true` when t
94
93
95
94
```squirrel
96
95
cm.onDisconnect(function(expected) {
97
-
if (expected) {
98
-
// Log a regular message that we disconnected as expected
99
-
cm.log("Expected Disconnect");
100
-
} else {
101
-
// Log an error message that we unexpectedly disconnected
102
-
cm.error("Unexpected Disconnect");
103
-
}
96
+
if (expected) {
97
+
// Log a regular message that we disconnected as expected
98
+
cm.log("Expected Disconnect");
99
+
} else {
100
+
// Log an error message that we unexpectedly disconnected
101
+
cm.error("Unexpected Disconnect");
102
+
}
104
103
});
105
104
```
106
105
@@ -112,8 +111,8 @@ The callback function has no parameters.
112
111
113
112
```squirrel
114
113
cm.onConnect(function() {
115
-
// Send a message to the agent indicating that we're online
116
-
agent.send("online", true);
114
+
// Send a message to the agent indicating that we're online
115
+
agent.send("online", true);
117
116
});
118
117
```
119
118
@@ -125,8 +124,8 @@ The callback function has no parameters.
125
124
126
125
```squirrel
127
126
cm.onTimeout(function() {
128
-
// Go to sleep for 10 minutes if the device fails to connect
129
-
server.sleepfor(600);
127
+
// Go to sleep for 10 minutes if the device fails to connect
128
+
server.sleepfor(600);
130
129
});
131
130
```
132
131
@@ -138,18 +137,18 @@ The callback function has no parameters.
138
137
139
138
```squirrel
140
139
function poll() {
141
-
// Wake up every 60 seconds and gather data
142
-
imp.wakeup(60, poll);
143
-
144
-
// Read the data, and insert the timestamp into the data table
145
-
// (in this example, we assume sensor.read() returns a table)
146
-
local data = sensor.read();
147
-
data["ts"] <- time();
148
-
149
-
// Send the data the next time we connect
150
-
cm.onNextConnect(function() {
151
-
agent.send("data", data);
152
-
});
140
+
// Wake up every 60 seconds and gather data
141
+
imp.wakeup(60, poll);
142
+
143
+
// Read the data, and insert the timestamp into the data table
144
+
// (in this example, we assume sensor.read() returns a table)
145
+
local data = sensor.read();
146
+
data.ts <- time();
147
+
148
+
// Send the data the next time we connect
149
+
cm.onNextConnect(function() {
150
+
agent.send("data", data);
151
+
});
153
152
}
154
153
```
155
154
@@ -163,25 +162,25 @@ The callback function has no parameters.
163
162
164
163
```squirrel
165
164
function poll() {
166
-
// Wake up every 60 seconds, connect, send data and disconnect
167
-
imp.wakeup(60, poll);
168
-
169
-
cm.connectFor(function() {
170
-
// Read and send the data
171
-
local data = sensor.read();
172
-
data["ts"] <- time();
173
-
agent.send("data", data);
174
-
});
165
+
// Wake up every 60 seconds, connect, send data and disconnect
166
+
imp.wakeup(60, poll);
167
+
168
+
cm.connectFor(function() {
169
+
// Read and send the data
170
+
local data = sensor.read();
171
+
data.ts <- time();
172
+
agent.send("data", data);
173
+
});
175
174
}
176
175
```
177
176
178
177
**Note** The *connectFor()* method is equivalent to:
179
178
180
179
```squirrel
181
180
cm.onNextConnect(function() {
182
-
// Do something
183
-
...
184
-
cm.disconnect();
181
+
// Do something
182
+
...
183
+
cm.disconnect();
185
184
}).connect();
186
185
```
187
186
@@ -203,7 +202,7 @@ If a connect is in process, the disconnect method will return `false` and won’
203
202
204
203
The *force* parameter provides a means to specify whether ConnectionManager should disconnect regardless of the connection status (ie. whether it’s in progress or not). The parameter is optional and is `false` by default.
205
204
206
-
The *flushTimeout* parameter specifies the timeout value used for [**server.flush()**](https://developer.electricimp.com/api/server/flush/) calls. The parameter is
205
+
The *flushTimeout* parameter specifies the timeout value used for [**server.flush()**](https://developer.electricimp.com/api/server/flush) calls. The parameter is
207
206
optional and is equal to *CM_FLUSH_TIMEOUT* (30 seconds) by default.
208
207
209
208
```
@@ -212,25 +211,25 @@ cm.disconnect();
212
211
213
212
### log(*message*) ###
214
213
215
-
This method will execute a [**server.log()**](https://developer.electricimp.com/api/server/log/) command (if connected), or queue the value of *message* to be logged on the next connect. Any object that can be passed to [**server.log()**](https://developer.electricimp.com/api/server/log/) can be passed to *log()*.
214
+
This method will execute a [**server.log()**](https://developer.electricimp.com/api/server/log) command (if connected), or queue the value of *message* to be logged on the next connect. Any object that can be passed to [**server.log()**](https://developer.electricimp.com/api/server/log) can be passed to *log()*.
216
215
217
216
**Note** The ConnectionManager class stores log messages in memory but doesn’t persist log messages across deep sleeps and cold boots.
218
217
219
218
```squirrel
220
219
cm.onDisconnect(function(expected) {
221
-
if (expected) {
222
-
// Log a regular message that we disconnected as expected
223
-
cm.log("Expected Disconnect");
224
-
} else {
225
-
// Log an error message that we unexpectedly disconnected
226
-
cm.error("Unexpected Disconnect");
227
-
}
220
+
if (expected) {
221
+
// Log a regular message that we disconnected as expected
222
+
cm.log("Expected Disconnect");
223
+
} else {
224
+
// Log an error message that we unexpectedly disconnected
225
+
cm.error("Unexpected Disconnect");
226
+
}
228
227
});
229
228
```
230
229
231
230
### error(*message*) ###
232
231
233
-
The *error()* method will execute a [**server.error()**](https://developer.electricimp.com/api/server/error/) command (if connected), or queue the value of *errorMessage* to be logged on the next connect. Any object that can be passed to [**server.error()**](https://developer.electricimp.com/api/server/error/) can be passed to *error()*.
232
+
The *error()* method will execute a [**server.error()**](https://developer.electricimp.com/api/server/error) command (if connected), or queue the value of *errorMessage* to be logged on the next connect. Any object that can be passed to [**server.error()**](https://developer.electricimp.com/api/server/error) can be passed to *error()*.
234
233
235
234
**Note** The ConnectionManager class stores log messages in memory but doesn’t persist log messages across deep sleeps and cold boots.
236
235
@@ -244,8 +243,8 @@ Alternatively, you can create an `.imptest-builder` file with *CM_TEST_SSID* and
0 commit comments