-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
512 lines (466 loc) · 21.3 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"use strict";
var Service, Characteristic, FakeGatoHistoryService;
var temperatureService;
var humidityService;
var got = require("got");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
FakeGatoHistoryService = require("fakegato-history")(homebridge);
homebridge.registerAccessory("homebridge-weather", "Weather", WeatherAccessory);
};
function WeatherAccessory(log, config) {
this.log = log;
// FakeGato
this.fakeGateHistoryService = undefined;
// Load settings from the config
this.name = config["name"];
// Humidity-sensor can have a divergent name
this.nameHumidity = config["nameHumidity"] || config["name"];
this.apikey = config["apikey"];
this.locationByCity = config["location"];
this.locationById = config["locationById"];
this.locationByCoordinates = config["locationByCoordinates"];
this.locationByZip = config["locationByZip"];
if (config["showHumidity"] != null) {
this.showHumidity = config["showHumidity"];
} else {
this.showHumidity = true;
}
if (config["showTemperature"] != null) {
this.showTemperature = config["showTemperature"];
} else {
this.showTemperature = true;
}
if (config["pollingInterval"] != null) {
this.pollingInterval = parseInt(config["pollingInterval"]) * 1000 * 60;
} else {
this.pollingInterval = 0;
}
if (config["enableHistory"] != null) {
this.enableHistory = config["enableHistory"];
} else {
this.enableHistory = false;
}
this.type = config["type"] || "current";
this.unit = config["unit"] || "metric";
this.cachedWeatherObj = undefined;
this.lastupdate = 0;
if (this.enableHistory && this.pollingInterval > 0) {
this.fakeGateHistoryService = new FakeGatoHistoryService("weather", this, 4032, this.pollingInterval);
}
// start periodical polling in background with setTimeout
if (this.pollingInterval > 0) {
this.log("Starting Polling background service for", this.name);
var that = this;
setTimeout(function () {
that.backgroundPolling();
}, this.pollingInterval);
}
}
WeatherAccessory.prototype =
{
backgroundPolling: function () {
this.log.info("Polling data in background");
if (this.showTemperature && (this.type === "current" || this.type === "max" || this.type === "min")) {
// Update Temperature
this.getStateTemp(function (error, temperature) {
if (!error && temperature != null) {
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, temperature);
}
}.bind(this));
}
// Update Humidity if configured
if (this.showHumidity && this.type === "current") {
this.getStateHum(function (error, humidity) {
if (!error && humidity != null) {
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
}
}.bind(this));
}
if (this.type === "clouds") {
this.getStateClouds(function (error, value) {
if (!error && value != null) {
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, value);
}
}.bind(this));
}
if (this.type === "sun") {
this.getStateSun(function (error, value) {
if (!error && value != null) {
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, value);
}
}.bind(this));
}
if (this.type === "windspeed") {
this.getStateWindspeed(function (error, value) {
if (!error && value != null) {
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, value);
}
}.bind(this));
}
var that = this;
setTimeout(function () {
// Recursive call after certain time
that.backgroundPolling();
}, this.pollingInterval);
},
/**
* Get's the temperature either from Cache or from HTTP
* @param callback
*/
getStateTemp: function (callback) {
// Only fetch new data once per minute
if (!this.cachedWeatherObj || this.pollingInterval > 0 || this.lastupdate + 60 < (new Date().getTime() / 1000 | 0)) {
var url = this.makeURL();
this.httpRequest(url, function (error, response, responseBody) {
if (error) {
this.log("HTTP get weather function failed: %s", error.message);
callback(error);
} else {
try {
this.setCacheObj(responseBody);
var temperature = this.returnTempFromCache();
callback(null, temperature);
} catch (error2) {
this.log("Getting Temperature failed: %s", error2, response, responseBody);
callback(error2);
}
}
}.bind(this));
} else {
try {
var temperature = this.returnTempFromCache();
this.log("Returning cached data", temperature);
callback(null, temperature);
} catch (error) {
this.log("Getting Temperature failed: %s", error);
callback(error);
}
}
},
getStateHum: function (callback) {
// Only fetch new data once per minute
if (!this.cachedWeatherObj || this.pollingInterval > 0 || this.lastupdate + 60 < (new Date().getTime() / 1000 | 0)) {
var url = this.makeURL();
this.httpRequest(url, function (error, response, responseBody) {
if (error) {
this.log("HTTP get weather function failed: %s", error.message);
callback(error);
} else {
try {
this.setCacheObj(responseBody);
var humidity = this.returnHumFromCache();
callback(null, humidity);
} catch (error2) {
this.log("Getting Humidity failed: %s", error2, response, responseBody);
callback(error2);
}
}
}.bind(this));
} else {
try {
var humidity = this.returnHumFromCache();
this.log("Returning cached data", humidity);
callback(null, humidity);
} catch (error) {
this.log("Getting Humidity failed: %s", error);
callback(error);
}
}
},
getStateClouds: function (callback) {
// Only fetch new data once per minute
if (!this.cachedWeatherObj || this.pollingInterval > 0 || this.lastupdate + 60 < (new Date().getTime() / 1000 | 0)) {
var url = this.makeURL();
this.httpRequest(url, function (error, response, responseBody) {
if (error) {
this.log("HTTP get weather function failed: %s", error.message);
callback(error);
} else {
try {
this.setCacheObj(responseBody);
var value = this.returnCloudinessFromCache();
callback(null, value);
} catch (error2) {
this.log("Getting Cloudiness failed: %s", error2, response, responseBody);
callback(error2);
}
}
}.bind(this));
} else {
try {
var value = this.returnCloudinessFromCache();
this.log("Returning cached data", value);
callback(null, value);
} catch (error) {
this.log("Getting Cloudiness failed: %s", error);
callback(error);
}
}
},
getStateSun: function (callback) {
// Only fetch new data once per minute
if (!this.cachedWeatherObj || this.pollingInterval > 0 || this.lastupdate + 60 < (new Date().getTime() / 1000 | 0)) {
var url = this.makeURL();
this.httpRequest(url, function (error, response, responseBody) {
if (error) {
this.log("HTTP get weather function failed: %s", error.message);
callback(error);
} else {
try {
this.setCacheObj(responseBody);
var value = this.returnSunFromCache();
callback(null, value);
} catch (error2) {
this.log("Getting Sun failed: %s", error2, response, responseBody);
callback(error2);
}
}
}.bind(this));
} else {
try {
var value = this.returnSunFromCache();
this.log("Returning cached data", value);
callback(null, value);
} catch (error) {
this.log("Getting Sun failed: %s", error);
callback(error);
}
}
},
getStateWindspeed: function (callback) {
// Only fetch new data once per minute
if (!this.cachedWeatherObj || this.pollingInterval > 0 || this.lastupdate + 60 < (new Date().getTime() / 1000 | 0)) {
var url = this.makeURL();
this.httpRequest(url, function (error, response, responseBody) {
if (error) {
this.log("HTTP get weather function failed: %s", error.message);
callback(error);
} else {
try {
this.setCacheObj(responseBody);
var value = this.returnWindspeedFromCache();
callback(null, value);
} catch (error2) {
this.log("Getting Windspeed failed: %s", error2, response, responseBody);
callback(error2);
}
}
}.bind(this));
} else {
try {
var value = this.returnWindspeedFromCache();
this.log("Returning cached data", value);
callback(null, value);
} catch (error) {
this.log("Getting Windspeed failed: %s", error);
callback(error);
}
}
},
/**
* Handles the response from HTTP-API and caches the data
* @param responseBody
*/
setCacheObj: function (responseBody) {
this.log.debug("Server response:", responseBody);
this.cachedWeatherObj = JSON.parse(responseBody);
this.lastupdate = (new Date().getTime() / 1000);
if (this.enableHistory) {
var temperature;
var humidity;
if (this.showTemperature) {
temperature = this.returnTempFromCache();
}
if (this.showHumidity && this.type === "current") {
humidity = this.returnHumFromCache();
}
this.addHistory(temperature, humidity);
}
},
returnTempFromCache: function () {
var temperature;
if (this.cachedWeatherObj) {
var dayNow = new Date().getDay();
if (this.type === "min") {
// Unfortunately we cannot use the "16 day weather forecast" API but the "5 day / 3 hour forecast" instead.
// this API gives one min/max every 3 hours but we want the daily min/max so we have to iterate over all those data
var min = parseFloat(this.cachedWeatherObj["list"][0]["main"]["temp_min"]);
for (var i = 0, len = this.cachedWeatherObj["list"].length; i < len; i++) {
var dayThen = new Date(this.cachedWeatherObj["list"][i]["dt"] * 1000).getDay();
if (dayThen !== dayNow) {
// API returns data for 5 days, we want min/max only for today
break;
}
if (parseFloat(this.cachedWeatherObj["list"][i]["main"]["temp_min"]) < min) {
min = parseFloat(this.cachedWeatherObj["list"][i]["main"]["temp_min"]);
}
}
temperature = min;
} else if (this.type === "max") {
var max = parseFloat(this.cachedWeatherObj["list"][0]["main"]["temp_max"]);
for (var j = 0, len2 = this.cachedWeatherObj["list"].length; j < len2; j++) {
var dayThen2 = new Date(this.cachedWeatherObj["list"][j]["dt"] * 1000).getDay();
if (dayThen2 !== dayNow) {
break;
}
if (parseFloat(this.cachedWeatherObj["list"][j]["main"]["temp_max"]) > max) {
max = parseFloat(this.cachedWeatherObj["list"][j]["main"]["temp_max"]);
}
}
temperature = max;
} else {
temperature = parseFloat(this.cachedWeatherObj["main"]["temp"]);
}
this.log("Fetched temperature value " + temperature + "deg of type '" + this.type + "' for accessory " + this.name);
}
return temperature;
},
returnHumFromCache: function () {
var value;
if (this.cachedWeatherObj && this.cachedWeatherObj["main"]) {
value = parseFloat(this.cachedWeatherObj["main"]["humidity"]);
this.log("Fetched humidity value " + value + "% of type '" + this.type + "' for accessory " + this.name);
}
return value;
},
returnCloudinessFromCache: function () {
var value;
if (this.cachedWeatherObj && this.cachedWeatherObj["clouds"]) {
value = parseFloat(this.cachedWeatherObj["clouds"]["all"]);
this.log("Fetched cloudiness value " + value + "% of type '" + this.type + "' for accessory " + this.name);
}
return value;
},
returnSunFromCache: function () {
var value = 0;
if (this.cachedWeatherObj && this.cachedWeatherObj["sys"]) {
var sunrise = parseInt(this.cachedWeatherObj["sys"]["sunrise"]);
var sunset = parseInt(this.cachedWeatherObj["sys"]["sunset"]);
var now = Math.round(new Date().getTime() / 1000);
if (now > sunset) {
// It's already dark outside
value = 100;
} else if (now > sunrise) {
// calculate how far though the day (where day is from sunrise to sunset) we are
var intervalLen = (sunset - sunrise);
value = ((now - sunrise) / intervalLen) * 100;
}
this.log("Fetched sun value " + value + "% of type '" + this.type + "' for accessory " + this.name);
}
return value;
},
returnWindspeedFromCache: function () {
var value;
if (this.cachedWeatherObj && this.cachedWeatherObj["wind"]) {
value = parseFloat(this.cachedWeatherObj["wind"]["speed"]);
this.log("Fetched windspeed value " + value + "% of type '" + this.type + "' for accessory " + this.name);
}
return value;
},
makeURL: function () {
var url = "http://api.openweathermap.org/data/2.5/";
if (this.type === "current" || this.type === "clouds" || this.type === "sun" || this.type === "windspeed") {
url += "weather";
} else {
// Min-/Max-sensors have different endpoint
url += "forecast";
}
url += "?appid=" + this.apikey + "&units=" + this.unit + "&";
if (this.locationByCity) {
url += "q=" + this.locationByCity;
} else if (this.locationById) {
url += "id=" + this.locationById;
} else if (this.locationByCoordinates) {
url += this.locationByCoordinates;
} else if (this.locationByZip) {
url += "zip=" + this.locationByZip;
}
this.log.debug("Using url: %s", url);
return url;
},
/**
* Log the temperature to the FakeGato-service.
* Only works if enableHistory is true and pollingInterval > 0
* @param temperature
* @param humidity
*/
addHistory: function (temperature, humidity) {
if (this.enableHistory && this.pollingInterval > 0 && this.fakeGateHistoryService && (temperature || humidity)) {
this.fakeGateHistoryService.addEntry({
time: new Date().getTime() / 1000,
temp: temperature,
humidity: humidity
});
}
},
identify: function (callback) {
this.log("Identify requested");
callback();
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "OpenWeatherMap")
.setCharacteristic(Characteristic.Model, "Location")
.setCharacteristic(Characteristic.SerialNumber, "Homebridge-Weather");
temperatureService = new Service.TemperatureSensor(this.name);
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on("get", this.getStateTemp.bind(this));
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({minValue: -60});
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({maxValue: 120});
var services = [informationService];
if (this.showHumidity && this.type === "current") {
humidityService = new Service.HumiditySensor(this.nameHumidity);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on("get", this.getStateHum.bind(this));
if (this.showTemperature) {
services[services.length] = temperatureService;
}
services[services.length] = humidityService;
} else if (this.type === "clouds") {
humidityService = new Service.HumiditySensor(this.name);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on("get", this.getStateClouds.bind(this));
services[services.length] = humidityService;
} else if (this.type === "sun") {
humidityService = new Service.HumiditySensor(this.name);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on("get", this.getStateSun.bind(this));
services[services.length] = humidityService;
} else if (this.type === "windspeed") {
humidityService = new Service.HumiditySensor(this.name);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on("get", this.getStateWindspeed.bind(this));
services[services.length] = humidityService;
} else {
if (this.showTemperature) {
services[services.length] = temperatureService;
}
}
// FakeGato
if (this.fakeGateHistoryService) {
services[services.length] = this.fakeGateHistoryService;
}
return services;
},
httpRequest: function (url, callback) {
got(url)
.then(response => {
callback(null, response, response.body);
})
.catch(error => {
callback(error);
})
}
};