forked from phenotypic/homebridge-http-thermometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (88 loc) · 3.14 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
let Service, Characteristic
const packageJson = require('./package.json')
const request = require('request')
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory('homebridge-http-thermometer', 'Thermometer', Thermometer)
}
function Thermometer (log, config) {
this.log = log
this.name = config.name
this.apiroute = config.apiroute
this.pollInterval = config.pollInterval || 300
this.manufacturer = config.manufacturer || packageJson.author
this.serial = config.serial || packageJson.version
this.model = config.model || packageJson.name
this.firmware = config.firmware || packageJson.version
this.username = config.username || null
this.password = config.password || null
this.timeout = config.timeout || 3000
this.http_method = config.http_method || 'GET'
if (this.username != null && this.password != null) {
this.auth = {
user: this.username,
pass: this.password
}
}
this.service = new Service.TemperatureSensor(this.name)
}
Thermometer.prototype = {
identify: function (callback) {
this.log('Identify requested!')
callback()
},
_httpRequest: function (url, body, method, callback) {
request({
url: url,
body: body,
method: this.http_method,
timeout: this.timeout,
rejectUnauthorized: false,
auth: this.auth
},
function (error, response, body) {
callback(error, response, body)
})
},
_getStatus: function (callback) {
const url = this.apiroute + '/status'
this.log.debug('Getting status: %s', url)
this._httpRequest(url, '', this.http_method, function (error, response, responseBody) {
if (error) {
this.log.warn('Error getting status: %s', error.message)
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(new Error('Polling failed'))
callback(error)
} else {
this.log.debug('Device response: %s', responseBody)
try {
const json = JSON.parse(responseBody)
this.service.getCharacteristic(Characteristic.CurrentTemperature).updateValue(json.currentTemperature)
this.log.debug('Updated CurrentTemperature to: %s', json.currentTemperature)
callback()
} catch (e) {
this.log.warn('Error parsing status: %s', e.message)
}
}
}.bind(this))
},
getServices: function () {
this.informationService = new Service.AccessoryInformation()
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
this.service.getCharacteristic(Characteristic.CurrentTemperature)
.setProps({
minValue: -100,
maxValue: 100,
minStep: 0.1
})
this._getStatus(function () {})
setInterval(function () {
this._getStatus(function () {})
}.bind(this), this.pollInterval * 1000)
return [this.informationService, this.service]
}
}