-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·121 lines (108 loc) · 3.66 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
var Service, Characteristic;
var broadlink = require('broadlinkjs-sm');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-broadlink-sp", "broadlinkSP", broadlinkSP);
}
function broadlinkSP(log, config, api) {
this.log = log;
this.ip = config['ip'];
this.name = config['name'];
this.mac = config['mac'];
this.powered = false;
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, '1.0')
}
broadlinkSP.prototype.getState = function(callback) {
var self = this
var b = new broadlink();
b.discover();
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;
return callback(null, false);
} else {
self.powered = true;
return callback(null, true);
}
});
} else {
dev.exit();
}
});
}
broadlinkSP.prototype.setState = function(state, callback) {
var self = this
var b = new broadlink();
b.discover();
self.log("set SP state: " + state);
if (state) {
if (this.powered) {
return callback(null, true)
} else {
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
self.log("ON!");
dev.set_power(true);
dev.exit();
this.powered = true;
return callback(null);
} else {
dev.exit();
}
});
}
} else {
if (this.powered) {
b.on("deviceReady", (dev) => {
if (self.mac_buff(self.mac).equals(dev.mac) || dev.host.address == self.ip) {
self.log("OFF!");
dev.set_power(false);
dev.exit();
self.powered = false;
return callback(null);
} else {
dev.exit();
}
});
} else {
return callback(null, false)
}
}
}
broadlinkSP.prototype.getServices = function() {
return [
this.service,
this.accessoryInformationService
]
}