forked from jensweigele/ioBroker.yahka
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyahka.function-factory.js
More file actions
430 lines (430 loc) · 19.3 KB
/
Copy pathyahka.function-factory.js
File metadata and controls
430 lines (430 loc) · 19.3 KB
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
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var yahka_homekit_bridge_1 = require("./yahka.homekit-bridge");
var TIoBrokerInOutFunction_State = (function () {
function TIoBrokerInOutFunction_State(adapter, stateName, deferredTime) {
if (deferredTime === void 0) { deferredTime = 0; }
this.adapter = adapter;
this.stateName = stateName;
this.deferredTime = deferredTime;
this.debounceTimer = -1;
this.subscriptionRequests = [];
this.addSubscriptionRequest(stateName);
}
TIoBrokerInOutFunction_State.prototype.addSubscriptionRequest = function (stateName) {
var subscriptionEvent = this.subscriptionEvent.bind(this, stateName);
this.subscriptionRequests.push({
subscriptionType: 'state',
subscriptionIdentifier: stateName,
subscriptionEvent: subscriptionEvent
});
};
TIoBrokerInOutFunction_State.prototype.getValueOnRead = function (ioState) {
if (ioState)
return ioState.val;
else
return null;
};
TIoBrokerInOutFunction_State.prototype.getValueOnNotify = function (ioState) {
if (ioState)
return ioState.val;
else
return null;
};
TIoBrokerInOutFunction_State.prototype.toIOBroker = function (plainIoValue, callback) {
var _this = this;
this.adapter.log.debug('writing state to ioBroker [' + this.stateName + ']: ' + JSON.stringify(plainIoValue));
this.adapter.getForeignState(_this.stateName, function (error, ioState) {
var value = _this.getValueOnRead(ioState);
_this.adapter.log.debug('checking value change: ' + value + ' to ' + JSON.stringify(plainIoValue));
// set state only, when changed (prevents setting brightness on hue lamps)
if (value !== plainIoValue) {
_this.adapter.setForeignState(_this.stateName, plainIoValue, false, function (error) {
if (error)
_this.adapter.log.error('setForeignState error [' + _this.stateName + '] to [' + JSON.stringify(plainIoValue) + ']: ' + error);
});
}
callback();
});
};
TIoBrokerInOutFunction_State.prototype.fromIOBroker = function (callback) {
var _this = this;
this.adapter.log.debug('reading state from ioBroker [' + this.stateName + ']');
this.adapter.getForeignState(this.stateName, function (error, ioState) {
_this.adapter.log.debug('read state from ioBroker [' + _this.stateName + ']: ' + JSON.stringify(ioState));
if (error)
_this.adapter.log.error('... with error: ' + error);
var value = _this.getValueOnRead(ioState);
callback(error, value);
});
};
TIoBrokerInOutFunction_State.prototype.subscriptionEvent = function (stateName, ioState, callback) {
this.adapter.log.debug('change event from ioBroker via [' + this.stateName + ']' + JSON.stringify(ioState));
var newValue = this.getValueOnNotify(ioState);
if (newValue !== undefined)
this.executeCallback(callback, newValue);
else
this.adapter.log.debug('state was filtered - notification is canceled');
};
TIoBrokerInOutFunction_State.prototype.executeCallback = function (callback, plainIOValue) {
if (this.deferredTime > 0)
this.setupDeferredChangeEvent(callback, plainIOValue);
else
callback(plainIOValue);
};
TIoBrokerInOutFunction_State.prototype.setupDeferredChangeEvent = function (callback, plainIOValue) {
this.cancelDeferredChangeEvent();
this.debounceTimer = setTimeout(this.deferredChangeEvent.bind(this, callback, plainIOValue), 150);
};
TIoBrokerInOutFunction_State.prototype.cancelDeferredChangeEvent = function () {
clearTimeout(this.debounceTimer);
this.debounceTimer = -1;
};
TIoBrokerInOutFunction_State.prototype.deferredChangeEvent = function (callback, plainIOValue) {
this.adapter.log.debug('[' + this.stateName + '] firing deferred change event:' + JSON.stringify(plainIOValue));
callback(plainIOValue);
};
return TIoBrokerInOutFunction_State;
}());
var TIoBrokerInOutFunction_State_OnlyACK = (function (_super) {
__extends(TIoBrokerInOutFunction_State_OnlyACK, _super);
function TIoBrokerInOutFunction_State_OnlyACK() {
return _super !== null && _super.apply(this, arguments) || this;
}
TIoBrokerInOutFunction_State_OnlyACK.prototype.getValueOnRead = function (ioState) {
if (ioState)
if (ioState.ack) {
this.lastAcknowledgedValue = ioState.val;
return ioState.val;
}
else {
this.adapter.log.debug("faking CurrentState.Read for [" + this.stateName + ']: ' + JSON.stringify(this.lastAcknowledgedValue));
return this.lastAcknowledgedValue;
}
else
return null;
};
TIoBrokerInOutFunction_State_OnlyACK.prototype.getValueOnNotify = function (ioState) {
if (ioState)
if (ioState.ack) {
this.lastAcknowledgedValue = ioState.val;
return ioState.val;
}
else {
this.adapter.log.debug("discarding CurrentState.Notify for [" + this.stateName + ']');
return undefined;
}
else
return null;
};
return TIoBrokerInOutFunction_State_OnlyACK;
}(TIoBrokerInOutFunction_State));
var TIoBrokerInOutFunction_State_OnlyNotACK = (function (_super) {
__extends(TIoBrokerInOutFunction_State_OnlyNotACK, _super);
function TIoBrokerInOutFunction_State_OnlyNotACK() {
return _super !== null && _super.apply(this, arguments) || this;
}
TIoBrokerInOutFunction_State_OnlyNotACK.prototype.getValueOnRead = function (ioState) {
if (ioState)
if (ioState.ack) {
this.adapter.log.debug("faking CurrentState.Read for [" + this.stateName + ']: ' + JSON.stringify(this.lastNotAcknowledgedValue));
return this.lastNotAcknowledgedValue;
}
else {
this.lastNotAcknowledgedValue = ioState.val;
return ioState.val;
}
else
return null;
};
TIoBrokerInOutFunction_State_OnlyNotACK.prototype.getValueOnNotify = function (ioState) {
if (ioState)
if (!ioState.ack) {
this.adapter.log.debug("discarding CurrentState.Notify for [" + this.stateName + ']');
return undefined;
}
else {
this.lastNotAcknowledgedValue = ioState.val;
return ioState.val;
}
else
return null;
};
return TIoBrokerInOutFunction_State_OnlyNotACK;
}(TIoBrokerInOutFunction_State));
var TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition = (function (_super) {
__extends(TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition, _super);
function TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition(adapter, stateName, workingItem) {
var _this = _super.call(this, adapter, stateName, 0) || this;
_this.adapter = adapter;
_this.stateName = stateName;
_this.workingItem = workingItem;
_this.lastWorkingState = false;
_this.lastAcknowledgedValue = undefined;
_this.debounceTimer = -1;
_this.addSubscriptionRequest(workingItem);
adapter.getForeignState(workingItem, function (error, ioState) {
if (ioState)
_this.lastWorkingState = ioState.val;
else
_this.lastWorkingState = undefined;
});
return _this;
}
TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition.prototype.subscriptionEvent = function (stateName, ioState, callback) {
if (!ioState)
return;
if (stateName == this.workingItem) {
this.adapter.log.debug('[' + this.stateName + '] got a working item change event: ' + JSON.stringify(ioState));
this.lastWorkingState = ioState.val;
this.setupDeferredChangeEvent(callback);
}
else if (stateName == this.stateName) {
this.adapter.log.debug('[' + this.stateName + '] got a target state change event:' + JSON.stringify(ioState));
if (ioState.ack) {
this.lastAcknowledgedValue = ioState.val;
this.setupDeferredChangeEvent(callback);
}
}
};
TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition.prototype.setupDeferredChangeEvent = function (callback) {
this.cancelDeferredChangeEvent();
this.debounceTimer = setTimeout(this.deferredChangeEvent.bind(this, callback), 150);
};
TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition.prototype.cancelDeferredChangeEvent = function () {
clearTimeout(this.debounceTimer);
this.debounceTimer = -1;
};
TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition.prototype.deferredChangeEvent = function (callback) {
if (!this.lastWorkingState) {
this.adapter.log.debug('[' + this.stateName + '] firing target state change event:' + JSON.stringify(this.lastAcknowledgedValue));
callback(this.lastAcknowledgedValue);
}
else {
this.adapter.log.debug('[' + this.stateName + '] canceling target state change event - covering is working');
}
};
return TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition;
}(TIoBrokerInOutFunction_State));
var inOutFactory = {
"ioBroker.State": function (adapter, parameters) {
if (typeof parameters !== "string")
return undefined;
var stateName = parameters;
return new TIoBrokerInOutFunction_State(adapter, stateName);
},
"ioBroker.State.Defered": function (adapter, parameters) {
if (typeof parameters !== "string")
return undefined;
var stateName = parameters;
return new TIoBrokerInOutFunction_State(adapter, stateName, 250);
},
"ioBroker.State.OnlyACK": function (adapter, parameters) {
if (typeof parameters !== "string")
return undefined;
var stateName = parameters;
return new TIoBrokerInOutFunction_State_OnlyACK(adapter, stateName);
},
"ioBroker.homematic.WindowCovering.TargetPosition": function (adapter, parameters) {
var p;
if (typeof parameters === 'string')
p = [parameters];
else if (parameters instanceof Array)
p = parameters;
else
p = [];
if (p.length == 0)
return undefined;
var stateName = p[0];
var workingItemName;
if (p.length >= 2)
workingItemName = p[1];
else {
var pathNames = stateName.split('.');
pathNames[pathNames.length - 1] = 'WORKING';
workingItemName = pathNames.join('.');
}
return new TIoBrokerInOutFunction_HomematicWindowCovering_TargetPosition(adapter, stateName, workingItemName);
},
"const": function (adapter, parameters) {
return {
toIOBroker: function (ioValue, callback) {
console.log('inoutFunc: const.toIOBroker: ', parameters);
callback();
},
fromIOBroker: function (callback) {
console.log('inoutFunc: const.fromIOBroker: ', parameters);
callback(null, parameters);
},
subscriptionRequests: []
};
}
};
var conversionFactory = {
"passthrough": function (adapter, parameters) {
return {
toHomeKit: function (value) { return value; },
toIOBroker: function (value) { return value; }
};
},
"level255": function (adapter, parameters) {
return {
toHomeKit: function (value) {
var newValue = Math.round((value / 255.0) * 100.0, 0);
adapter.log.debug('level255: converting value to homekit: ' + value + ' to ' + newValue);
return newValue;
},
toIOBroker: function (value) {
var newValue = Math.round((value / 100.0) * 255.0, 0);
adapter.log.debug('level255: converting value to ioBroker: ' + value + ' to ' + newValue);
return newValue;
}
};
},
"hue": function (adapter, parameters) {
return {
toHomeKit: function (value) {
var newValue = Math.round((value / 65535.0) * 360.0, 0);
adapter.log.debug('hue: converting value to homekit: ' + value + ' to ' + newValue);
return newValue;
},
toIOBroker: function (value) {
var newValue = Math.round((value / 360.0) * 65535.0, 0);
adapter.log.debug('hue: converting value to ioBroker: ' + value + ' to ' + newValue);
return newValue;
}
};
},
"HomematicDirectionToHomekitPositionState": function (adapter, parameters) {
return {
toHomeKit: function (value) {
var num = undefined;
if (typeof value !== 'number')
num = parseInt(value);
else
num = value;
var result = undefined;
switch (num) {
case 0:
result = yahka_homekit_bridge_1.HAPCharacteristic.PositionState.STOPPED;
break;
case 1:
result = yahka_homekit_bridge_1.HAPCharacteristic.PositionState.INCREASING;
break;
case 2:
result = yahka_homekit_bridge_1.HAPCharacteristic.PositionState.DECREASING;
break;
default:
result = yahka_homekit_bridge_1.HAPCharacteristic.PositionState.STOPPED;
break;
}
adapter.log.debug('HomematicDirectionToHomekitPositionState.toHomeKit, from ' + JSON.stringify(value) + '[' + (typeof value) + '] to ' + JSON.stringify(result));
return result;
},
toIOBroker: function (value) {
var num = undefined;
if (typeof value !== 'number')
num = parseInt(value);
else
num = value;
var result = undefined;
switch (num) {
case yahka_homekit_bridge_1.HAPCharacteristic.PositionState.STOPPED:
result = 0;
break;
case yahka_homekit_bridge_1.HAPCharacteristic.PositionState.INCREASING:
result = 1;
break;
case yahka_homekit_bridge_1.HAPCharacteristic.PositionState.DECREASING:
result = 2;
break;
default:
result = 0;
break;
}
adapter.log.debug('HomematicDirectionToHomekitPositionState.toIOBroker, from ' + JSON.stringify(value) + '[' + (typeof value) + '] to ' + JSON.stringify(result));
return result;
}
};
},
"HomematicControlModeToHomekitHeathingCoolingState": function (adapter, parameters) {
return {
toHomeKit: function (value) {
var num = undefined;
if (typeof value !== 'number')
num = parseInt(value);
else
num = value;
var result = undefined;
switch (num) {
case 0:
result = yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.AUTO;
break;
case 1:
result = yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.HEAT;
break;
case 2:
result = yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.HEAT;
break;
case 3:
result = yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.HEAT;
break;
default:
result = yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.OFF;
break;
}
adapter.log.debug('HomematicDirectionToHomekitHeatingCoolingState.toHomeKit, from ' + JSON.stringify(value) + '[' + (typeof value) + '] to ' + JSON.stringify(result));
return result;
},
toIOBroker: function (value) {
var num = undefined;
if (typeof value !== 'number')
num = parseInt(value);
else
num = value;
var result = undefined;
switch (num) {
case yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.OFF:
result = 0;
break;
case yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.HEAT:
result = 1;
break;
case yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.COOL:
result = 0;
break;
case yahka_homekit_bridge_1.HAPCharacteristic.TargetHeatingCoolingState.AUTO:
result = 0;
break;
default:
result = 0;
break;
}
adapter.log.debug('HomematicDirectionToHomekitHeatingCoolingState.toIOBroker, from ' + JSON.stringify(value) + '[' + (typeof value) + '] to ' + JSON.stringify(result));
return result;
}
};
}
};
exports.functionFactory = {
createInOutFunction: function (adapter, inOutFunction, inOutParameters) {
if (!(inOutFunction in inOutFactory))
return inOutFactory["const"](adapter, inOutParameters);
return inOutFactory[inOutFunction](adapter, inOutParameters);
},
createConversionFunction: function (adapter, conversionFunction, conversionParameters) {
if (!(conversionFunction in conversionFactory))
return conversionFactory["passthrough"](adapter, conversionParameters);
return conversionFactory[conversionFunction](adapter, conversionParameters);
}
};
//# sourceMappingURL=yahka.function-factory.js.map