-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·170 lines (130 loc) · 4.51 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
var Service, Characteristic;
var broadlink = require('./broadlinkjs');
const { version } = require("./package.json");
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-kb-broadlink-sp", "kb_broadlinkSP", kb_broadlinkSP);
}
function kb_broadlinkSP(log, config, api) {
this.ip = config['ip'];
this.name = config['name'];
this.mac = config['mac'];
this.serialNumber = config.serialNumber || '';
this.timeoutStep = 300;
if(config['timeout']){
this.stepsCount = Math.round(config['timeout']/this.timeoutStep);
}else{
this.stepsCount = 5;
}
this.powered = false;
this.log = log;
this.updateTimeout = config.updateTimeout || 30000;
if (!this.ip && !this.mac) throw new Error("You must provide a config value for 'ip' or 'mac'.");
// MAC string to MAC buffer
this.mac_buff = function(mac) {
var mb = new Buffer(6);
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);
}
} else {
//this.log("MAC address emtpy, using IP: " + this.ip);
}
return mb;
}
this.service = new Service.Switch(this.name);
this.service.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this))
;
this.accessoryInformationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Broadlink')
.setCharacteristic(Characteristic.Model, 'SP')
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber)
.setCharacteristic(Characteristic.FirmwareRevision, version)
;
}
kb_broadlinkSP.prototype.getState = function(callback) {
var self = this
var real_result = -1;
var step = 0;
var callbackStep = function(){
setTimeout(function(){
if(-1 != real_result){
self.log("real_result get SP state: " + real_result, ", step:", step);
callback(null, real_result);
}else if(step < self.stepsCount){
step++;
callbackStep();
}else{
self.log("old_result get SP state: " + self.powered, ", step:", step);
callback(null, self.powered);
}
}, self.timeoutStep);
};
callbackStep();
var b = new broadlink();
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
dev.check_power();
dev.on("power", (pwr) => {
self.log("power is on - " + pwr);
dev.exit();
if (!pwr) {
self.powered = false;
} else {
self.powered = true;
}
real_result = !!self.powered;
});
} else {
dev.exit();
}
});
b.discover(self.ip, self.log);
};
kb_broadlinkSP.prototype.setState = function(state, callback) {
var self = this
var real_result = -1;
var step = 0;
var callbackStep = function(){
setTimeout(function(){
if(-1 != real_result){
self.log("real_result set SP state, step:", step);
callback(null);
}else if(step < self.stepsCount){
step++;
callbackStep();
}else{
self.log("err_result set SP state, step:", step);
callback('error');
}
}, self.timeoutStep);
};
callbackStep();
var b = new broadlink();
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
dev.set_power(state);
dev.exit();
self.powered = state;
real_result = true;
} else {
dev.exit();
}
});
b.discover(self.ip, self.log);
};
kb_broadlinkSP.prototype.getServices = function() {
var that = this;
return [
this.service,
this.accessoryInformationService
]
};