forked from maxlyth/shelly-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoapShellyList.js
145 lines (134 loc) · 5.17 KB
/
coapShellyList.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
const _ = require('lodash');
const { CoIoTServer, } = require('coiot-coap');
const Shelly = require('./coapShellyDevice.js');
const NodePersist = require('node-persist');
const findShellyPropIndex = function (target, prop) {
if (Reflect.getPrototypeOf(target).hasOwnProperty(prop)) return -2;
if (!isNaN(prop)) {
return Number(prop);
} else if (_.isString(prop)) {
return target.findIndex(shelly => shelly.deviceKey === prop);
}
return -1;
};
const ShellyListProxyHandler = {
get: function (target, prop) {
let foundIndex = findShellyPropIndex(target, prop);
if (foundIndex >= 0) return target[foundIndex]
return Reflect.get(...arguments)
},
set: function (target, prop, value) {
let foundIndex = findShellyPropIndex(target, prop);
if (foundIndex >= 0) {
target[foundIndex] = value;
return true;
}
if (foundIndex === -1) {
target.push(value);
return true;
}
return Reflect.set(...arguments);
},
deleteProperty(target, prop) {
let foundIndex = findShellyPropIndex(target, prop);
if (foundIndex >= 0) {
target.splice(foundIndex, 1);
return true;
}
return Reflect.deleteProperty(...arguments);
}
};
const ShellyListSSEProxyHandler = {
get: function (target, prop) {
let foundIndex = findShellyPropIndex(target, prop);
if (foundIndex >= 0) return target[foundIndex].getSSEobj();
return Reflect.get(...arguments);
}
};
class ShellyFinder {
coIoTserver
_shellylist
shellylist
shellyListSSE
appStorage
storage
sse
constructor(sse) {
this.sse = sse;
this.coIoTserver = new CoIoTServer();
this._shellylist = new Array;
// Create a proxy for array of Shellys that has key lookup convenience
this.shellylist = new Proxy(this._shellylist, ShellyListProxyHandler);
// Set the initial data sse connections as abbreviated version of _shellylist
this.shellyListSSE = new Proxy(this._shellylist, ShellyListSSEProxyHandler);
this.appStorage = NodePersist.create({ dir: process.cwd() + '/.node-persist/shellyList' });
this.storage = NodePersist.create({ dir: process.cwd() + '/.node-persist/shellyDevices' })
}
get prefs() {
return (async () => {
try {
const appPrefs = (await this.appStorage.getItem('prefs')) ?? {};
if (_.isEmpty(appPrefs.shellyUser)) appPrefs.shellyUser = 'shelly';
if (!_.isString(appPrefs.shellyPassword)) appPrefs.shellyPassword = '';
if (!_.isBoolean(appPrefs.enableMQTT)) appPrefs.enableMQTT = false;
if (!_.isString(appPrefs.MQTTURL)) appPrefs.MQTTURL = 'mqtt.homeassistant.local:1833';
if (!_.isBoolean(appPrefs.MQTTauth)) appPrefs.MQTTauth = false;
if (!_.isString(appPrefs.MQTTuser)) appPrefs.MQTTuser = '';
if (!_.isString(appPrefs.MQTTpassword)) appPrefs.MQTTpassword = '';
this.appStorage.setItem('prefs', appPrefs);
return appPrefs;
} catch (e) {
return {}; // fallback value
}
})();
}
async start(app) {
await this.appStorage.init();
const prefs = await this.prefs;
const deviceAuth = await this.appStorage.getItem('deviceAuth');
await this.storage.init({ forgiveParseErrors: true });
try {
let importlist = [];
try {
importlist = await this.storage.values();
} catch (err) {
console.error(`Error: ${err.message} while trying reading storage values`);
}
await Promise.all(importlist.map(async (importShelly) => {
try {
let newShelly = await new Shelly(importShelly.type, importShelly.id, importShelly.ip, this.sse, this.storage).initialized;
newShelly.revive(importShelly);
newShelly.online = false;
newShelly.connected = false;
newShelly.locked = true;
this.shellylist[newShelly.deviceKey] = newShelly;
newShelly.start();
} catch (err) { console.error(`Error: ${err.message} while trying read persisted device`); }
}));
console.info(`Loaded ${this.shellylist.length} Shellies from disk storage`);
this.sse.updateInit(this.shellyListSSE);
this.sse.send('shellysLoad', this.shellyListSSE);
console.info(`Start coIoT Discovery`);
this.coIoTserver.on('status', async (status) => {
try {
const deviceKey = `${status.deviceType}-${status.deviceId}`;
if (_.isObject(this.shellylist[deviceKey])) {
//console.error(`coIoTserver.on: Ignore device. It already exists!`)
return;
}
console.info(`CoIoT Discovered new device with ID ${status.deviceId} and type ${status.deviceType}`);
const shelly = await new Shelly(status.deviceType, status.deviceId, status.location.host, this.sse, this.storage).initialized;
shelly.start();
this.shellylist[deviceKey] = shelly;
shelly.persist();
shelly.ssesend('shellyCreate');
} catch (err) { console.error(`Error: ${err.message} in coIoTserver.on`); }
});
this.coIoTserver.listen();
} catch (err) { console.error(`Error: ${err.message} shelly-coap start`); }
app.locals.shellylist = this.shellylist;
app.locals.shellyFinder = this;
return this;
}
}
module.exports = ShellyFinder;