-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeMaticDevice.js
56 lines (45 loc) · 1.36 KB
/
HomeMaticDevice.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
const EventEmitter = require('events').EventEmitter
class HomeMaticDevice extends EventEmitter {
constructor (type, address, version, client) {
super()
this.type = type
this.address = address
this.version = version
this.client = client
this.characteristics = []
this.values = {}
this.client.methodCall('getParamset', [this.address, 'VALUES'], (err, res) => {
if (err) return console.log(err)
for (var characteristic in res) {
this.updateValue(characteristic, res[characteristic])
}
this.emit('ready')
})
}
getValue (characteristic) {
return this.values[characteristic]
}
setValue (characteristic, value, callback) {
console.log('setting value', characteristic, value)
if (typeof value === 'number') {
value = { explicitDouble: value.toFixed(1) }
}
this.client.methodCall('setValue', [this.address, characteristic, value], (err) => callback(err))
}
updateValue (characteristic, value) {
if (this.values[characteristic] !== value) {
this.values[characteristic] = value
return true
}
return false
}
applyUpdate (characteristic, value) {
if (this.updateValue(characteristic, value)) {
this.emit('update', characteristic, value)
}
}
getCharacteristics () {
return Object.keys(this.values)
}
}
module.exports = HomeMaticDevice