-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
505 lines (467 loc) · 21.7 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* LinkTap adapter
*
* Controls LinkTap Wireless Water Timer via LinkTap Gateway
*
* Copyright 2021 Smart-Gang <[email protected]>,
* MIT License
*
*/
'use strict';
const utils = require('@iobroker/adapter-core');
const LinkTapApiController = require("./lib/linktap_api_controller");
class LinkTap extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'linktap',
});
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.connected = false;
this.dataPollIntervalWatering = 60000;
this.dataPollTimeoutWatering = null;
this.dataPollIntervalTaplinker = 300000;
this.dataPollTimeoutTaplinker = null;
this.dataPollIntervalHistory = 3600000;
this.dataPollTimeoutHistory = null;
this.myApiController = null;
}
/**
* Gets ids for channels and states
* @param {string} gatewayId ID of gateway
* @param {object} taplinkerId ID of taplinker
* @param {object} stateKey ID of state
*/
getId(gatewayId, taplinkerId, stateKey){
if(taplinkerId === null && stateKey === null){
return gatewayId;
}
if(gatewayId != null && taplinkerId != null && stateKey == null){
return gatewayId+'.'+taplinkerId
}
if(taplinkerId === null) {
return gatewayId+'.'+stateKey;
}
return gatewayId+'.'+taplinkerId+'.'+stateKey;
}
/**
* Gets the current value for the state
* @param {string} id ID of state
*/
getCurrentState(id) {
return new Promise((resolve, reject) => {
this.getState(id, function (err, state) {
if (err) {
reject(err);
} else {
if (typeof state != undefined && state != null) {
const value = state.val;
resolve(value);
} else {
const value = false;
resolve(value);
}
}
});
});
}
/**
* Creates all channels
*/
async createChannels() {
const fctName = 'createChannels';
this.log.info(fctName + ' started');
if(this.myApiController != null ){
for(const g of this.myApiController.gateways) {
await this.setObjectAsync(this.getId(g.gatewayId, null, null), {
type: 'channel',
role: 'gateway',
common: {
name: g.name,
},
native: {}
});
for(const d of g.devices) {
await this.setObjectAsync(this.getId(g.gatewayId, d.taplinkerId, null), {
type: 'channel',
role: 'device',
common: {
name: d.taplinkerName,
},
native: {}
});
}
}
}
this.log.info(fctName + ' finished');
}
/**
* Creates or updates adapter objects
*/
async createOrUpdateObject(id, object){
const foundObject = await this.getObjectAsync(id);
if (foundObject == null) {
await this.setObjectAsync(id, object);
this.log.info('Creating new object: '+id);
return; //new object
}
if(foundObject.hasOwnProperty("common") && object.hasOwnProperty("common")) {
if(JSON.stringify(foundObject.common) !== JSON.stringify(object.common)) {
this.log.info('Update required for changed property: common for object: '+id);
await this.setObjectAsync(id, object); //updating changed object
}
}
}
/**
* Creates the data points
*/
async createDataPoints() {
const fctName = 'createDataPoints';
this.log.info(fctName + ' started');
if(this.myApiController != null ){
const gatewayStructure = require("./lib/gateway.json");
const taplinkerStructure = require("./lib/taplinker.json");
for(const g of this.myApiController.gateways) {
const gatewayObject = Object.assign({}, gatewayStructure);
for (const gatewayProp in gatewayObject) {
var dataPointId = this.getId(g.gatewayId, null, gatewayProp)
await this.createOrUpdateObject(dataPointId, gatewayObject[gatewayProp]);
if(gatewayObject[gatewayProp].common.write === false){
this.setState(dataPointId, { val: g[gatewayProp], ack: true });
}
}
for(const d of g.devices) {
const taplinkerObject = Object.assign({}, taplinkerStructure);
for (const taplinkerProp in taplinkerObject) {
var dataPointId = this.getId(g.gatewayId, d.taplinkerId, taplinkerProp)
await this.createOrUpdateObject(dataPointId, taplinkerObject[taplinkerProp]);
if(taplinkerObject[taplinkerProp].common.write === false){
this.setState(dataPointId, { val: d[taplinkerProp], ack: true });
} else {
if(taplinkerObject[taplinkerProp].common.role === "button"){
this.setState(dataPointId, { val: false, ack: true });
}
}
}
}
}
}
this.log.info(fctName + ' finished');
}
/**
* Creates data polling schedule for gateways and devices
*/
createTaplinkerScheduler() {
const fctName = 'createTaplinkerScheduler';
this.log.info(fctName + ' started');
if(this.dataPollTimeoutTaplinker !== null) {
clearInterval(this.dataPollTimeoutTaplinker);
this.dataPollTimeoutTaplinker = null;
this.log.info(fctName + ' scheduler stopped');
}
this.dataPollTimeoutTaplinker = setInterval(async () => {
var fctName = 'updateTaplinkerStatus';
this.log.info(fctName + ' started');
if(this.myApiController != null ){
await this.myApiController.getDevices();
}
this.setTaplinkerStates();
this.log.info(fctName + ' finished');
}, this.dataPollIntervalTaplinker);
this.log.info(fctName + ' finished');
}
/**
* Creates data polling schedule for watering state
*/
createWateringScheduler() {
const fctName = 'createWateringScheduler';
this.log.info(fctName + ' started');
if(this.dataPollTimeoutWatering !== null) {
clearInterval(this.dataPollTimeoutWatering);
this.dataPollTimeoutWatering = null;
this.log.info(fctName + ' scheduler stopped');
}
this.dataPollTimeoutWatering = setInterval(async () => {
var fctName = 'updateWateringStatus';
this.log.info(fctName + ' started');
if(this.myApiController != null ){
await this.myApiController.getWateringStatus();
}
this.setWateringStates();
this.log.info(fctName + ' finished');
}, this.dataPollIntervalWatering);
this.log.info(fctName + ' finished');
}
/**
* Creates data polling schedule for history
*/
createHistoryScheduler() {
const fctName = 'createHistoryScheduler';
this.log.info(fctName + ' started');
if(this.dataPollTimeoutHistory !== null) {
clearInterval(this.dataPollTimeoutHistory);
this.dataPollTimeoutHistory = null;
this.log.info(fctName + ' scheduler stopped');
}
this.dataPollTimeoutHistory = setInterval(async () => {
var fctName = 'updateHistory';
this.log.info(fctName + ' started');
if(this.myApiController != null ){
await this.myApiController.getHistory();
}
this.setHistoryState();
this.log.info(fctName + ' finished');
}, this.dataPollIntervalHistory);
this.log.info(fctName + ' finished');
}
/**
* Set watering states
*/
setWateringStates(){
if(this.myApiController != null ){
this.myApiController.gateways.forEach((g) => {
g.devices.forEach(d => {
this.setState(this.getId(g.gatewayId,d.taplinkerId,'watering'), { val: d.watering, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'vel'), { val: d.vel, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'vol'), { val: d.vol, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'total'), { val: d.total, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'onDuration'), { val: d.onDuration, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'ecoTotal'), { val: d.ecoTotal, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'ecoOn'), { val: d.ecoOn, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'ecoOff'), { val: d.ecoOff, ack: true });
});
});
}
}
/**
* Set gateway / device states
*/
setTaplinkerStates(){
if(this.myApiController != null ){
this.myApiController.gateways.forEach((g) => {
this.setState(this.getId(g.gatewayId,null,'name'), { val: g.name, ack: true });
this.setState(this.getId(g.gatewayId,null,'status'), { val: g.status, ack: true });
this.setState(this.getId(g.gatewayId,null,'location'), { val: g.location, ack: true });
this.setState(this.getId(g.gatewayId,null,'version'), { val: g.version, ack: true });
this.setState(this.getId(g.gatewayId,null,'gatewayId'), { val: g.gatewayId, ack: true });
g.devices.forEach(d => {
this.setState(this.getId(g.gatewayId,d.taplinkerId,'taplinkerId'), { val: d.taplinkerId, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'taplinkerName'), { val: d.taplinkerName, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'location'), { val: d.location, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'status'), { val: d.status, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'version'), { val: d.version, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'signal'), { val: d.signal, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'batteryStatus'), { val: d.batteryStatus, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'workMode'), { val: d.workMode, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'fall'), { val: d.fall, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'valveBroken'), { val: d.valveBroken, ack: true });
this.setState(this.getId(g.gatewayId,d.taplinkerId,'noWater'), { val: d.noWater, ack: true });
});
});
}
}
/**
* Set history state
*/
setHistoryState(){
if(this.myApiController != null ){
this.myApiController.gateways.forEach((g) => {
g.devices.forEach(d => {
this.setState(this.getId(g.gatewayId,d.taplinkerId,'history'), { val: d.history, ack: true });
});
});
}
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
this.setConnected(false);
this.log.info('User : ' + this.config.txtUsername);
if (!this.config.txtUsername || !this.config.txtApiKey) {
this.log.warn('Please open Admin page for this adapter to set the username and the API key.');
return;
}
if(isNaN(this.config.txtPollIntervalDevices) || this.config.txtPollIntervalDevices === "" || this.config.txtPollIntervalDevices === null){
this.log.warn('No valid device poll interval found. Set poll interval to 10 minute.');
} else {
var parsedPollIntervalDevices = parseInt(this.config.txtPollIntervalDevices, 10);
if(parsedPollIntervalDevices < 5) parsedPollIntervalDevices = 5;
this.log.info('Set device poll interval to '+parsedPollIntervalDevices+ ' minute(s).');
this.dataPollIntervalTaplinker = (parsedPollIntervalDevices *60 * 1000)
}
if(isNaN(this.config.txtPollIntervalWatering) || this.config.txtPollIntervalWatering === "" || this.config.txtPollIntervalWatering === null){
this.log.warn('No valid watering poll interval found. Set poll interval to 1 minute.');
} else {
var parsedPollIntervalWatering = parseInt(this.config.txtPollIntervalWatering, 10);
if(parsedPollIntervalWatering < 1) parsedPollIntervalWatering = 1;
this.log.info('Set water state poll interval to '+parsedPollIntervalWatering+ ' minute(s).');
this.dataPollIntervalWatering = (parsedPollIntervalWatering *60 * 1000)
}
if(isNaN(this.config.txtPollIntervalHistory) || this.config.txtPollIntervalHistory === "" || this.config.txtPollIntervalHistory === null){
this.log.warn('No valid history poll interval found. Set poll interval to 10 minute.');
} else {
var parsedPollIntervalHistory = parseInt(this.config.txtPollIntervalHistory, 10);
if(parsedPollIntervalHistory < 10) parsedPollIntervalHistory = 10;
this.log.info('Set history state poll interval to '+parsedPollIntervalHistory+ ' minute(s).');
this.dataPollIntervalHistory = (parsedPollIntervalHistory *60 * 1000)
}
const foreignObject = this.getForeignObject('system.config', (err, obj) => {
this.myApiController = new LinkTapApiController({
logger: this.log,
username: this.config.txtUsername,
apiKey: this.config.txtApiKey
});
this.queryAndCreateStructure();
});
}
/**
* Queries gateways and devices and creates the data structure
*/
async queryAndCreateStructure(){
await this.myApiController.getDevices();
await this.myApiController.getWateringStatus();
await this.myApiController.getHistory();
await this.createChannels();
await this.createDataPoints();
this.subscribeStates('*');
this.setConnected(this.myApiController.connected);
this.createTaplinkerScheduler();
this.createWateringScheduler();
this.createHistoryScheduler();
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
if(this.dataPollTimeoutTaplinker !== null) clearInterval(this.dataPollTimeoutTaplinker);
if(this.dataPollTimeoutWatering !== null) clearInterval(this.dataPollTimeoutWatering);
if(this.dataPollTimeoutHistory !== null) clearInterval(this.dataPollTimeoutHistory);
this.setConnected(false);
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 (!id || !state || state.ack) return;
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (id.endsWith('ActivateSevenDayMode')
|| id.endsWith('ActivateOddEvenMode')
|| id.endsWith('ActivateMonthMode')
|| id.endsWith('ActivateIntervalMode')
|| id.endsWith('StartInstantMode')
|| id.endsWith('StopInstantMode')
|| id.endsWith('StartEcoInstantMode')) {
if (this.myApiController != null) {
var idParts = id.split('.');
if (idParts.length != 5) {
this.logger.error("ID: " + id + " has invalid format.");
return "Error while activating scheduling mode.";
}
switch (idParts[4]) {
case "StartInstantMode":
idParts.pop();
this.getCurrentState(idParts.join('.') + '.' + 'InstantModeDuration').then((value) => {
if(state.val === true) {
this.myApiController.startInstantMode(idParts, value);
this.setState(id, { val: false, ack: true });
}
});
break;
case "StartEcoInstantMode":
idParts.pop();
this.getCurrentState(idParts.join('.') + '.' + 'InstantModeDuration').then((value) => {
this.getCurrentState(idParts.join('.') + '.' + 'EcoInstantModeOn').then((ecoOn) => {
this.getCurrentState(idParts.join('.') + '.' + 'EcoInstantModeOff').then((ecoOff) => {
if(state.val === true) {
this.myApiController.startEcoInstantMode(idParts, value, ecoOn, ecoOff);
this.setState(id, { val: false, ack: true });
}
});
});
});
break;
case "StopInstantMode":
if(state.val === true) {
this.myApiController.stopInstantMode(idParts);
this.setState(id, { val: false, ack: true });
}
break;
default:
if(state.val === true) {
this.myApiController.activateSchedulingMode(idParts);
this.setState(id, { val: false, ack: true });
}
break;
}
}
}
}
// 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.message" 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);
// }
// }
// }
/**
* Set connected state
* @param {boolean} isConnected
*/
setConnected(isConnected) {
if (this.connected !== isConnected) {
this.connected = isConnected;
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new LinkTap(options);
} else {
// otherwise start the instance directly
new LinkTap();
}