-
Notifications
You must be signed in to change notification settings - Fork 50
/
HomeMaticRPC.js
executable file
·339 lines (289 loc) · 11.6 KB
/
HomeMaticRPC.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
'use strict'
const xmlrpc = require('homematic-xmlrpc')
const semver = require('semver')
var HomeMaticRPC = function (log, ccuip, port, system, platform) {
this.log = log
this.system = system
this.ccuip = ccuip
this.platform = platform
this.server = undefined
this.client = undefined
this.stopping = false
this.localIP = undefined
this.bindIP = undefined
this.listeningPort = port
this.lastMessage = 0
this.watchDogTimer = undefined
this.rpc = undefined
this.rpcInit = undefined
this.pathname = '/'
this.watchDogTimeout = 0
if (platform.config['watchdog'] !== undefined) {
this.watchDogTimeout = platform.config['watchdog']
}
if (semver.lt(process.version, '4.5.0')) {
this.log.warn('[RPC] you are running an outdated node version. for now it may work but please update.')
}
switch (system) {
case 0:
this.interface = 'BidCos-RF.'
this.ccuport = 2001
// if (semver.lt(process.version, '4.5.0')) {
this.log.info('[RPC] using xmprpc for communication with BidCos-RF')
this.rpc = xmlrpc
this.rpcInit = 'http://'
// } else {
// this.log.info('using binrpc for communication with BidCos-RF')
// this.rpc = binrpc
// this.rpcInit = 'xmlrpc_bin://'
// }
break
case 1:
this.interface = 'BidCos-Wired.'
// if (semver.lt(process.version, '4.5.0')) {
this.log.info('[RPC] using xmprpc for communication with BidCos-Wired')
this.rpc = xmlrpc
this.rpcInit = 'http://'
// } else {
// this.rpc = binrpc
// this.rpcInit = 'xmlrpc_bin://'
// }
this.ccuport = 2000
break
case 2:
this.interface = 'HmIP-RF.'
this.rpc = xmlrpc
this.ccuport = 2010
this.rpcInit = 'http://'
break
case 3:
this.interface = 'VirtualDevices.'
this.rpc = xmlrpc
this.ccuport = 9292
this.pathname = '/groups'
this.rpcInit = 'http://'
break
}
this.log.info('init RPC for %s', this.interface)
}
HomeMaticRPC.prototype.init = function () {
var that = this
var bindIP = this.platform.config.bind_ip
if (bindIP === undefined) {
bindIP = this.getIPAddress()
if (bindIP === '0.0.0.0') {
that.log('[RPC] Can not fetch IP')
return
}
}
var ip = this.platform.config.local_ip
if (ip === undefined) {
ip = bindIP
}
this.localIP = ip
this.bindIP = bindIP
this.log.info('[RPC] local ip used : %s. you may change that with local_ip parameter in config', ip)
this.isPortTaken(this.listeningPort, function (error, inUse) {
if (error === null) {
if (inUse === false) {
that.server = that.rpc.createServer({
host: that.bindIP,
port: that.listeningPort
})
that.server.on('[RPC] NotFound', function (method, params) {
// that.log.debug("Method %s does not exist. - %s",method, JSON.stringify(params));
})
that.server.on('system.listMethods', function (err, params, callback) {
that.log.debug("[RPC] Method call params for 'system.listMethods': %s (%s)", JSON.stringify(params), err)
callback(null, ['event', 'system.listMethods', 'system.multicall'])
})
that.server.on('listDevices', function (err, params, callback) {
that.log.debug('[RPC] <- listDevices on %s - Zero Reply (%s)', that.interface, err)
callback(null, [])
})
that.server.on('newDevices', function (err, params, callback) {
that.log.debug('[RPC] <- newDevices on %s nobody is interested in newdevices ... (%s)', that.interface, err)
// we are not intrested in new devices cause we will fetch them at launch
callback(null, [])
})
that.server.on('event', function (err, params, callback) {
if (!err) {
that.handleEvent('event', params)
}
callback(err, [])
})
that.server.on('system.multicall', function (err, params, callback) {
that.log.debug('[RPC] <- system.multicall on %s (%s)', that.interface, err)
that.lastMessage = Math.floor((new Date()).getTime() / 1000)
params.map(function (events) {
try {
events.map(function (event) {
that.handleEvent(event['methodName'], event['params'])
})
} catch (err) {}
})
callback(null)
})
that.log.info('[RPC] server for interface %s is listening on port %s.', that.interface, that.listeningPort)
that.connect()
} else {
that.log.error('****************************************************************************************************************************')
that.log.error('* Sorry the local port %s on your system is in use. Please make sure, that no other instance of this plugin is running.', that.listeningPort)
that.log.error('* you may change the initial port with the config setting for local_port in your config.json ')
that.log.error('* giving up ... the homematic plugin is not able to listen for ccu events on %s until you fix this. ', that.interface)
that.log.error('****************************************************************************************************************************')
}
} else {
that.log.error('* Error while checking ports')
}
})
}
HomeMaticRPC.prototype.handleEvent = function (method, params) {
let that = this
if ((method === 'event') && (params !== undefined)) {
var channel = that.interface + params[1]
var datapoint = params[2]
var value = params[3]
let address = that.interface + params[1] + '.' + params[2]
that.log.debug('[RPC] event for %s.%s with value %s', channel, datapoint, value)
that.platform.cache.doCache(address, value)
that.platform.foundAccessories.map(function (accessory) {
var deviceAdress = channel.slice(0, channel.indexOf(':'))
if (accessory.adress === channel) {
that.log.debug('[RPC] Accessory (%s) found by channeladress (%s) -> Send Event with value %s', accessory.name, channel, value)
accessory.event(channel, datapoint, value)
} else
if ((accessory.cadress !== undefined) && (accessory.cadress === channel)) {
that.log.debug('[RPC] Accessory (%s) found by accessory.cadress %s matches channel %s -> Send Event with value %s', accessory.name, accessory.cadress, channel, value)
accessory.event(channel, datapoint, value)
} else
if ((accessory.deviceAdress !== undefined) && (accessory.deviceAdress === deviceAdress) && (accessory.isMultiChannel === true)) {
that.log.debug('[RPC] Accessory (%s) found -> by deviceadress %s matches %s Send Event with value %s', accessory.name, accessory.deviceAdress, deviceAdress, value)
accessory.event(channel, datapoint, value)
}
})
that.platform.eventAdresses.map(function (tuple) {
if (address === tuple.address) {
tuple.accessory.event(channel, datapoint, value, tuple.function)
}
})
}
}
HomeMaticRPC.prototype.getIPAddress = function () {
var interfaces = require('os').networkInterfaces()
for (var devName in interfaces) {
var iface = interfaces[devName]
for (var i = 0; i < iface.length; i++) {
var alias = iface[i]
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal && (alias.address.indexOf('169.254.') === -1)) {
return alias.address
}
}
}
return '0.0.0.0'
}
HomeMaticRPC.prototype.getValue = function (channel, datapoint, callback) {
var that = this
if (this.client === undefined) {
this.log.debug('Returning cause client is invalid')
return
}
if (channel.indexOf(that.interface) > -1) {
channel = channel.substr(that.interface.length)
this.log.debug('[RPC] getValue Call for %s %s', channel, datapoint)
this.client.methodCall('getValue', [channel, datapoint], function (error, value) {
that.log.debug('[RPC] getValue (%s %s) Response %s | Errors: %s', channel, datapoint, JSON.stringify(value), error)
callback(value)
})
} else {}
}
HomeMaticRPC.prototype.setValue = function (channel, datapoint, value, callback) {
var that = this
this.log.debug('[RPC] setValue %s %s %s', channel, datapoint, value)
if (this.client === undefined) {
this.log.error('client missing')
return
}
if (channel.indexOf(that.interface) > -1) {
channel = channel.substr(that.interface.length)
}
// if (that.interface != "HmIP-RF.") {
// value = String(value);
// }
this.log.debug('[RPC] setValue Call for %s %s Value %s Type %s', channel, datapoint, value, typeof value)
this.client.methodCall('setValue', [channel, datapoint, value], function (error, value) {
that.log.debug('[RPC] setValue (%s %s) Response %s Errors: %s', channel, datapoint, JSON.stringify(value), error)
if ((value !== undefined) && (value['faultCode'] !== undefined) && (callback !== undefined)) {
callback(value['faultCode'], value)
} else
if (callback !== undefined) {
callback(error, value)
}
})
}
HomeMaticRPC.prototype.connect = function () {
var that = this
this.lastMessage = Math.floor((new Date()).getTime() / 1000)
var port = this.ccuport
this.log.info('[RPC] Creating Local HTTP Client for CCU RPC Events')
this.client = that.rpc.createClient({
host: this.ccuip,
port: port,
path: this.pathname,
queueMaxLength: 100
})
this.log.debug('[RPC] CCU RPC Init Call on port %s for interface %s', port, this.interface)
var command = this.rpcInit + this.localIP + ':' + this.listeningPort
this.client.methodCall('init', [command, 'homebridge_' + this.interface], function (error, value) {
that.log.debug('[RPC] CCU Response for init at %s with %s...Value (%s) Error : (%s)', that.interface, command, JSON.stringify(value), error)
that.lastMessage = Math.floor((new Date()).getTime() / 1000)
})
if (this.watchDogTimeout > 0) {
this.ccuWatchDog()
}
}
HomeMaticRPC.prototype.ccuWatchDog = function () {
var that = this
if (this.lastMessage !== undefined) {
var now = Math.floor((new Date()).getTime() / 1000)
var timeDiff = now - this.lastMessage
if (timeDiff > that.watchDogTimeout) {
that.log.debug('[RPC] Watchdog Trigger - Reinit Connection for %s after idle time of %s seconds', this.interface, timeDiff)
that.lastMessage = now
that.client.methodCall('init', [this.rpcInit + this.localIP + ':' + this.listeningPort, 'homebridge_' + this.interface], function (error, value) {
that.log.debug('[RPC] CCU Response ...Value (%s) Error : (%s)', JSON.stringify(value), error)
that.lastMessage = Math.floor((new Date()).getTime() / 1000)
})
}
}
var recall = function () {
that.ccuWatchDog()
}
this.watchDogTimer = setTimeout(recall, 10000)
}
HomeMaticRPC.prototype.stop = function () {
this.log.info('[RPC] Removing Event Server for Interface %s', this.interface)
this.client.methodCall('init', ['xmlrpc_bin://' + this.localIP + ':' + this.listeningPort], function (error, value) {
if (error !== undefined) {
this.log.error('[RPC] Error while removing eventserver %s', error)
}
})
}
// checks if the port is in use
// https://gist.github.com/timoxley/1689041
HomeMaticRPC.prototype.isPortTaken = function (port, fn) {
var net = require('net')
var tester = net.createServer().once('error', function (err) {
if (err.code !== 'EADDRINUSE') return fn(err)
fn(null, true)
})
.once('listening', function () {
tester.once('close', function () {
fn(null, false)
})
.close()
}).listen(port)
}
module.exports = {
HomeMaticRPC: HomeMaticRPC
}