This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
forked from roozbehf/homebridge-http-temperature-humidity
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·133 lines (115 loc) · 3.46 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
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"] || "GET";
this.sendimmediately = config["sendimmediately"] || "";
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "Luca Manufacturer";
this.model = config["model"] || "Luca Model";
this.serial = config["serial"] || "Luca Serial";
this.humidity = config["humidity"];
}
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 > 300) {
this.log("HTTP power function failed");
var err = new Error(
"Server responded with status code " + res.statusCode
);
err.statusCode = res.statusCode;
err.headers = res.headers;
err.body = res.body;
callback(err);
} else {
this.log("HTTP power function succeeded!");
var info = JSON.parse(res.body);
info.temperature = parseFloat(info.temperature);
info.humidity = parseFloat(info.humidity);
temperatureService.setCharacteristic(
Characteristic.CurrentTemperature,
info.temperature
);
if (this.humidity != false)
humidityService.setCharacteristic(
Characteristic.CurrentRelativeHumidity,
info.humidity
);
this.log(res.body);
this.log(info);
this.temperature = info.temperature;
if (this.humidity !== false) this.humidity = info.humidity;
callback(null, this.temperature);
}
},
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);
// setting the serial number resulted an issues
// .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.humidity !== false) {
humidityService = new Service.HumiditySensor(this.name);
humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.setProps({ minValue: 0, maxValue: 100 })
.on("get", this.getStateHumidity.bind(this));
services.push(humidityService);
}
return services;
}
};