forked from honcheng/homebridge-rfremote-fan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (180 loc) · 4.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
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
var exec = require("child_process").exec;
var locks = require('locks');
var request = require('request');
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-rfremote-fan', 'RF-Remote Fan', FanAccessory);
};
function FanAccessory(log, config) {
this.log = log;
this.host = config.host;
this.name = config.name;
this.id = config.id;
this.state = {
power: false,
speed: 25,
};
this.mutex = locks.createMutex()
}
FanAccessory.prototype.getRelays = function(value, callback) {
request({
url: 'http://' + this.host + '/rfremotefan/api/v1.0/status',
method: 'GET',
json: true,
body: value,
}, function(error, response, body) {
if (error) {
callback(error);
} else if (response.statusCode == 200) {
callback(null, body);
} else {
callback(new Error('HTTP response ' + response.statusCode + ': ' + JSON.stringify(body)));
}
});
};
FanAccessory.prototype.updateRelays = function(value, callback) {
this.log('update relays: ' + value);
request({
url: 'http://' + this.host + '/rfremotefan/api/v1.0/update',
method: 'POST',
json: true,
body: value,
}, function(error, response, body) {
if (error) {
callback(error);
} else if (response.statusCode == 200) {
callback(null);
} else {
callback(new Error('HTTP response ' + response.statusCode + ': ' + JSON.stringify(body)));
}
});
};
FanAccessory.prototype.getFanState = function(callback) {
info = {"id": this.id}
this.getRelays(info, (error, data) => {
if (error) {
callback(error);
} else {
var state = {}
speed = data["speed"]
if (speed == 3) {
state.power = true;
state.speed = 100;
} else if (speed == 2) {
state.power = true;
state.speed = 50;
} else if (speed == 1) {
state.power = true;
state.speed = 25;
} else {
state.power = false;
state.speed = 25;
}
// state.temperature = data.temperature;
this.log("speed ---" + state.speed)
this.state = state;
callback(null, state);
}
});
};
FanAccessory.prototype.setFanState = function(state, callback) {
var relay;
if (state.power && state.speed > 50) {
relay = 3;
} else if (state.power && state.speed > 25) {
relay = 2;
} else if (state.power && state.speed > 0) {
relay = 1;
} else {
relay = 0;
}
this.log('active relay ' + relay);
var update1 = {
};
update1["id"] = this.id;
update1["speed"] = relay;
var update2 = {
1: false,
2: false,
3: false,
};
if (relay) {
delete update2[relay];
}
this.mutex.timedLock(5000, (error) => {
if (error) {
callback(error);
return;
}
this.updateRelays(update1, (error) => {
if (error) {
this.mutex.unlock();
callback(error);
return;
}
this.updateRelays(update1, (error) => {
this.mutex.unlock();
callback(error);
return;
});
});
});
}
FanAccessory.prototype.identify = function(callback) {
this.log("Identify requested!");
this.updateRelays({4: true}, (error) => {
if (error) {
callback(error);
return;
}
setTimeout(() => {
this.updateRelays({4: false}, callback);
}, 500);
});
};
FanAccessory.prototype.getServices = function() {
this.fanService = new Service.Fan();
this.fanService.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
this.fanService.getCharacteristic(Characteristic.RotationSpeed)
.setProps({
minValue: 0,
maxValue: 100,
minStep: 25,
})
.on('get', this.getSpeed.bind(this))
.on('set', this.setSpeed.bind(this));
return [this.fanService];
};
FanAccessory.prototype.getOn = function(callback) {
this.getFanState(function(error, state) {
callback(null, state && state.power);
});
};
FanAccessory.prototype.setOn = function(value, callback) {
if (this.state.power != value) {
this.log('setting power to ' + value);
this.state.power = value;
this.setFanState(this.state, callback);
} else {
callback(null);
}
};
FanAccessory.prototype.getSpeed = function(callback) {
this.log("get speed");
this.getFanState(function(error, state) {
callback(null, state && state.speed);
});
};
FanAccessory.prototype.setSpeed = function(value, callback) {
if (this.state.speed != value) {
this.log('setting speed to ' + value);
this.state.speed = value;
this.setFanState(this.state, callback);
} else {
callback(null);
}
};