-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.nut
377 lines (340 loc) · 13.6 KB
/
device.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* LIBRARY CODE
*
*/
// Code version for Squinter
#version "2.1.1"
/**
* Disconection/reconnection Mananger
*
* Provides disconnectionManager, a global object which operates as a handler for imp connection states.
* It monitors connection state and will automatically attempt to reconnect when the imp disconnects unexpectedly
*
* @author Tony Smith (@smittytone)
* @copyright Tony Smith, 2018-19
* @licence MIT
* @version 2.1.1
*
* @table
*
*/
disconnectionManager <- {
// ********** Public Properties **********
"reconnectTimeout" : 30,
"reconnectDelay" : 60,
"monitoring" : false,
"isConnected" : true,
"message" : "",
"reason" : SERVER_CONNECTED,
"retries" : 0,
"offtime" : null,
"eventCallback" : null,
/**
* Begin monitoring device connection state
*
* @param {integer/float} [timeout] - The max. time (in seconds) allowed for the server to acknowledge receipt of data. Default: 10s
* @param {integer} [sendPolicy] - The send policy: either WAIT_TIL_SENT or WAIT_FOR_ACK. Default: WAIT_TIL_SENT
*
*/
"start" : function(timeout = 10, sendPolicy = WAIT_TIL_SENT) {
// Check parameter type, and fix if it's wrong
if (typeof timeout != "integer" && typeof timeout != "float") timeout = 10;
if (sendPolicy != WAIT_TIL_SENT && sendPolicy != WAIT_FOR_ACK) sendPolicy = WAIT_TIL_SENT;
// Register handlers etc.
// NOTE We assume use of RETURN_ON_ERROR as DisconnectionManager is
// largely redundant with the SUSPEND_ON_ERROR policy
server.setsendtimeoutpolicy(RETURN_ON_ERROR, sendPolicy, timeout);
server.onunexpecteddisconnect(disconnectionManager._hasDisconnected.bindenv(this));
disconnectionManager.monitoring = true;
disconnectionManager._wakeup({"message": "Enabling disconnection monitoring"});
// Check for initial connection (give it time to connect)
disconnectionManager.connect();
},
/**
* Stop monitoring connection state
*
*/
"stop" : function() {
// De-Register handlers etc.
disconnectionManager.monitoring = false;
disconnectionManager._wakeup({"message": "Disabling disconnection monitoring"});
},
/**
* Attempt to connect to the server. No effect if the imp is already connected
*
*/
"connect" : function() {
// Attempt to connect to the server if we're not connected already
// We do this to set our initial state
disconnectionManager.isConnected = server.isconnected();
if (!disconnectionManager.isConnected) {
disconnectionManager._wakeup({"message": "Manually connecting to server", "type": "connecting"});
server.connect(disconnectionManager._eventHandler.bindenv(this), disconnectionManager.reconnectTimeout);
} else {
disconnectionManager._wakeup({"type": "connected"});
}
},
/**
* Manually disconnect from the server
*
*/
"disconnect" : function() {
// Disconnect from the server if we're not disconnected already
disconnectionManager.isConnected = false;
if (server.isconnected()) {
imp.onidle(function() {
server.flush(10);
server.disconnect();
disconnectionManager._wakeup({"message": "Manually disconnected from server", "type": "disconnected"});
}.bindenv(this));
} else {
disconnectionManager._wakeup({"type": "disconnected"});
}
},
/**
* Connection/disconnection event descriptor
*
* @typedef {table} eventDesc
*
* @property {string} type - Human-readable event type: "connected", "connecting", "disconnected"
* @property {string} message - Human-readable notification message
* @property {integer} ts - The timestamp when the message was queued. Added automatically
*
*/
/**
* Connection state change notification callback function
*
* @callback eventcallback
*
* @param {eventDesc} event - An event descriptior
*
*/
/**
* Set the manager's network event callback
*
* @param {eventcallback} cb - A function to which connection state change notifications are sent
*
*/
"setCallback" : function(cb = null) {
// Convenience function for setting the framework's event report callback
if (cb != null && typeof cb == "function") disconnectionManager.eventCallback = cb;
},
// ********** Private Properties DO NOT ACCESS DIRECTLY **********
"_noIP" : false,
"_codes" : ["No WiFi connection", "No LAN connection", "No IP address (DHCP error)", "impCloud IP not resolved (DNS error)",
"impCloud unreachable", "Connected to impCloud", "No proxy server", "Proxy credentials rejected"],
// ********** Private Methods DO NOT CALL DIRECTLY **********
/**
* Function called whenever the server connection is broken or re-established, initially by impOS' unexpected disconnect
* code and then repeatedly by server.connect(), below, as it periodically attempts to reconnect
*
* @private
*
* @param {integer} reason - The imp API (see server.connect()) connection/disconnection event code
*
*/
"_eventHandler" : function(reason) {
// If we are not checking for unexpected disconnections, bail
if (!disconnectionManager.monitoring) return;
if (reason != SERVER_CONNECTED) {
// The device wasn't previously disconnected, so set the state to 'disconnected', ie. 'isConnected' is true
if (disconnectionManager.isConnected) {
// Set the connection state data and disconnection info data
// NOTE connection fails 60s before 'eventHandler' is called
disconnectionManager.isConnected = false;
disconnectionManager.retries = 0;
disconnectionManager.reason = reason;
disconnectionManager.offtime = date();
// Send a 'disconnected' event to the host app
disconnectionManager._wakeup({"message": "Device unexpectedly disconnected", "type" : "disconnected"});
} else {
// Send a 'still disconnected' event to the host app
local m = disconnectionManager._formatTimeString();
disconnectionManager._wakeup({"message": "Device still disconnected at " + m,
"type" : "disconnected"});
}
// Schedule an attempt to re-connect in 'reconnectDelay' seconds
imp.wakeup(disconnectionManager.reconnectDelay, function() {
if (!server.isconnected()) {
// If we're not connected, send a 'connecting' event to the host app and try to connect
disconnectionManager.retries += 1;
disconnectionManager._wakeup({"message": "Device connecting", "type" : "connecting"});
server.connect(disconnectionManager._eventHandler.bindenv(this), disconnectionManager.reconnectTimeout);
} else {
// If we are connected, re-call 'eventHandler()' to make sure the 'connnected' flow is executed
// disconnectionManager._wakeup({"message": "Wakeup code called, but device already connected"});
disconnectionManager._eventHandler(SERVER_CONNECTED);
}
}.bindenv(this));
} else {
// The imp is back online
if (!disconnectionManager.isConnected) {
// Send a 'connected' event to the host app
// Report the time that the device went offline
local m = disconnectionManager._formatTimeString(disconnectionManager.offtime);
m = format("Went offline at %s. Reason: %s (%i)", m, disconnectionManager._getReason(disconnectionManager.reason), disconnectionManager.reason);
disconnectionManager._wakeup({"message": m});
// Report the time that the device is back online
m = disconnectionManager._formatTimeString();
m = format("Back online at %s. Connection attempts: %i", m, disconnectionManager.retries);
disconnectionManager._wakeup({"message": m, "type" : "connected"});
}
// Re-set state data
disconnectionManager.isConnected = true;
disconnectionManager._noIP = false;
disconnectionManager.offtime = null;
}
},
/**
* This is an intercept function for 'server.onunexpecteddisconnect()' to handle the double-calling of this method's registered handler
* when the imp loses its link to DHCP but still has WiFi
*
* @private
*
* @param {integer} reason - The imp API (see server.connect()) connection/disconnection event code
*
*/
"_hasDisconnected" : function(reason) {
if (!disconnectionManager._noIP) {
disconnectionManager._noIP = true;
disconnectionManager._eventHandler(reason);
}
},
/**
* Return the connection error/disconnection reason as a human-readable string
*
* @private
*
* @param {integer} code - The imp API (see server.connect()) connection/disconnection event code
*
* @returns {string} The human-readable string
*
*/
"_getReason" : function(code) {
return _codes[code];
},
/**
* Format a timestamp string, either the current time (default; pass null as the argument),
* or a specific time (pass a timestamp as the argument). Includes the timezone
* NOTE It is able to make use of the 'utilities' BST checker, if also included in your application
*
* @private
*
* @param {table} [n] - A Squirrel date/time description table (see date()). Default: current date
*
* @returns {string} The timestamp string, eg. "12:45:0 +1:00"
*
*/
"_formatTimeString" : function(time = null) {
local bst = false;
if ("utilities" in getroottable()) bst = utilities.isBST();
if (time == null) time = date();
time.hour += (bst ? 1 : 0);
if (time.hour > 23) time.hour -= 24;
local z = bst ? "+01:00" : "UTC";
return format("%02i:%02i:%02i %s", time.hour, time.min, time.sec, z);
},
//
/**
* Queue up a message post with the supplied data on an immediate timer
*
* @private
*
* @param {eventDesc} evd - An event descriptor
*
*/
"_wakeup": function(evd) {
// Add a message timestamp
evd.ts <- time();
if (disconnectionManager.eventCallback != null) {
imp.wakeup(0, function() {
disconnectionManager.eventCallback(evd);
});
}
}
}
/*
* GLOBALS
*
*/
local green = hardware.pinK;
local red = hardware.pinN;
local yellow = hardware.pinB;
local networks = null;
local isScanning = false;
/*
* RUNTIME START
*
*/
/*
* Configure the status LEDs:
* GREEN - device is connected
* YELLOW - device is attempting to connect
* RED - device is disconnected
*/
local isConnected = server.isconnected();
green.configure(DIGITAL_OUT, (isConnected ? 1 : 0));
red.configure(DIGITAL_OUT, (isConnected ? 0 : 1));
yellow.configure(DIGITAL_OUT, 0);
// Register the connection state reporting callback
disconnectionManager.eventCallback = function(event) {
if ("message" in event) server.log(event.message + " (Timestamp: " + event.ts + ")");
if ("type" in event) {
if (event.type == "connected") {
// Set the LEDs to green on, yellow off, red off
green.write(1);
yellow.write(0);
red.write(0);
// Relay connection information
local i = imp.net.info();
agent.send("send.net.status", i.ipv4);
i = "active" in i ? i.interface[i.active] : i.interface[0];
server.log("Current RSSI " + ("rssi" in i ? i.rssi : "unknown"));
} else if (event.type == "disconnected") {
// Set the LEDs to green off, yellow off, red on
green.write(0);
red.write(1);
yellow.write(0);
} else if (event.type == "connecting") {
// Set the yellow LED on
yellow.write(1);
} else {
// Just in case, turn all LEDs off
green.write(0);
red.write(0);
yellow.write(0);
}
}
};
// Set up the connection handler
disconnectionManager.reconnectDelay = 61;
disconnectionManager.start();
/*
* Register handlers for messages sent to the device by its agent
*/
agent.on("get.wifi.data", function(dummy) {
// The agent has requested WLAN status information which the web UI will display
local i = imp.net.info();
if ("active" in i) {
// Get the active network interface and make sure it's WiFi
local item = i.interface[i.active];
if (item.type == "wifi") {
// Send the network data
agent.send("send.net.status", i.ipv4);
}
}
});
agent.on("get.wlan.list", function(dummy) {
// The agent has requested a list of nearby WiFi networks, so begin
// a new scan if one is not already in progress
if (!isScanning) {
isScanning = true;
imp.scanwifinetworks(function(wlans) {
// This callback is triggered when the list has been retrieved
isScanning = false;
networks = wlans;
// Send the retrieved WLAN list to the agent
agent.send("set.wlan.list", networks);
}.bindenv(this));
}
}.bindenv(this));