forked from cojalvo/homebridge-http-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (133 loc) · 5.88 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
"use strict";
let Service, Characteristic, closeState, openState;
const axios = require('axios');
// Update UI immediately after sensor state change
const updateUI = true;
module.exports = function (homebridge) {
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform('homebridge-http-status', 'HttpStatus', HttpsStatusPlatform);
homebridge.registerAccessory('homebridge-http-status', 'HttpStatusContact', HttpsStatusContactAccessory);
openState = Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
closeState = Characteristic.ContactSensorState.CONTACT_DETECTED;
};
function HttpsStatusPlatform(log, config) {
this.log = log;
this.sensors = config['sensors'] || [];
// Allow retrieval of data from package.json
this.pkginfo = require('pkginfo')(module);
}
HttpsStatusPlatform.prototype = {
accessories: function (callback) {
const accessories = [];
for (let i = 0; i < this.sensors.length; i++) {
var sensorAccessory = new HttpsStatusContactAccessory(this.pkginfo, this.log, this.sensors[i]);
accessories.push(sensorAccessory);
}
const accessoriesCount = accessories.length;
this.log(callback);
callback(accessories);
}
};
function HttpsStatusContactAccessory(pkginfo, log, config) {
this.log = log;
this.name = config['name'] || 'Https Status Sensor';
this.url = config['url'] || 'localhost';
this.method = config['method'] || 'get';
this.headers = config['headers'] || {};
this.expectedResponse = config['expectedResponse'];
this.responsePath = config['responsePath'];
this.requestBody = config['requestBody'];
this.ignoreRequestError = config['ignoreRequestError'] ? config['ignoreRequestError'] : false;
this.okStatus = config['okStatus'] || 200;
this.pingInterval = parseInt(config['interval']) || 300;
// Initial state
this.stateValue = closeState;
this.lastStateValue = closeState;
this._service = new Service.ContactSensor(this.name);
// Default state is open, we want it to be closed
this._service.getCharacteristic(Characteristic.ContactSensorState)
.setValue(this.stateValue);
this._service
.getCharacteristic(Characteristic.ContactSensorState)
.on('get', this.getState.bind(this));
this._service.addCharacteristic(Characteristic.StatusFault);
this.changeHandler = (function (newState) {
this.log('[' + this.name + '] Setting sensor state set to ' + newState);
this._service.getCharacteristic(Characteristic.ContactSensorState)
.setValue(newState ? closeState : openState);
if (updateUI)
this._service.getCharacteristic(Characteristic.ContactSensorState)
.getValue();
}).bind(this);
this.doCheckStatus();
setInterval(this.doCheckStatus.bind(this), this.pingInterval * 1000);
}
HttpsStatusContactAccessory.prototype = {
updateStateIfNeeded: function () {
if (this.lastStateValue !== this.stateValue) {
this.log('[' + this.name + '] Changing state to:' + this.stateValue);
this._service.getCharacteristic(Characteristic.ContactSensorState)
.setValue(this.stateValue);
this._service.getCharacteristic(Characteristic.ContactSensorState)
.getValue();
this.lastStateValue = this.stateValue;
}
}, doCheckStatus: async function () {
try {
this.log(`Start checking status for ${this.url}`);
const config = {
method: this.method,
baseURL: this.url,
headers: this.headers,
data: this.requestBody,
url: ''
};
const res = await axios.request(config);
let success = res.status === this.okStatus;
//check body?
if (success && this.expectedResponse && this.responsePath) {
this.log(`Parsing response body: ${JSON.stringify(res.data)}`);
const parts = this.responsePath.split('.');
let finalRes = res.data;
parts.forEach(p => {
finalRes = finalRes[p];
});
this.log(`Parsed response: ${finalRes} , Expected response: ${this.expectedResponse}`);
success = finalRes === this.expectedResponse;
}
this.stateValue = success ? closeState : openState;
this.setStatusFault(0);
this.updateStateIfNeeded();
this.log('[' + this.name + '] Ping result for ' + this.url + ' was ' + (success ? 'ok' : 'not ok'));
} catch (e) {
if (e.response || this.ignoreRequestError !== true) {
this.log(JSON.stringify(e));
this.stateValue = openState;
this.updateStateIfNeeded();
this.setStatusFault(1);
}
}
},
setStatusFault: function (value) {
this._service.setCharacteristic(Characteristic.StatusFault, value);
},
identify: function (callback) {
this.log('[' + this.name + '] Identify sensor requested');
callback();
},
getState: function (callback) {
this.log('[' + this.name + '] Getting sensor state, which is currently ' + this.stateValue);
callback(null, this.stateValue);
},
getServices: function () {
var informationService = new Service.AccessoryInformation();
// Set plugin information
informationService
.setCharacteristic(Characteristic.Manufacturer, 'jsWorks')
.setCharacteristic(Characteristic.Model, 'Ping State Sensor')
.setCharacteristic(Characteristic.SerialNumber, 'Version ' + module.exports.version);
return [informationService, this._service];
}
};