forked from smka/homebridge-broadlink-platform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
243 lines (198 loc) · 7.65 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
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
var Service, Characteristic;
var broadlink = require('broadlinkjs-dw');
const MP = "MP";
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-broadlink-platform-outlet", "broadlinkPlatformOutlet", broadlinkPlatform);
}
class broadlinkPlatform {
constructor(log, config, api) {
this.log = log;
this.config = config;
if (api) this.api = api;
}
accessories(callback) {
//For each device in cfg, create an accessory!
var accessories = this.config.accessories;
var accessoriesList = [];
var length = accessories ? accessories.length : 0;
for (var i = 0; i < length; i++) {
if (accessories[i].type == MP) {
for (var outlet = 1; outlet <= 4; outlet++) {
accessories[i].index = outlet;
accessories[i].outletName = "S" + outlet;
accessories[i].outletTimer = [];
if (accessories[i].timer)
if (accessories[i].timer[outlet - 1])
accessories[i].outletTimer = accessories[i].timer[outlet - 1];
accessories[i].interval = accessories[i].interval || 0;
accessoriesList.push(new broadlinkAccessory(this.log, accessories[i]));
}
this.log(`${accessories[i].name} - Created`);
} else {
accessoriesList.push(new broadlinkAccessory(this.log, accessories[i]));
this.log(`${accessories[i].name} - Created`);
}
}
callback(accessoriesList);
}
}
class broadlinkAccessory {
constructor(log, config) {
this.log = log;
this.config = config;
this.type = config.type || MP; // Expected value MP or SP (default)
this.outletName = config.outletName || "";
this.name = (config.name || this.type) + (this.type == MP ? ": " : "") + this.outletName;
this.index = config.index || 0;
this.mac = config.mac;
this.timer = config.outletTimer;
this.interval = config.interval < 1000 ? 1000 : config.interval;
// Other value
this.powered = false;
this.firstTime = true;
this.onGetState = false;
this.onSetState = false;
this.device = null;
if (!this.mac) throw new Error("You must provide a config value for 'mac' address.");
// MAC string to MAC buffer
this.mac_buff = function (mac) {
var mb = new Buffer.alloc(6, 0);
if (mac) {
var values = mac.split(':');
if (!values || values.length != 6) {
throw new Error('Invalid MAC [' + mac + ']; should follow pattern ##:##:##:##:##:##');
}
for (var i = 0; i < values.length; ++i) {
var tmpByte = parseInt(values[i], 16);
mb.writeUInt8(tmpByte, i);
}
}
return mb;
};
}
getServices() {
var type = this.config.type;
var services = [];
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Broadlink');
this.switchService = new Service.Outlet(this.name);
this.switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.informationService
.setCharacteristic(Characteristic.Model, type)
.setCharacteristic(Characteristic.SerialNumber, '1.0.5');
services.push(this.switchService, this.informationService);
// Only update once for MP
this.updateState();
return services;
}
discover(broadlink) {
try {
broadlink.discover();
} catch (error) {
this.onGetState = false;
this.onSetState = false;
}
}
// Get device
async _getDevice() {
return new Promise(async (resolve) => {
var device = new broadlink();
var checkAgain;
var timeout;
// Find device
this.discover(device);
checkAgain = setInterval(() => {
this.discover(device);
}, 1000);
timeout = setTimeout(() => {
clearTimeout(timeout);
clearInterval(checkAgain);
device = null;
// return empty device
resolve(null);
}, 5000);
// Device is ready
device.on("deviceReady", (dev) => {
if (this.mac_buff(this.mac).equals(dev.mac)) {
clearTimeout(timeout);
clearInterval(checkAgain);
device = null;
// return the device
resolve(dev);
}
});
});
}
// Initiialized device
async _initDevice() {
this.device = await this._getDevice();
if (!this.device) return;
// When recived power
this.device.on(this.type == MP ? "mp_power" : "power", (statuses) => {
var status = (this.type == MP ? statuses[this.index - 1] : statuses) ? true : false;
if (this.firstTime || this.powered != status) {
if (this.firstTime) this.firstTime = false;
this.powered = status ? true : false;
this.switchService.updateCharacteristic(Characteristic.On, this.powered);
this.logState();
}
});
}
// Get MP/SP outlets status
async getState(callback) {
callback(null, this.powered);
}
// Set MP/SP outlets status
async setState(state, callback) {
if (!this.onSetState) {
this.onSetState = true;
// Initialized device if it get disconnected
if (!this.device) await this._initDevice();
if (this.device) {
// Log state
this.log(`[${this.name}] 👈`);
// Set the power
this.powered = state ? true : false;
if (this.type == MP) this.device.set_power(this.index, this.powered);
else this.device.set_power(this.powered);
this.switchService.updateCharacteristic(Characteristic.On, this.powered);
// Log state
this.logState();
// Send check power command
this.device.check_power();
}
}
this.onSetState = false;
// Callback;
callback(null);
}
// Update outlets status based on the last status
async updateState() {
// Send check power command and loop it
if (this.mainInterval) clearInterval(this.mainInterval);
this.mainInterval = setInterval(async () => {
if (!this.onGetState) {
this.onGetState = true;
// Initialized device if it get disconnected
if (!this.device) await this._initDevice();
// Send check power command
if (this.device) this.device.check_power();
this.onGetState = false;
}
}, this.interval);
}
logState(extra, tail) {
var message = `${extra ? extra + ": " : ""}${this.bulb(this.powered)} [${this.name}] ${tail ? "-> " + tail : ""}`;
if (message != this._message) this.log(message);
this._message = message;
}
bulb(state) {
return state ? "🟡" : "⚪️";
}
}