forked from thkl/homebridge-homematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeMaticRPCTestDriver.js
executable file
·157 lines (129 loc) · 5.18 KB
/
HomeMaticRPCTestDriver.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
'use strict'
var isInTest = typeof global.it === 'function'
var HomeMaticRPCTestDriver = function (log, ccuip, port, system, platform) {
this.log = log
this.system = system
this.ccuip = ccuip
this.platform = platform
this.interface = 'BidCos-RF.'
}
HomeMaticRPCTestDriver.prototype.init = function () {
if (!isInTest) {
this.log.warn('Rega Dummy Class for Tests only it looks like i am running in production mode.')
}
}
HomeMaticRPCTestDriver.prototype.getIPAddress = function () {
return '0.0.0.0'
}
HomeMaticRPCTestDriver.prototype.getValue = function (channel, datapoint, callback) {
if (this.platform.homebridge !== undefined) {
var adrchannel = channel + '.' + datapoint
if (channel.indexOf(this.interface) === -1) {
adrchannel = this.interface + channel + '.' + datapoint
}
let result = this.platform.homebridge.values[adrchannel]
this.log.debug('RPC Query %s (%s)', adrchannel, result)
callback(result)
} else {
let result = 0
callback(result)
}
}
HomeMaticRPCTestDriver.prototype.setValue = function (channel, datapoint, value) {
var adrchannel = channel + '.' + datapoint
if (channel.indexOf(this.interface) === -1) {
adrchannel = this.interface + channel + '.' + datapoint
}
if (typeof value === 'object') {
value = value['explicitDouble']
}
this.log.debug('RPC Set %s (%s)', adrchannel, value)
this.platform.homebridge.values[adrchannel] = value
}
HomeMaticRPCTestDriver.prototype.connect = function () {
}
HomeMaticRPCTestDriver.prototype.ccuWatchDog = function () {
}
HomeMaticRPCTestDriver.prototype.stop = function () {
}
HomeMaticRPCTestDriver.prototype.event = function (params, callback) {
let that = this
this.log.debug('rpc <- event on %s', this.interface)
this.lastMessage = Math.floor((new Date()).getTime() / 1000)
var channel = this.interface + params[1]
var datapoint = params[2]
var value = params[3]
let address = this.interface + params[1] + '.' + params[2]
if (typeof value === 'object') {
value = value['explicitDouble']
}
this.log.debug('RPC Event %s (%s)', address, value)
this.platform.cache.doCache(address, value)
this.platform.homebridge.values[address] = value
this.log.debug('Ok here is the Event' + JSON.stringify(params))
this.log.debug('RPC single event for %s.%s with value %s', channel, datapoint, value)
this.platform.foundAccessories.map(function (accessory) {
if ((accessory.adress === channel) || ((accessory.cadress !== undefined) && (accessory.cadress === channel))) {
that.log.debug('found accessory %s', accessory.adress)
accessory.event(channel, datapoint, value)
}
})
this.platform.eventAdresses.map(function (tuple) {
that.log.debug('check %s vs %s', address, tuple.address)
if (address === tuple.address) {
that.log.debug('found jump into')
tuple.accessory.event(channel, datapoint, value, tuple.function)
}
})
if (callback !== undefined) {
callback(null, [])
}
}
HomeMaticRPCTestDriver.prototype.multicall = function (events, callback) {
this.log.debug('rpc <- system.multicall on %s', this.interface)
let that = this
let params = []
params.map(function (events) {
try {
events.map(function (event) {
if ((event['methodName'] === 'event') && (event['params'] !== undefined)) {
var params = event['params']
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.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)
}
})
}
})
} catch (err) {}
})
if (callback !== undefined) {
callback(null, [])
}
}
HomeMaticRPCTestDriver.prototype.isPortTaken = function (port, fn) {
return false
}
module.exports = {
HomeMaticRPCTestDriver: HomeMaticRPCTestDriver
}