This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disconnect.nut
277 lines (251 loc) · 10.7 KB
/
disconnect.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
// 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);
});
}
}
}