-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
198 lines (165 loc) · 5.87 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
var request = require("request");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-ac-over-http", "AirConditioner", AirConditioner);
}
function AirConditioner(log, config) {
this.log = log;
this.name = config["name"];
this.url = config["server_url"];
// Keep a record of the current temperature around. This is not a source of
// truth, but just a hint for calculating if the system is heating or cooling.
this.temperaturehint = 24;
this.service = new Service.Thermostat(this.name);
this.log("URL is set to: " + this.url);
this.service
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentState.bind(this));
this.service
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', this.getTargetState.bind(this))
.on('set', this.setTargetState.bind(this));
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this));
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTemperature.bind(this))
.on('set', this.setTemperature.bind(this));
this.service
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', this.getTemperatureDisplayUnits.bind(this));
}
AirConditioner.prototype.getCurrentState = function(callback) {
request.get({
url: this.url + "/status"
}, function(err, reponse, body) {
if (!err && reponse.statusCode == 200) {
var json = JSON.parse(body);
var active = json.on;
var temperature = json.temp;
this.temperaturehint = temperature;
if (active === true) {
// It's always running in auto, so if temp is >= 27, say it's heating.
// otherwise, say it's cooling.
if (temperature >= 27) {
this.log("Current state: Heating");
callback(null, Characteristic.CurrentHeatingCoolingState.HEAT)
} else {
this.log("Current state: Cooling");
callback(null, Characteristic.CurrentHeatingCoolingState.COOL)
}
} else {
this.log("Current state: Off");
callback(null, Characteristic.CurrentHeatingCoolingState.OFF)
}
} else {
this.log("Error getting current state");
callback(err);
}
}.bind(this));
}
AirConditioner.prototype.getTargetState = function(callback) {
request.get({
url: this.url + "/status"
}, function(err, reponse, body) {
if (!err && reponse.statusCode == 200) {
var json = JSON.parse(body);
var active = json.on;
if (active === true) {
this.log("Target state: Auto");
callback(null, Characteristic.TargetHeatingCoolingState.AUTO)
} else {
this.log("Target state: Off");
callback(null, Characteristic.TargetHeatingCoolingState.OFF)
}
} else {
this.log("Error getting target state");
callback(err);
}
}.bind(this));
}
AirConditioner.prototype.setTargetState = function(state, callback) {
var url = this.url;
if (state === Characteristic.TargetHeatingCoolingState.OFF) {
url += "/off"
} else { // for all other states (HEAT, COOL and AUTO), just use AUTO
url += "/on"
}
request.get({
url: url
}, function(err, reponse, body) {
if (!err && reponse.statusCode == 200) {
// Success, so update current state. Can't set current state to auto,
// so use the temperature hint to work out it heating or cooling
var currentState;
if (state === Characteristic.TargetHeatingCoolingState.OFF) {
this.service
.setCharacteristic(Characteristic.CurrentHeatingCoolingState, Characteristic.CurrentHeatingCoolingState.OFF);
} else {
this.log(this.temperaturehint);
if (this.temperaturehint >= 27) {
this.service
.setCharacteristic(Characteristic.CurrentHeatingCoolingState, Characteristic.CurrentHeatingCoolingState.HEAT);
} else {
this.service
.setCharacteristic(Characteristic.CurrentHeatingCoolingState, Characteristic.CurrentHeatingCoolingState.COOL);
}
}
callback(null);
} else {
this.log("Error setting target state: " + err);
callback(err);
}
}.bind(this));
}
AirConditioner.prototype.getTemperature = function(callback) {
request.get({
url: this.url + "/status"
}, function(err, reponse, body) {
if (!err && reponse.statusCode == 200) {
var json = JSON.parse(body);
var temperature = json.temp;
this.temperaturehint = temperature;
this.log("Current temperature: " + temperature);
callback(null, parseInt(temperature));
} else {
this.log("Error getting tempterature");
callback(err);
}
}.bind(this));
}
AirConditioner.prototype.setTemperature = function(temperature, callback) {
this.log("Set temperature to " + temperature);
// Ensure the temperature is in the correct range, otherwise the web server
// will throw 404s
if (temperature > 30) {
temperature = 30;
} else if (temperature < 17) {
temperature = 17;
}
this.temperaturehint = temperature;
var url = this.url + "/" + temperature;
request.get({
url: url
}, function(err, reponse, body) {
if (!err && reponse.statusCode == 200) {
// Success, so update current temperature
this.service
.setCharacteristic(Characteristic.CurrentTemperature, temperature);
callback(null);
} else {
this.log("Error setting temperature: " + err);
callback(err);
}
}.bind(this));
}
AirConditioner.prototype.getTemperatureDisplayUnits = function(callback) {
// Always celsius, because we're not animals
callback(null, Characteristic.TemperatureDisplayUnits.CELSIUS);
}
AirConditioner.prototype.getServices = function() {
return [this.service];
}