-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
376 lines (308 loc) · 9.75 KB
/
main.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
376
"use strict";
/*
* Created with @iobroker/create-adapter v1.34.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
// Load your modules here, e.g.:
// const fs = require("fs");
const WebSocketServer = require('ws');
class SmarthomeWebots extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "smarthome_webots",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("objectChange", this.onObjectChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
this.server = null;
this.reconnectTimer = null;
this.pingInterval = null;
this.devices = {};
this.clients = {};
this.lock = false;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// The adapters config (in the instance object everything under the attribute "native") is accessible via
// this.config:
this.log.info("Cleanup devices on start: " +this.config.cleanup_devices);
this.log.info("config webots_url: " + this.config.webots_port);
if (this.config.cleanup_devices){
this.deleteAllDevices();
}
this.connect_webots(this.config.webots_port);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// Here you must clear all timeouts or intervals that may still be active
// clearTimeout(timeout1);
// clearTimeout(timeout2);
// ...
// clearInterval(interval1);
this.log.info("Unloading Adapter");
callback();
} catch (e) {
callback();
}
}
// If you need to react to object changes, uncomment the following block and the corresponding line in the constructor.
// You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`.
// /**
// * Is called if a subscribed object changes
// * @param {string} id
// * @param {ioBroker.Object | null | undefined} obj
// */
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.info(`object ${id} deleted`);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
//if (!this.lock){
//this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (!state.ack){
// send only if state is updated from iobrokers side
var devname = id.split('.');
var msg = {
name: devname[devname.length-2],
property: devname[devname.length-1],
value: state.val
};
this.sendWebots(msg.name,msg);
}
//}
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
// If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor.
// /**
// * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
// * Using this method requires "common.messagebox" property to be set to true in io-package.json
// * @param {ioBroker.Message} obj
// */
// onMessage(obj) {
// if (typeof obj === "object" && obj.message) {
// if (obj.command === "send") {
// // e.g. send email or pushover or whatever
// this.log.info("send command");
// // Send response in callback if required
// if (obj.callback) this.sendTo(obj.from, obj.command, "Message received", obj.callback);
// }
// }
// }
async deleteAllDevices(){
this.log.info("delete all devices");
const objects = await this.getAdapterObjectsAsync(); // Alle folder, device, channel und state Objekte
// Loop through all objects and devices and delete them
for (const id in objects) {
this.delObject(id, (err) => {
if (err) {
this.log.error(`Error deleting object ${id}: ${err}`);
}
});
}
}
heartbeat() {
this.isAlive = true;
}
connect_webots(port){
this.log.info("Starting websocket server on port: " + port);
var that = this;
that.server = new WebSocketServer.Server({ port: port })
that.server.on('connection', function connection(ws) {
clearInterval(that.reconnectTimer);
that.log.info('Device connected');
ws.isAlive = true;
ws.on('pong', that.heartbeat);
ws.on('message', function message(data) {
//that.log.info("Received: '" + data + "'");
try{
const msg = JSON.parse(data);
switch (msg['type']) {
case 'object':
that.add_client(msg['name'],ws);
that.addDevice(msg);
break;
case 'state':
that.updateState(msg['name'], msg['data']['name'],msg['data']['value'] )
break;
case 'reset':
that.deleteSHDevice(msg['name']);
break;
default:
break;
}
} catch (e) {
that.log.warn("not JSON: " + e.stack + " -> " + data );
}
if (typeof data === 'string') {
if (data == "RESET-SH"){
that.reset();
}
}
});
});
const interval = setInterval(function ping() {
that.server.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
that.server.on('close', function close() {
clearInterval(interval);
that.log.info('Client Closed');
that.log.warn('server is closed. Reconnect will be attempted in 3 seconds.');
that.reconnectTimer = setTimeout(function() {
that.connect_webots(port);
}, 3000);
});
}
sendWebots(name,msg){
this.log.info("Sending Message to Client [" + name + "]: " + msg);
var client = this.clients[name];
if (client != null){
if ( this.server != null && client.isAlive) {
client.send(JSON.stringify(msg));
}
}else this.log.warn("Client " + name + " is not connected" )
}
async reset(){
this.log.info("Reseting Devices");
for (const [name, data] of Object.entries(this.devices)) {
this.log.info(name)
await this.deleteDeviceAsync(name);
}
}
async deleteSHDevice(name){
await this.deleteDeviceAsync(name);
if (this.devices.hasOwnProperty(name)){
delete this.devices[name]
}
}
async addDevice(dict){
var dev_name = dict["name"];
//check if device has to be created exists
if(!(dev_name in this.devices)){
// create new Device entry
this.log.info("Add Device: " + dev_name);
this.devices[dev_name] = dict["data"];
await this.deleteDeviceAsync(dev_name)
await this.createDeviceAsync(dev_name);
for (const [state_name, data] of Object.entries(this.devices[dev_name])) {
if (data['remap'] != null){
this.log.warn("Skip State: " + state_name + " remapped -> " + data['remap']);
continue;
}
this.log.info("Add State: " +state_name + " Data: " +data['value'] + " type: " +typeof(data['value']))
let state = {};
state.role = "state";
state.name = state_name;
state.desc = data['description'];
state.def = data['value'];
state.type = typeof(data['value']);
state.read = data['read'];
state.write = data['write'];
if (data['min'] != null)
state.min = data['min'];
if (data['max'] != null)
state.max = data['max'];
state.unit = data["unit"];
await this.setObjectAsync(dev_name +"."+ state_name, {
type: "state",
common: state,
native: {},
});
}
this.subscribeStates(dev_name+".*");
}
}
async update_device(dict){
this.log.info("Update Device:" + dict["name"]);
var dev_name = dict["name"];
// this.log.info("LOCK DEVICE:" + dict["name"]);
// await this.unsubscribeStatesAsync(dev_name+".*");
// this.lock = true;
//check if device has to be created exists
if(!(dev_name in this.devices)){
// create new Device entry
this.log.info("Add Device: " + dev_name);
this.devices[dev_name] = dict["data"];
Object.entries(dict).forEach(([key, value]) => {
this.log.info(`${key}: ${value}`);
});
await this.createDeviceAsync(dev_name);
for (const [state_name, data] of Object.entries(this.devices[dev_name])) {
this.log.info("Add State: " +state_name + " Data: " +data + " type: " +typeof(data["value"]))
await this.setObjectAsync(dev_name +"."+ state_name, {
type: "state",
common: {
name: state_name,
type: typeof(data["value"]),
role: "value",
read: true,
write: true,
def: data["value"],
},
native: {},
});
}
this.subscribeStates(dev_name+".*");
}else{
// update states
this.devices[dev_name] = dict["data"];
const state_name = this.devices[dev_name]["name"];
const value = this.devices[dev_name]["value"];
//for (const [state_name, data] of Object.entries(this.devices[dev_name])) {
this.log.info("SetState: " + dev_name +"."+state_name + " to " + value);
await this.setStateAsync(dev_name +"."+ state_name, {val: value , ack:true});
}
}
// await this.subscribeStatesAsync(dev_name+".*");
// this.lock = false
// this.log.info("UNLOCK DEVICE:" + dict["name"]);
add_client(name,ws){
this.clients[name] = ws
}
async updateState(device, name, value){
this.setStateAsync(device +"."+ name, value, true);
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new SmarthomeWebots(options);
} else {
// otherwise start the instance directly
new SmarthomeWebots();
}