-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
112 lines (90 loc) · 3.75 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
var Service, Characteristic;
var request = require('request');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-advanced-http-temperature-humidity", "AdvancedHttpTemperatureHumidity", AdvancedHttpTemperatureHumidity);
}
function AdvancedHttpTemperatureHumidity(log, config) {
this.log = log;
this.humidityService = false;
// Set default values
this.humidity = 0;
// Config
this.url = config["url"];
this.http_method = config["http_method"] || "GET";
this.sendimmediately = config["sendimmediately"] || false;
this.username = config["username"] || "";
this.password = config["password"] || "";
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "HttpTemperatureHumidity";
this.model = config["model"] || "Default";
this.serial = config["serial"] || "18981898";
this.disableHumidity = config["disableHumidity"] || false;
}
AdvancedHttpTemperatureHumidity.prototype = {
httpRequest: function (url, body, method, username, password, sendimmediately, callback) {
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false,
auth: {
user: username,
pass: password,
sendImmediately: sendimmediately
}
},
function (error, response, body) {
callback(error, response, body)
})
},
getStateHumidity: function (callback) {
callback(null, this.humidity);
},
getState: function (callback) {
this.httpRequest(this.url, "", this.http_method, this.username, this.password, this.sendimmediately, function (error, response, responseBody) {
if (error) {
this.log('Get Temperature failed: %s', error.message);
callback(error);
} else {
this.log('Get Temperature succeeded!');
var info = JSON.parse(responseBody);
var temperature = parseFloat(info.temperature);
if (this.humidityService !== false) {
var humidity = parseFloat(info.humidity)
this.humidityService.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
this.humidity = humidity;
}
callback(null, temperature);
}
}.bind(this));
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
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);
temperatureService = new Service.TemperatureSensor(this.name);
temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this));
services.push(temperatureService);
if (this.disableHumidity !== true) {
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({minValue: 0, maxValue: 100})
.on('get', this.getStateHumidity.bind(this));
services.push(this.humidityService);
}
return services;
}
};