-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
195 lines (162 loc) · 6.91 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"use strict";
let Service, Characteristic, api;
const _http_base = require("homebridge-http-base");
const http = _http_base.http;
const configParser = _http_base.configParser;
const PullTimer = _http_base.PullTimer;
const notifications = _http_base.notifications;
const MQTTClient = _http_base.MQTTClient;
const Cache = _http_base.Cache;
const utils = _http_base.utils;
const packageJSON = require("./package.json");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
api = homebridge;
homebridge.registerAccessory("homebridge-http-temperature-sensor", "HTTP-TEMPERATURE", HTTP_TEMPERATURE);
};
const TemperatureUnit = Object.freeze({
Celsius: "celsius",
Fahrenheit: "fahrenheit"
});
function HTTP_TEMPERATURE(log, config) {
this.log = log;
this.name = config.name;
this.debug = config.debug || false;
if (config.getUrl) {
try {
this.getUrl = configParser.parseUrlProperty(config.getUrl);
} catch (error) {
this.log.warn("Error occurred while parsing 'getUrl': " + error.message);
this.log.warn("Aborting...");
return;
}
}
else {
this.log.warn("Property 'getUrl' is required!");
this.log.warn("Aborting...");
return;
}
this.unit = utils.enumValueOf(TemperatureUnit, config.unit, TemperatureUnit.Celsius);
if (!this.unit) {
this.unit = TemperatureUnit.Celsius;
this.log.warn(`${config.unit} is an unsupported temperature unit! Using default!`);
}
this.statusCache = new Cache(config.statusCache, 0);
this.statusPattern = /(-?[0-9]{1,3}(\.[0-9])?)/;
try {
if (config.statusPattern)
this.statusPattern = configParser.parsePattern(config.statusPattern);
} catch (error) {
this.log.warn("Property 'statusPattern' was given in an unsupported type. Using default one!");
}
this.patternGroupToExtract = 1;
if (config.patternGroupToExtract) {
if (typeof config.patternGroupToExtract === "number")
this.patternGroupToExtract = config.patternGroupToExtract;
else
this.log.warn("Property 'patternGroupToExtract' must be a number! Using default value!");
}
this.homebridgeService = new Service.TemperatureSensor(this.name);
this.homebridgeService.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100
})
.on("get", this.getTemperature.bind(this));
/** @namespace config.pullInterval */
if (config.pullInterval) {
this.pullTimer = new PullTimer(log, config.pullInterval, this.getTemperature.bind(this), value => {
this.homebridgeService.setCharacteristic(Characteristic.CurrentTemperature, value);
});
this.pullTimer.start();
}
/** @namespace config.notificationPassword */
/** @namespace config.notificationID */
notifications.enqueueNotificationRegistrationIfDefined(api, log, config.notificationID, config.notificationPassword, this.handleNotification.bind(this));
/** @namespace config.mqtt */
if (config.mqtt) {
let options;
try {
options = configParser.parseMQTTOptions(config.mqtt);
} catch (error) {
this.log.error("Error occurred while parsing MQTT property: " + error.message);
this.log.error("MQTT will not be enabled!");
}
if (options) {
try {
this.mqttClient = new MQTTClient(this.homebridgeService, options, this.log);
this.mqttClient.connect();
} catch (error) {
this.log.error("Error occurred creating MQTT client: " + error.message);
}
}
}
}
HTTP_TEMPERATURE.prototype = {
identify: function (callback) {
this.log("Identify requested!");
callback();
},
getServices: function () {
if (!this.homebridgeService)
return [];
const informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "Andreas Bauer")
.setCharacteristic(Characteristic.Model, "HTTP Temperature Sensor")
.setCharacteristic(Characteristic.SerialNumber, "TS01")
.setCharacteristic(Characteristic.FirmwareRevision, packageJSON.version);
return [informationService, this.homebridgeService];
},
handleNotification: function(body) {
const characteristic = utils.getCharacteristic(this.homebridgeService, body.characteristic);
if (!characteristic) {
this.log("Encountered unknown characteristic when handling notification (or characteristic which wasn't added to the service): " + body.characteristic);
return;
}
let value = body.value;
if (body.characteristic === "CurrentTemperature" && this.unit === TemperatureUnit.Fahrenheit)
value = (value - 32) / 1.8;
if (this.debug)
this.log("Updating '" + body.characteristic + "' to new value: " + body.value);
characteristic.updateValue(value);
},
getTemperature: function (callback) {
if (!this.statusCache.shouldQuery()) {
const value = this.homebridgeService.getCharacteristic(Characteristic.CurrentTemperature).value;
if (this.debug)
this.log(`getTemperature() returning cached value ${value}${this.statusCache.isInfinite()? " (infinite cache)": ""}`);
callback(null, value);
return;
}
http.httpRequest(this.getUrl, (error, response, body) => {
if (this.pullTimer)
this.pullTimer.resetTimer();
if (error) {
this.log("getTemperature() failed: %s", error.message);
callback(error);
}
else if (!http.isHttpSuccessCode(response.statusCode)) {
this.log("getTemperature() returned http error: %s", response.statusCode);
callback(new Error("Got http error code " + response.statusCode));
}
else {
let temperature;
try {
temperature = utils.extractValueFromPattern(this.statusPattern, body, this.patternGroupToExtract);
} catch (error) {
this.log("getTemperature() error occurred while extracting temperature from body: " + error.message);
callback(new Error("pattern error"));
return;
}
if (this.unit === TemperatureUnit.Fahrenheit)
temperature = (temperature - 32) / 1.8;
if (this.debug)
this.log("Temperature is currently at %s", temperature);
this.statusCache.queried();
callback(null, temperature);
}
});
},
};