-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·99 lines (65 loc) · 2.91 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
"use strict"
let Service, Characteristic, HomebridgeAPI
const packageJson = require("./package.json")
const storage = require('node-persist')
module.exports = function(homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
HomebridgeAPI = homebridge
homebridge.registerAccessory("homebridge-dummy-valve", "DummyValve", DummyValve)
}
function DummyValve(log, config) {
this.log = log
this.name = config.name || "DummyValveSwitch"
this.manufacturer = packageJson.author
this.serial = packageJson.version + "-" + packageJson.author
this.model = packageJson.displayName
this.firmware = packageJson.version
this.storage = storage
this.cacheDirectory = HomebridgeAPI.user.persistPath()
this.service = new Service.Valve(this.name)
this.storage.initSync({dir:this.cacheDirectory, forgiveParseErrors: true})
let cachedActive = this.storage.getItemSync(this.name)
if((cachedActive === undefined) || (cachedActive === false) || (cachedActive === 0)) {
this.service.getCharacteristic(Characteristic.Active).updateValue(0)
this.service.getCharacteristic(Characteristic.InUse).updateValue(0)
this.storage.setItemSync(this.name, 0)
} else {
this.service.getCharacteristic(Characteristic.Active).updateValue(1)
this.service.getCharacteristic(Characteristic.InUse).updateValue(1)
}
}
DummyValve.prototype = {
getServices: function () {
this.informationService = new Service.AccessoryInformation()
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
.setCharacteristic(Characteristic.ValveType, 0)
this.service.getCharacteristic(Characteristic.Active)
.onSet(async (value) => {
this.log("set 'Active' to => " + (value == 0 ? "Off" : "On"))
this.storage.setItemSync(this.name, value)
if(value == 0) {
this.service.getCharacteristic(Characteristic.InUse).updateValue(value)
setTimeout(() => {
this.service.getCharacteristic(Characteristic.Active).updateValue(value)
}, 50)
} else {
this.service.getCharacteristic(Characteristic.Active).updateValue(value)
setTimeout(() => {
this.service.getCharacteristic(Characteristic.InUse).updateValue(value)
}, 50)
}
})
.onGet(async () => {
let cachedActive = this.storage.getItemSync(this.name)
this.service.getCharacteristic(Characteristic.Active).updateValue(cachedActive)
this.service.getCharacteristic(Characteristic.InUse).updateValue(cachedActive)
return cachedActive
})
return [this.informationService, this.service]
}
}