-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·92 lines (73 loc) · 2.76 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
var Service, Characteristic;
var request = require('sync-request');
var temperatureService;
var humidityService;
var url
var humidity = 0;
var temperature = 0;
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;
// url info
this.url = config["url"];
this.http_method = config["http_method"];
this.sendimmediately = config["sendimmediately"];
this.name = config["name"];
}
HttpTemphum.prototype = {
httpRequest: function (url, body, method, username, password, sendimmediately, callback) {
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false
},
function (error, response, body) {
callback(error, response, body)
})
},
getStateHumidity: function(callback){
callback(null, this.humidity);
},
getState: function (callback) {
var body;
var res = request(this.http_method, this.url, {});
if(res.statusCode > 400){
this.log('HTTP power function failed');
callback(error);
}else{
this.log('HTTP power function succeeded!');
var info = JSON.parse(res.body);
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, info.temperature);
humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, info.humidity);
this.log(res.body);
this.log(info);
this.temperature = info.temperature;
callback(null, this.temperature);
}
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Luca Manufacturer")
.setCharacteristic(Characteristic.Model, "Luca Model")
.setCharacteristic(Characteristic.SerialNumber, "Luca Serial Number");
temperatureService = new Service.TemperatureSensor(this.name);
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this));
humidityService = new Service.HumiditySensor(this.name);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getStateHumidity.bind(this));
return [temperatureService, humidityService];
}
};