-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
156 lines (139 loc) · 6.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
/*
* Copyright © 2018 Hector Mendoza. All rights reserved.
* Homebridge plugin for D-Link DCH-S220 Wi-Fi enabled Siren.
*
*/
'use strict'
let Service, Characteristic;
let Siren = require('./lib/Siren');
module.exports = (homebridge) => {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-dchs220-siren', 'SirenSwitch', SirenSwitchAccessory)
}
class SirenSwitchAccessory {
constructor(log, config) {
this.log = log
this.config = config || {};
// :: Config parameters
this.name = this.config.name || 'D-Link Siren';
this.pin = this.config.pin || 123456;
this.ipAddress = this.config.ipAddress || '127.0.0.1';
this.sound = (this.config.sound && !isNaN(this.config.sound) && this.config.sound > 0 && this.config.sound < 7) ? this.config.sound : 1;
this.volume = (this.config.volume && !isNaN(this.config.volume) && this.config.volume > 0 && this.config.volume <= 100) ? this.config.volume : 100;
this.duration = (this.config.duration && !isNaN(this.config.duration) && this.config.duration > 0 && this.config.duration <= 88888) ? this.config.duration : 88888;
this.sirenClient = new Siren(this.ipAddress, this.pin);
this.updateInterval = (this.config.updateInterval && !isNaN(this.config.updateInterval) && this.config.updateInterval >= 100) ? this.config.updateInterval : false;
this.service = new Service.Switch(this.config.name)
}
getServices() {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'D-Link')
.setCharacteristic(Characteristic.Model, 'DCH-S220')
.setCharacteristic(Characteristic.SerialNumber, 'dlink-dch-s220-siren')
this.service.getCharacteristic(Characteristic.On)
.on('get', this.getOnCharacteristicHandler.bind(this))
.on('set', this.setOnCharacteristicHandler.bind(this));
// :: Fault status (Home does not seem to support it, but Eve app does)
this.service.addOptionalCharacteristic(Characteristic.StatusFault);
// :: Status polling
if (this.updateInterval) {
setInterval(() => {
this.getStatus().then(status => {
this.service.getCharacteristic(Characteristic.On).setValue(status);
}).catch(err => {
console.log(":: Error from status polling: " + err);
});
}, this.updateInterval);
}
return [informationService, this.service]
}
setOnCharacteristicHandler(value, callback) {
// :: Log In to the Siren
this.sirenClient.login().then(status => {
if (status !== 'success') {
this.log(":: An error occurred while logging in to the siren, please check the credentials in config.");
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
}
// :: Play the Siren
if (value === true) {
this.sirenClient.play(this.sound, this.volume, this.duration).then(status => {
if (status === 'OK') {
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(false);
return callback(null);
}
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
}).catch(err => {
this.log(":: An error occurred while playing the siren: " + err);
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
});
}
// :: Stop Playing the Siren
if (value === false) {
this.sirenClient.stop().then(status => {
if (status === 'OK') {
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(false);
return callback(null);
}
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
}).catch(err => {
this.log(":: An error occurred while stopping the siren: " + err);
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
})
}
}).catch(err => {
this.log(":: An error occurred while logging in to the siren: " + err);
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
});
}
/*
* Get the "playing" status of the siren
*/
getStatus() {
return new Promise((resolve, reject) => {
// :: Log In to the Siren
this.sirenClient.login().then(status => {
if (status !== 'success') {
this.log(":: An error occurred while logging in to the siren, please check the credentials in config.");
return reject("Error while logging in to the siren.");
}
// :: Retrieve playing status
this.sirenClient.getPlayingStatus().then(status => {
return resolve(status);
}).catch(err => {
this.log(":: An error occurred while retrieving siren status: " + err);
return reject(err);
});
}).catch(err => {
this.log(":: An error occurred while logging in to the siren: " + err);
return reject(err);
});
});
}
getOnCharacteristicHandler(callback) {
this.getStatus().then(status => {
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(false);
return callback(null, status);
}).catch(err => {
console.log(":: Error from status polling: " + err);
this.service.getCharacteristic(Characteristic.StatusFault)
.updateValue(true);
return callback(false);
});
}
}