-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
323 lines (261 loc) · 10.5 KB
/
index.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
'use strict';
const path = require('path');
var Service, Characteristic, Hap, PlatformAccessory
const API = require('freeathome-api');
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Hap = homebridge.hap;
PlatformAccessory = homebridge.platformAccessory;
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform("homebridge-freeathome", "free@home", BuschJaegerApPlatform);
homebridge.registerPlatform("homebridge-freeathome", "BuschJaegerSysAp", BuschJaegerApPlatform); // deprecated
}
// Platform constructor
// config may be null
// api may be null if launched from old homebridge version
function BuschJaegerApPlatform(log, config, api) {
this.log = log;
this.hap = Hap;
this.hApi = api;
this.platformAccessory = PlatformAccessory;
this.log('Initialising BuschJaeger Plugin');
this.mappings = config.mappings;
this.log('Will try to connect to the SysAP at %s', config.sysIP);
this.configuration = new API.ClientConfiguration(config.sysIP, config.username, config.password);
this.accessoryCallback = null;
this.accessoryCallbackSet = false;
this.actuatorInfo = {};
this.scheduler = null;
this.connect();
this.foundAccessories = [];
this.config = config;
}
BuschJaegerApPlatform.prototype.accessories = function(callback) {
this.accessoryCallback = callback;
this.accessoryCallbackSet = true;
}
BuschJaegerApPlatform.prototype.transformAccessories = function(actuators) {
let acc = [];
for (let serial in actuators) {
let mapping = {};
if (this.mappings && serial in this.mappings) {
mapping = this.mappings[serial];
}
let actuator = actuators[serial];
if ('blacklist' in mapping && mapping['blacklist'].includes('*')) {
this.log('Ignoring blacklisted accessory ' + actuator['typeName'] + ' with serial ' + serial);
continue;
}
if (Object.keys(actuator['channels']).length > 0) {
for (let channelNo in actuator['channels']) {
let channel = actuator['channels'][channelNo];
let accessory = this.initializeAccessory(channelNo, actuator, channel, serial, mapping);
if (accessory != null) {
acc.push(accessory);
}
}
// some actuators do not expose their channels
} else {
let accessory = this.initializeAccessory(null, actuator, {}, serial, mapping);
if (accessory != null) {
acc.push(accessory);
}
}
}
this.foundAccessories = acc;
}
BuschJaegerApPlatform.prototype.initializeAccessory = function(channelNo, actuator, channel, serial, mapping) {
let [accessoryClass, forcedChannel] = this.getAccessoryClass(actuator['deviceId'], channelNo, channel['functionId'], channel['iconId'], channel['floor'], channel['room'], mapping);
if (channelNo == null) {
channelNo = forcedChannel;
}
if ('blacklist' in mapping && mapping['blacklist'].includes(channelNo)) {
this.log('Ignoring blacklisted accessory ' + actuator['typeName'] + '/' + channel['displayName'] + ' with serial ' + serial + ' and channel ' + channelNo);
return;
}
if (accessoryClass == null) {
this.log.debug('Ignoring non-supported accessory ' + actuator['typeName'] + '/' + channel['displayName'] + ' with serial ' + serial + ' and channel ' + channelNo);
return;
}
if (channelNo != null) {
let service = require(path.join(__dirname, 'lib', accessoryClass));
return new service(this, Service, Characteristic, actuator, channelNo, mapping);
}
}
BuschJaegerApPlatform.prototype.getAccessoryClass = function(deviceId, channel, functionId, iconId, floor, room, mapping) {
if (!floor || !room) {
return [null, null];
}
switch (deviceId) {
case '1008': // Bewegungsmelder
case '100A': // Bewegungsmelder/Schaltaktor 1-fach
return ['BuschJaegerMotionSensorAccessory', 'ch0000'];
}
if (!iconId) {
return [null, null];
}
if ('doorbell' in mapping && channel in mapping['doorbell']) {
let doorbell = mapping['doorbell'][channel];
if (doorbell['video']) {
return ['BuschJaegerVideoDoorBellAccessory', null];
} else {
return ['BuschJaegerDoorBellAccessory', null];
}
} else if ('garagedoor' in mapping && channel in mapping['garagedoor']) {
return ['BuschJaegerGarageDoorAccessory', null];
}
switch (functionId) {
case '4':
return ['BuschJaegerBinarySensorAccessory', null];
case '7':
return ['BuschJaegerSchaltAktorAccessory', null];
case '9':
case '61':
return ['BuschJaegerJalousieAccessory', null];
case '11':
return ['BuschJaegerMotionSensorAccessory', null];
case '12':
return ['BuschJaegerDimmAktorAccessory', null];
case 'f':
return ['BuschJaegerContactSensorAccessory', null];
case '23':
return ['BuschJaegerThermostatAccessory', null];
case '1a':
return ['BuschJaegerDoorLockAccessory', null];
case '5a':
return ['BuschJaegerMediaPlayerAccessory', null];
case '7d':
return ['BuschJaegerSmokeSensorAccessory', null];
default:
return [null, null];
}
}
BuschJaegerApPlatform.prototype.setDatapoint = function(serial, channelNo, datapoint, value = null) {
if ('debug' in this.config && this.config.debug) {
this.log.warn("setDatapoint(): " + [serial, channelNo, datapoint, value.toString()].join('/'));
}
this.api.setDatapoint(serial, channelNo, datapoint, value.toString());
}
BuschJaegerApPlatform.prototype.connect = async function() {
this.log('Trying to connect to SysAP');
const that = this;
this.api = new API.SystemAccessPoint(this.configuration, this, class {
static log(...messages) {
for (let message of messages) {
that.log.info(message);
}
}
static warn(...messages) {
for (let message of messages) {
that.log.warn(message);
}
}
static error(...messages) {
for (let message of messages) {
that.log.error(message);
}
}
static debug(...messages) {
for (let message of messages) {
that.log.debug(message);
}
}
});
try {
await this.api.connect();
} catch(e) {
this.log.error(e.message);
}
}
BuschJaegerApPlatform.prototype.processMessage = function(jsonData) {
if ('debug' in this.config && this.config.debug) {
this.log.warn("processMessage(): " + JSON.stringify(jsonData));
}
if (!('result' in jsonData)) {
this.log.warn('Invalid message received.');
return;
}
var isUpdate = false;
if ('type' in jsonData && jsonData['type'] == 'update') {
this.processUpdate(jsonData['result']);
isUpdate = true;
} else {
// Full data received
this.actuatorInfo = jsonData['result'];
}
if (this.accessoryCallbackSet && !isUpdate) {
this.transformAccessories(jsonData['result']);
/*
There may be an edge case where the connection to the SysAP Node Plugin
is established successfully but the SysAP Node Plugin can not authenticate against
the SysAP and therefore returns no accessories. This will remove all devices from the
HomeKit database.
*/
if (this.foundAccessories.length >= 1) {
// Only expose if accessory has more than one service (first is informationService)
let filteredAccessories = this.foundAccessories.filter(accessory => accessory.getServices().length > 1);
this.accessoryCallback(filteredAccessories);
this.accessoryCallback = null;
this.accessoryCallbackSet = false;
}
}
}
BuschJaegerApPlatform.prototype.processUpdate = function(actuators) {
for (let serial in actuators) {
for (let channel in actuators[serial]['channels']) {
let channels = actuators[serial]['channels'][channel];
for (let datapoint in channels['datapoints']) {
if (this.actuatorInfo[serial]) {
if (!this.actuatorInfo[serial]['channels'][channel]) {
this.actuatorInfo[serial]['channels'][channel] = {'datapoints': {}};
}
let value = channels['datapoints'][datapoint];
this.actuatorInfo[serial]['channels'][channel]['datapoints'][datapoint] = value;
this.sendUpdateToAccessory(serial, channel.replace('ch',''), datapoint, value);
}
}
}
}
}
BuschJaegerApPlatform.prototype.sendUpdateToAccessory = function(serial, channel, datapoint, value = null) {
let accessory = this.findAccessoryBySerial(serial, channel);
if (accessory) {
accessory.update(channel, datapoint, value);
}
}
BuschJaegerApPlatform.prototype.findAccessoryBySerial = function(sn, ch = null) {
for (let i = 0; i < this.foundAccessories.length; i++) {
if (this.foundAccessories[i].serial == sn) {
if (!ch && !this.foundAccessories[i].channel) {
return this.foundAccessories[i];
}
if (ch && this.foundAccessories[i].channel && this.foundAccessories[i].channel == ch) {
return this.foundAccessories[i];
}
}
}
return null;
}
BuschJaegerApPlatform.prototype.broadcastMessage = function(message) {
switch (message.type) {
case 'error':
this.log.error(message.result.message);
break;
case 'update':
this.processMessage(message);
break;
case 'subscribed':
if (message.result) {
this.processMessage({'type': 'result', 'result': this.api.getDeviceData()});
} else {
this.log.error("Unsubscribed from System Access Point!");
}
break;
default:
this.log.warn("Unknown message type received: " + message.type);
}
}