-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomeLEDs_device.nut
167 lines (139 loc) · 5.67 KB
/
homeLEDs_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
// -----------------------------------------------------------------------------
// Connection manager
//
// This connection manager is designed to make the use of the RETURN_ON_ERROR timeout
// policy simple. All the developer has to do is create an instance of the connection
// manager class and the timeout policy will change but the device will otherwise
// behave the same - it will continuously try to restore a connection to the Wifi.
// Then all your network requests, like server.log(), will fail without freezing the CPU.
//
// This class can be extended, if required, to do a few more things:
// - Back off the reconnection attempts to conserve batteries.
// - Put the device to deep sleep while offline and reattempt to connect on wakeup.
//
class Connection {
static CONNECTION_TIMEOUT = 30;
static CHECK_TIMEOUT = 5;
static MAX_LOGS = 100;
connected = null;
connecting = false;
stayconnected = true;
reason = null;
callbacks = null;
blinkup_timer = null;
logs = null;
// .........................................................................
constructor(_do_connect = true) {
callbacks = {};
logs = [];
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, CONNECTION_TIMEOUT);
connected = server.isconnected();
imp.wakeup(CHECK_TIMEOUT, _check.bindenv(this));
if (_do_connect && !connected) imp.wakeup(0, connect.bindenv(this));
else if (connected) imp.wakeup(0, _reconnect.bindenv(this));
}
// .........................................................................
function _check() {
imp.wakeup(CHECK_TIMEOUT, _check.bindenv(this));
if (!server.isconnected() && !connecting && stayconnected) {
// We aren't connected or connecting, so we should try
_disconnected(NOT_CONNECTED, true);
}
}
// .........................................................................
function _disconnected(_reason, _do_reconnect = false) {
local fireevent = connected;
connected = false;
connecting = false;
reason = _reason;
if (fireevent && "disconnected" in callbacks) callbacks.disconnected();
if (_do_reconnect) connect();
}
// .........................................................................
function _reconnect(_state = null) {
if (_state == SERVER_CONNECTED || _state == null) {
connected = true;
connecting = false;
// Dump the logs
while (logs.len() > 0) {
local logo = logs[0];
logs.remove(0);
local d = date(logo.ts);
local msg = format("%04d-%02d-%02d %02d:%02d:%02d UTC %s", d.year, d.month+1, d.day, d.hour, d.min, d.sec, logo.msg);
if (logo.err) server.error(msg);
else server.log(msg);
}
if ("connected" in callbacks) callbacks.connected(SERVER_CONNECTED);
} else {
connected = false;
connecting = false;
connect();
}
}
// .........................................................................
function connect(withblinkup = true) {
stayconnected = true;
if (!connected && !connecting) {
server.connect(_reconnect.bindenv(this), CONNECTION_TIMEOUT);
connecting = true;
}
if (withblinkup) {
// Enable BlinkUp for 60 seconds
imp.enableblinkup(true);
if (blinkup_timer) imp.cancelwakeup(blinkup_timer);
blinkup_timer = imp.wakeup(60, function() {
blinkup_timer = null;
imp.enableblinkup(false);
}.bindenv(this))
}
}
// .........................................................................
function disconnect() {
stayconnected = false;
server.disconnect();
_disconnected(NOT_CONNECTED, false);
}
// .........................................................................
function isconnected() {
return connected == true;
}
// .........................................................................
function ondisconnect(_disconnected = null) {
if (_disconnected == null) delete callbacks["disconnected"];
else callbacks["disconnected"] <- _disconnected;
}
// .........................................................................
function onconnect(_connected = null) {
if (_connected == null) delete callbacks["connected"];
else callbacks["connected"] <- _connected;
}
// .........................................................................
function log(msg, err=false) {
if (server.isconnected()) server.log(msg);
else logs.push({msg=msg, err=err, ts=time()})
if (logs.len() > MAX_LOGS) logs.remove(0);
}
// .........................................................................
function error(msg) {
log(msg, true);
}
}
// -------------- Connection Manager ---------------
cm <- Connection();
cm.onconnect(function(reason=null) {
cm.log("Connected")
});
cm.ondisconnect(function(reason=null) {
cm.error("Disconnected")
});
// --------------- Configure LEDs ------------------
led <- hardware.pin2
led.configure(PWM_OUT, 1.0 / 400.0, 0.0)
state <- 0; //sets up state variable
// ----------------- Run Time ----------------------
function setState(state) {
server.log("changing led level to " + state);
led.write(state);
}
agent.on("state", setState); //receives new state from agent and sets it
imp.wakeup(1, function() { agent.send("getState", null) }); //get current state from agent