-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (82 loc) · 3.26 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('request');
const DEF_MIN_TEMPERATURE = -100,
DEF_MAX_TEMPERATURE = 100,
DEF_TIMEOUT = 5000;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-twine-temperature", "TwineTemperature", TwineTemperature);
}
function TwineTemperature(log, config) {
this.log = log;
// url info
this.url = config["url"];
this.http_method = config["http_method"] || "GET";
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "SuperMechnical";
this.model = config["model"] || "Twine";
this.serial = config["serial"] || "18356D2499BD";
this.fieldName = config["field_name"] || "temperature";
this.timeout = config["timeout"] || DEF_TIMEOUT;
this.minTemperature = config["min_temp"] || DEF_MIN_TEMPERATURE;
this.maxTemperature = config["max_temp"] || DEF_MAX_TEMPERATURE;
this.refresh = config["refresh"] || 600;
setInterval(this.polling.bind(this), this.refresh * 1000);
}
TwineTemperature.prototype = {
getState: function (callback) {
var ops = {
uri: this.url,
method: this.http_method,
timeout: this.timeout,
rejectUnauthorized: false
};
this.log('Requesting temperature on "' + ops.uri + '", method ' + ops.method);
request(ops, (error, res, body) => {
var value = null;
if (error) {
this.log('HTTP bad response (' + ops.uri + '): ' + error.message);
} else {
try {
var value = JSON.parse(body);
var temperature = value.values[1][1].toString();
temperaturesub = temperature.substr(0, temperature.length-2);
value = (temperaturesub - 32) * (5/9);
if (value < this.minTemperature || value > this.maxTemperature || isNaN(value)) {
throw new Error("Invalid value received");
}
this.log('HTTP successful response: ' + value);
} catch (parseErr) {
this.log('Error processing received information: ' + parseErr.message);
error = parseErr;
}
}
callback(error, value);
});
//setInterval(this.polling.bind(this), this.refresh * 1000);
},
polling: function(){
if(this.refresh !==0){
this.log("Polling Tempearture");
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature).getValue();
}
},
getServices: function () {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getState.bind(this))
.setProps({
minValue: this.minTemperature,
maxValue: this.maxTemperature
});
return [this.informationService, this.temperatureService];
}
};