-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcoapShellyDevice.js
375 lines (356 loc) · 13.8 KB
/
coapShellyDevice.js
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
const _ = require('lodash');
const assert = require('assert');
const network = require('net');
const Pollinator = require('pollinator');
const got = require("got");
const makeDeviceKey = (type, id) => `${type}-${id}`;
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
if (_.isEmpty(object)) return base;
if (_.isEmpty(base)) return object;
return _.transform(object, (result, value, key) => {
if (!_.isEqual(value, base[key])) {
result[key] = _.isObject(value) && _.isObject(base[key]) ? difference(value, base[key]) : value;
}
});
}
const TimeAgo = require('javascript-time-ago');
const { isObject } = require('lodash');
TimeAgo.addDefaultLocale(require('javascript-time-ago/locale/en'));
const timeAgo = new TimeAgo('en-GB')
class Shelly {
#coapshelly = {};
_coapsettings = {};
_coapstatus = {};
#sse
#lastSentSSE = {};
#lastSSEEventType = '';
#deviceAPIqueuePromise;
#deviceAPIqueue = null;
#storage
#storageQueuePromise
#storageQueue = null;
#connectPoll
#statusPoll
#settingsPoll
constructor(type, id, host, sse, storage) {
this.deviceKey = makeDeviceKey(type, id);
this.type = type;
this.id = id;
this.ip = host;
this.firmware = { curlong: '', curshort: '', status: 'idle', hasupgrade: false, newlong: '', newshort: '' };
this.online = false;
this.connected = false;
this.locked = true;
this.#sse = sse;
this.#deviceAPIqueuePromise = (async () => { return import('p-queue'); })();
this.#storage = storage;
this.#storageQueuePromise = (async () => { return import('p-queue'); })();
this.#connectPoll = new Pollinator(this.connectPollfn.bind(this), { delay: 10208 + Math.round(Math.random() * 5079) });
this.#statusPoll = new Pollinator(this.statusPollfn.bind(this), { delay: 14200 + Math.round(Math.random() * 8479) });
this.#settingsPoll = new Pollinator(this.settingsPollfn.bind(this), { delay: 39600 + Math.round(Math.random() * 14822) });
}
get initialized() {
return Promise.all([
this.#deviceAPIqueuePromise.then((deviceAPIqueue) => {
this.#deviceAPIqueue = new deviceAPIqueue.default({ concurrency: 1 });
return this;
}),
this.#storageQueuePromise.then((storageQueue) => {
this.#storageQueue = new storageQueue.default({ concurrency: 1 });
return this;
})
]).then(result => {
return result[0]; // Both promises return same value so take first.
})
}
async connectPollfn() {
if ((this.deviceKey === 'SHSW-PM-A4CF12F3F2D3') || (this.deviceKey === 'SHSW-PM-F30AA2') || (this.deviceKey === 'SHSW-1-E098068D0745'))
console.log(`connectPoll ${this.deviceKey}`);
try {
const newShellyRec = await this.callShelly('shelly');
const differences = difference(this.coapshelly, newShellyRec);
if (!_.isEmpty(differences)) {
this.coapshelly = newShellyRec;
//} else {
// console.info(`No point to update coapshelly`);
}
} catch (err) {
if (err.message === 'Aborted') {
console.warn(`connectPollfn error: Timeout trying to connect to device ${this.deviceKey}`);
return;
}
console.error(`*********ERROR*********: ${err.message} failed to retreive coapshelly REST call for ${this.id}`);
}
}
async statusPollfn() {
if (this.deviceKey === 'SHSW-PM-A4CF12F3F2D3')
console.log(`statusPoll ${this.deviceKey}`);
try {
const newStatus = await this.callShelly('status');
// We are not interested in the time data in status so delete so we do not see as a difference
delete newStatus.time;
delete newStatus.unixtime;
this.connected = true;
const differences = difference(this.coapstatus, newStatus);
if (!_.isEmpty(differences)) {
this.coapstatus = newStatus;
} else {
console.info(`Got coapstatus for ${this.deviceKey} but ignored as they are the same`);
}
} catch (err) {
if (err.message === 'Unauthorized') {
console.warn(`statusPollfn error: Got Unauthorized trying to connect to ${this.id} so stopping further Status requests`);
this.#statusPoll.stop();
this.connected = false;
this.locked = true;
this.auth = true;
this.persist();
this.ssesend('shellyUpdate');
return;
}
if (err.message === 'Aborted') {
console.warn(`statusPollfn error: Timeout trying to connect to device ${this.deviceKey}`);
return;
}
console.error(`*********ERROR*********: ${err.message} failed to retreive coapstatus for ${this.id}`);
}
return this.coapstatus;
}
async settingsPollfn() {
if (this.deviceKey === 'SHSW-PM-A4CF12F3F2D3')
console.log(`settingsPoll ${this.deviceKey}`);
try {
const newSettings = await this.callShelly('settings');
this.connected = true;
// We are not interested in the time data in settings so delete so we do not see as a difference
delete newSettings.time;
delete newSettings.unixtime;
const differences = difference(this.coapsettings, newSettings);
if (!_.isEmpty(differences)) {
this.coapsettings = newSettings;
}
} catch (err) {
if (err.message === 'Unauthorized') {
console.error(`settingsPollfn error: Got Unauthorized trying to connect to ${this.id} so stopping further Status requests`);
this.#settingsPoll.stop();
this.connected = false;
this.locked = true;
this.auth = true;
this.persist();
this.ssesend('shellyUpdate');
return;
}
if (err.message === 'Aborted') {
console.warn(`settingsPollfn error: Timeout trying to connect to device ${this.deviceKey}`);
return;
}
console.error(`*********ERROR*********: ${err.message} failed to retreive coapsettings for ${this.id}`);
}
}
async upgradePollfn() {
try {
const newOTA = await this.callShelly('ota');
console.info(`upgradePollfn: OTA status of device ${this.deviceKey} is ${newOTA.status}`)
if (newOTA.status != this.firmware.status) {
this._coapstatus.update = newOTA;
this.firmware.status = newOTA.status;
this.ssesend('shellyUpdate')
}
return newOTA;
} catch (err) {
if (err.message === 'Aborted') {
console.warn(`upgradePollfn error: Timeout trying to gety OTA status from device ${this.deviceKey}`);
return;
}
console.error(`*********ERROR*********: ${err.message} failed to retreive OTA details for ${this.id}`);
}
}
get coapshelly() {
return this.#coapshelly;
}
set coapshelly(coapshelly) {
assert(_.isObject(coapshelly));
if (this.#coapshelly === coapshelly) {
console.error(`Tried to assign coapshelly for ${this.deviceKey} that already exists`);
return;
}
this.#coapshelly = coapshelly;
if (!_.isEmpty(coapshelly.type)) this.modelName = coapshelly.type;
this.firmware.curlong = coapshelly.fw;
this.firmware.curshort = (/([^/]*\/)([^-]*)(-.*)/g.exec(coapshelly.fw + "/-"))[2];
this.online = coapshelly.online;
this.auth = coapshelly.auth;
if (this.connected === false) {
if (this.auth === true) {
if (_.isEmpty(this.shellyuser)) {
console.warn(`set coapshelly: There is no device (${this.deviceKey}) specific username found so using global user '${process.env.SHELLYUSER}/${process.env.SHELLYPW.slice(0, 2)}***'`);
this.shellyuser = process.env.SHELLYUSER;
this.shellypassword = process.env.SHELLYPW;
} else {
console.warn(`set coapshelly: Found device specific username '${this.shellyuser}/${this.shellypassword.slice(0, 2)}***' so using that for device '${this.deviceKey}'`);
}
}
this.callShelly('settings/login')
.then(function () {
this.connected = true;
this.locked = false;
this.#connectPoll._config.delay = 120000;
this.#statusPoll.start();
this.#settingsPoll.start();
this.persist();
this.ssesend('shellyUpdate');
}.bind(this))
.catch(function () {
this.connected = false;
this.#connectPoll._config.delay = 30000;
console.warn(`set coapshelly: Auth error while trying to connect to ${this.deviceKey}`);
}.bind(this))
}
this.persist();
this.ssesend('shellyUpdate');
}
get coapstatus() {
return this._coapstatus;
}
set coapstatus(coapstatus) {
//console.info(`Setting coapstatus for ${this.deviceKey}`);
this._coapstatus = coapstatus;
if (_.isNumber(coapstatus?.wifi_sta?.rssi))
this.rssi = coapstatus?.wifi_sta.rssi;
if (_.isBoolean(coapstatus?.mqtt?.connected))
this.mqtt_connected = coapstatus.mqtt.connected;
if (_.isBoolean(coapstatus?.update?.has_update))
this.firmware.hasupgrade = coapstatus?.update.has_update;
if (!_.isEmpty(coapstatus.update?.new_version)) {
this.firmware.newlong = coapstatus.update.new_version;
this.firmware.newshort = (/([^/]*\/)([^-]*)(-.*)/g.exec(this.firmware.newlong + "/-"))[2];
}
if (!_.isEmpty(coapstatus.update?.status))
this.firmware.status = coapstatus.update.status;
this.persist();
this.ssesend('shellyUpdate');
}
get coapsettings() {
return this._coapsettings;
}
set coapsettings(coapsettings) {
//console.info(`Setting coapsettings for ${this.deviceKey}`);
this._coapsettings = coapsettings;
if (!_.isEmpty(coapsettings.name))
this.givenname = coapsettings.name;
if (!_.isEmpty(coapsettings?.wifi_sta?.ssid))
this.ssid = coapsettings.wifi_sta.ssid;
if (_.isBoolean(coapsettings?.mqtt?.enable))
this.mqtt_enable = coapsettings.mqtt.enable;
if (!_.isEmpty(coapsettings?.fw)) {
this.firmware.curlong = coapsettings.fw;
this.firmware.curshort = (/([^/]*\/)([^-]*)(-.*)/g.exec(this.firmware.curlong + "/-"))[2];
}
if (!_.isEmpty(coapsettings.device?.type)) this.modelName = coapsettings.device.type;
this.persist();
this.ssesend('shellyUpdate');
}
start() {
if (!network.isIP(this.ip)) debugger;
this.online = false;
this.auth = false;
this.connected = false;
this.#connectPoll.start();
this.persist();
this.ssesend('shellyUpdate');
return this;
}
stop() {
this.online = false;
this.#connectPoll.stop();
this.#statusPoll.stop();
this.#settingsPoll.stop();
return this;
}
async callShelly(urlPath) {
let requestOptions = { responseType: 'json', options: { responseType: 'json' } };
if (this.auth) {
requestOptions.options.username = this.shellyuser;
requestOptions.options.password = this.shellypassword;
requestOptions.options.auth = this.shellyuser + ':' + this.shellypassword;
requestOptions.username = this.shellyuser;
requestOptions.password = this.shellypassword;
requestOptions.auth = this.shellyuser + ':' + this.shellypassword;
}
const { body } = await this.#deviceAPIqueue.add(() => got(`http://${this.ip}/${urlPath}`, requestOptions));
const result = JSON.parse(body);
if (this.auth === true)
console.info(`Got result of auth device ${this.deviceKey} to path '${urlPath}'`)
return result;
}
setAuthCredentials(user, password) {
console.info(`Set the credentials of device ${this.deviceKey} to ${user}/${password}`);
if ((this.shellyuser !== user) || (this.shellypassword !== password)) {
this.shellyuser = user;
this.shellypassword = password;
this.auth = !_.isEmpty(password);
this.#connectPoll.stop().start();
}
}
getSSEobj() {
let result = _.omitBy(this, (value, key) => {
if (_.startsWith(key, '#')) return true;
if (_.startsWith(key, '_coap')) return true;
if (_.startsWith(key, 'coap')) return true;
return false;
});
if (result.auth && result.locked) {
result.givenname = 'Unavailable (locked)';
result.ssid = 'n/a';
}
return Object(result);
}
ssesend(eventName) {
//await sseQueue.add(() => sseSender.send(eventName, this.getSSEobj()));
const shellySSE = this.getSSEobj();
const differences = difference(shellySSE, this.#lastSentSSE);
if ((!_.isEmpty(differences)) || (eventName != this.#lastSSEEventType)) {
//console.info(`ssesend: sending ${eventName} for device ${this.deviceKey} ${this.#lastSSEEventType}:${JSON.stringify(differences)}`)
this.#sse.send(shellySSE, eventName);
this.#lastSSEEventType = eventName;
this.#lastSentSSE = shellySSE;
//} else {
// console.info(`ssesend: ignored ${eventName} for device ${this.deviceKey}`)
}
}
async persist() {
await this.#storageQueue.add(() => this.#storage.setItem(this.deviceKey, this));
}
revive(srcShelly) {
for (const [key, value] of Object.entries(srcShelly)) {
try {
if (!_.isUndefined(value)) this[key] = value;
} catch (err) { console.error(`Object revival error: ${err.message} in for key ${key} and value ${value}`); }
}
}
async upgrade() {
this.firmware.status = 'requesting';
this.ssesend('shellyUpdate');
await this.callShelly('ota?update=true');
let upgradePoll = new Pollinator(this.upgradePollfn.bind(this),
{
delay: 2000, conditionFn: function (currentResponse, previousResponse) {
const result = ((currentResponse.status == 'idle') || (currentResponse.status == 'pending'));
if (result) {
this.firmware.status = 'finished';
this.ssesend('shellyUpdate');
}
return result;
}.bind(this)
});
upgradePoll.start();
}
}
['coapshelly', 'coapstatus', 'coapsettings'].forEach(prop => Object.defineProperty(Shelly.prototype, prop, { enumerable: true }));
module.exports = Shelly;