-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
executable file
·113 lines (95 loc) · 4.03 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
var Service, Characteristic;
var request = require("superagent");
// Require and instantiate a cache module
var cacheModule = require("cache-service-cache-module");
var cache = new cacheModule({storage: "session", defaultExpiration: 60});
// Require superagent-cache-plugin and pass your cache module
var superagentCache = require("superagent-cache-plugin")(cache);
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-httptemperaturehumidity", "HttpTemphum", HttpTemphum);
}
function HttpTemphum(log, config) {
this.log = log;
// Configuration
this.url = config["url"];
this.httpMethod = config["httpMethod"] || "GET";
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "Generic";
this.model = config["model"] || "HTTP(S)";
this.serial = config["serial"] || "";
this.humidity = config["humidity"];
this.lastUpdateAt = config["lastUpdateAt"] || null;
this.cacheExpiration = config["cacheExpiration"] || 60;
}
HttpTemphum.prototype = {
getRemoteState: function(service, callback) {
request(this.httpMethod, this.url)
.set("Accept", "application/json")
.use(superagentCache)
.expiration(this.cacheExpiration)
.end(function(err, res, key) {
if (err) {
this.log(`HTTP failure (${this.url})`);
callback(err);
} else {
this.log(`HTTP success (${key})`);
this.temperatureService.setCharacteristic(
Characteristic.CurrentTemperature,
res.body.temperature
);
this.temperature = res.body.temperature;
if (this.humidity !== false) {
this.humidityService.setCharacteristic(
Characteristic.CurrentRelativeHumidity,
res.body.humidity
);
this.humidity = res.body.humidity;
}
this.lastUpdateAt = +Date.now();
switch (service) {
case "temperature":
callback(null, this.temperature);
break;
case "humidity":
callback(null, this.humidity);
break;
default:
var error = new Error("Unknown service: " + service);
callback(error);
}
}
}.bind(this));
},
getTemperatureState: function(callback) {
this.getRemoteState("temperature", callback);
},
getHumidityState: function(callback) {
this.getRemoteState("humidity", callback);
},
getServices: function () {
var services = [],
informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(informationService);
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({ minValue: -273, maxValue: 200 })
.on("get", this.getTemperatureState.bind(this));
services.push(this.temperatureService);
if (this.humidity !== false) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({ minValue: 0, maxValue: 100 })
.on("get", this.getHumidityState.bind(this));
services.push(this.humidityService);
}
return services;
}
};