-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
442 lines (386 loc) · 11.3 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"use strict";
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const express = require("express");
const { WebServer } = require("@iobroker/webserver");
const xml = require("xml2js");
//Geräte
const Lex10 = require("./lib/Lex10.js");
const LexPlus10SL = require("./lib/LexPlus10SL.js");
class Syrconnect extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "syrconnect",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
this.devicesMap = new Map();
this.xmlStart = '<?xml version="1.0" encoding="utf-8"?><sc version="1.0"><d>';
this.xmlEnd = "</d></sc>";
this.basicC = ["getSRN", "getVER", "getTYP", "getCNA"];
this.rpwFixStates = {
"0000000": "NN",
"0000001": "Mo",
"0000010": "Di",
"0000100": "Mi",
"0001000": "Do",
"0010000": "Fr",
"0100000": "Sa",
1000000: "So",
};
this.server = null;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.setState("info.connection", false, true);
this.initWebServer(this.config);
}
getPermutations(list, maxLen) {
// Copy initial values as arrays
const perm = list.map(function (val) {
return [val];
});
// Our permutation generator
const generate = function (perm, maxLen, currLen) {
// Reached desired length
if (currLen === maxLen) {
return perm;
}
// For each existing permutation
for (let i = 0, len = perm.length; i < len; i++) {
const currPerm = perm.shift();
// Create new permutation
for (let k = 0; k < list.length; k++) {
perm.push(currPerm.concat(list[k]));
}
}
// Recurse
return generate(perm, maxLen, currLen + 1);
};
// Start with size 1 because of initial values
return generate(perm, maxLen, 1);
}
getBinary(arr) {
let bin = "";
for (let i = 0; i < arr.length; i++) {
bin += arr[i];
}
return bin;
}
/**
* Wird aus createObjectWithState aufgerufen, weil die Funktion in meta für getRPW eingetragen ist.
* Die möglichen Werte werden wie folgt ermittelt:
* - Basis sind alle 7stelligen Permutationen von 0 und 1. Daraus ergeben sich also Binärzahlen der Form 0000001
* - Jede 1 steht je nach Position in der Binärzahl für einen Wochentag
* - Mehrere Einsen bedeuten dann eine Kombination aus mehreren Tagen
* - 0000010 = 2 = Dienstag
* - 0000011 = 3 = Montag und Dienstag
* - 0000100 = 4 = Mittworch
* @returns
*/
createRPWStates() {
const ret = {};
const res = this.getPermutations([0, 1], 7);
for (let i = 0; i < res.length; i++) {
const bin = this.getBinary(res[i]);
//Unbekannte States hinzugügen
// eslint-disable-next-line no-prototype-builtins
if (!this.rpwFixStates.hasOwnProperty(bin) && (bin.match(/1/g) || []).length > 1) {
const tmpArr = [];
const tmp = new Array(res[i].length).fill(0);
//Ermittelt die Position der Einsen, um damit die fixen Tage zu ermitteln
for (let l = 0; l < res[i].length; l++) {
if (res[i][l] == 1) {
tmp[l] = 1;
tmpArr.unshift(this.rpwFixStates[this.getBinary(tmp)]);
tmp.fill(0);
}
}
this.rpwFixStates[bin] = tmpArr.join(";");
}
//Dezimalzahlen sind die neuen Keys
ret[parseInt(bin, 2)] = this.rpwFixStates[bin];
}
return ret;
}
async createObjectWithState(dev, name, value) {
let obj = {};
const dp = dev[name];
//Root-Knoten
await this.setObjectNotExistsAsync(dev.name, {
type: "device",
common: {
name: "Device",
},
native: {},
});
//Serial des Gerätes
await this.setObjectNotExistsAsync(dev.name + "." + dev.id, {
type: "channel",
common: {
name: dev.name,
},
native: {},
});
const id = dev.name + "." + dev.id + "." + name;
//String
if (dp.type == "string") {
obj = {
_id: id,
type: "state",
common: {
name: dp.label,
type: dp.type,
role: "text",
read: true,
write: dp.writable,
},
native: {},
};
//Number
} else if (dp.type == "number") {
obj = {
_id: id,
type: "state",
common: {
name: dp.label,
type: dp.type,
unit: dp.unit,
min: dp.min,
max: dp.max,
def: dp.def,
role: dp.writable ? "level" : "value",
read: true,
write: dp.writable,
},
native: {},
};
if (dp.min == null) {
delete obj.common.min;
}
if (dp.max == null) {
delete obj.common.max;
}
if (dp.def == null) {
delete obj.common.def;
}
if (dp.states) {
if (dp.states instanceof Object) {
obj.common.states = dp.states;
} else {
obj.common.states = eval("this." + dp.states);
}
}
//Boolean
} else if (dp.type == "boolean") {
obj = {
_id: id,
type: "state",
common: {
name: dp.label,
type: dp.type,
def: dp.def,
role: dp.writable ? "switch" : "indicator",
read: true,
write: dp.writable,
},
native: {},
};
}
try {
//Objekt erstellen
// @ts-ignore
await this.setObjectNotExistsAsync(obj._id, obj);
//Value setzen
// @ts-ignore
this.setState(obj._id, value, true);
this.log.debug("Object created: " + this.namespace + "." + obj._id);
} catch (e) {
this.log.error("Object " + obj._id + " error on creation: " + e.message);
}
}
getXmlBasicC() {
let ret = "";
this.basicC.forEach((c) => (ret += '<c n="' + c + '" v=""/>'));
return ret;
}
getXmlAllC(device) {
let ret = "";
if (device) {
for (const key in device) {
if (key.startsWith("get")) {
//Wenn der Datenpunkt geändert wurde, muss im Response-XML eine Zeile mit dem set statt get und dem
//neuen Value hinzugefügt werden.
if (device.changed.has(key)) {
ret += '<c n="' + key.replace(/get/g, "set") + '" v="' + device.changed.get(key) + '"/>';
device.changed.delete(key);
}
ret += '<c n="' + key + '" v=""/>';
}
}
}
return ret;
}
async initWebServer(settings) {
this.server = null;
const app = express();
// for parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
app.post("/WebServices/SyrConnectLimexWebService.asmx/GetBasicCommands", (req, res) => {
this.basicCommands(req, res);
});
app.post("/GetBasicCommands", (req, res) => {
this.basicCommands(req, res);
});
app.post("/WebServices/SyrConnectLimexWebService.asmx/GetAllCommands", (req, res) => {
this.allCommands(req, res);
});
app.post("/GetAllCommands", (req, res) => {
this.allCommands(req, res);
});
const webServer = new WebServer({ app: app, adapter: this, secure: false });
const server = await webServer.init();
server.listen(parseInt(settings.webport, 10) || 8090, settings.host, () => {
this.log.info("Webserver started listening on " + settings.host + ":" + settings.webport);
this.server = server;
this.setState("info.connection", true, true);
});
server.on("error", (e) => {
//https://stackoverflow.com/questions/9164915/node-js-eacces-error-when-listening-on-most-ports
if (e.toString().includes("EACCES") && settings.webport <= 1024) {
this.log.error(
`node.js process has no rights to start server on the port ${settings.webport}.\n` +
`Do you know that on linux you need special permissions for ports under 1024?\n`,
);
} else if (e.toString().includes("EADDRINUSE")) {
this.log.error(
`Cannot start server on ${settings.host}:${settings.webport}: Port is in use. Please configure another in instance settings`,
);
} else {
this.log.error(`Cannot start server on ${settings.host}:${settings.webport}: ${e}`);
}
this.terminate
? this.terminate(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION)
: process.exit(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION);
});
}
basicCommands(req, res) {
this.log.debug(req.path);
res.set("Content-Type", "text/xml");
res.send(this.xmlStart + this.getXmlBasicC() + this.xmlEnd);
}
allCommands(req, res) {
this.log.debug(req.path + ": " + req.body.xml);
const _this = this;
xml.parseStringPromise(req.body.xml)
.then(async function (result) {
const json = result.sc.d[0].c;
const device = await _this.getDevice(json);
if (device) {
for (let i = 0; i < json.length; i++) {
const id = json[i].$.n.replace(_this.FORBIDDEN_CHARS, "_");
const newVal = device.convertToGUI(id, json[i].$.v);
//Objekte erzeugen und Values setzen, wenn es sich um neue Daten handelt
const knownObj = await _this.getObjectAsync(device.name + "." + device.id + "." + id);
if (!knownObj) {
await _this.createObjectWithState(device, id, newVal);
} else {
//Bei bekannten Objekten, den Status bei Bedarf aktualisieren
const state = await _this.getStateAsync(device.name + "." + device.id + "." + id);
if (state) {
const oldVal = state.val;
if ((state.ack && oldVal != newVal) || (!state.ack && oldVal == newVal)) {
_this.setState(device.name + "." + device.id + "." + id, newVal, true);
}
}
}
}
//Event für Statusänderungen für alle Objekte des Adapters registrieren
_this.subscribeStates("*");
}
//Response senden
res.set("Content-Type", "text/xml");
const responseXml = _this.xmlStart + _this.getXmlAllC(device) + _this.xmlEnd;
res.send(responseXml);
_this.log.debug("Response: " + responseXml);
})
.catch(function (err) {
_this.log.error(err);
});
}
async getDevice(json) {
let name,
ser = "";
for (let i = 0; i < json.length; i++) {
const id = json[i].$.n;
if (id == "getCNA") {
name = json[i].$.v.toLowerCase();
}
if (id == "getSRN") {
ser = json[i].$.v.toLowerCase();
}
if (name && ser) {
//Device erzeugen und speichern
if (!this.devicesMap.has(ser)) {
if (name.toLowerCase() == "lex10") {
this.devicesMap.set(ser, new Lex10(ser, name));
} else if (name.toLowerCase() == "lexplus10sl") {
this.devicesMap.set(ser, new LexPlus10SL(ser, name));
}
}
break;
}
}
return this.devicesMap.get(ser);
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// @ts-ignore
this.server.close();
callback();
} catch (e) {
this.log.debug("Exception while unload: " + e);
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} oid
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(oid, state) {
// The state was changed by GUI
if (state && !state.ack) {
let val = state.val;
this.log.debug("state " + oid + " changed: " + val + " (ack = " + state.ack + ")");
//Änderungen für Response merken
const arr = oid.split(".");
oid = arr[arr.length - 1];
const device = this.devicesMap.get(arr[arr.length - 2]);
val = device.convertFromGUI(oid, val);
device.changed.set(oid, val);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Syrconnect(options);
} else {
// otherwise start the instance directly
new Syrconnect();
}