-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
82 lines (70 loc) · 2.25 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
var Service, Characteristic;
var superagent = require('superagent');
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http-json", "http-json", HttpAccessory);
}
function HttpAccessory(log, config) {
this.log = log;
this.url = config["url"];
this.service = config["service"];
this.name = config["name"];
this.sensors = config["sensors"];
}
HttpAccessory.prototype = {
getTemperature: function(callback) {
this.log("Temperature Triggered");
superagent.get(this.url).end(function(err, res){
if (res.body.temperature) {
callback(null, res.body['temperature']);
} else {
callback(null, null);
}
});
},
getHumidity: function(callback) {
this.log("Humidity Triggered");
superagent.get(this.url).end(function(err, res){
if (res.body.humidity) {
callback(null, res.body['humidity']);
} else {
callback(null, null);
}
});
},
identify: function(callback) {
this.log("Identify requested!");
callback();
},
getServices: function() {
this.log("getServices")
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Nuno Ferro")
.setCharacteristic(Characteristic.Model, "HTTP JSON")
.setCharacteristic(Characteristic.SerialNumber, "ACME#1")
if (this.service == "Thermostat") {
var services = [informationService];
for (var i = this.sensors.length - 1; i >= 0; i--) {
let sensor = this.sensors[i];
let url = this.url;
this.log("Setting up: " + sensor.name);
newService = new Service[sensor.service](sensor.name);
newService.getCharacteristic(Characteristic[sensor.caractheristic])
.on('get', function(callback) {
console.log(sensor.name + " Triggered");
superagent.get(url).end(function(err, res){
if (res && res.body[sensor.field]) {
callback(null, res.body[sensor.field]);
} else {
callback(null, null);
}
});
})
services.push(newService);
}
return services;
}
}
};