-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (177 loc) · 6.19 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
196
197
198
199
200
201
202
203
204
205
206
207
208
"use strict";
var Service, Characteristic;
const http = require("http");
const url = require("url");
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-motionsensor-web", "motionsensor-web-platform", MotionSensorWebPlatform);
};
/*
* Platform code
* */
function MotionSensorWebPlatform(log, config) {
this.log = log;
this.config = config;
}
MotionSensorWebPlatform.prototype = {
accessories: function (callback) {
var config = this.config;
var sensors = config.sensors;
this.httpPort = config["httpPort"] || 8888;
this.accessories = [];
this.sensorDictionary= {};
for (var i = 0, l = sensors.length; i < l; i++) {
var sensor = sensors[i];
var sensorName = sensor.sensorName;
if (!sensorName) {
this.log("Could not find name in the following sensor config, so did not add.");
this.log(JSON.stringify(sensor));
continue;
}
sensorName = sensorName.toLowerCase();
this.sensorDictionary[sensorName] = new MotionSensorWeb(this.log, sensor);
this.accessories.push(this.sensorDictionary[sensorName]);
this.log(`Added sensor with name ${sensorName}.`);
}
callback(this.accessories);
this.setupWebServer();
},
updateSensorState: function (isStart, sensorName) {
if (!sensorName) {
this.log("A sensor name must be supplied to update sensor state.");
return;
}
var sensor = this.sensorDictionary[sensorName.toLowerCase()];
if (!sensor) {
this.log(`Could not find sensor with name ${sensorName}.`);
return;
}
sensor.updateState(isStart);
this.log(`Updated sensor ${sensorName} as ${isStart}.`);
},
//WEBS
setupWebServer: function () {
this._webServer = http.createServer(this.requestHandler.bind(this));
this._webServer.listen(this.httpPort, (err) => {
if (err) {
return this.log(`An error occurred when setting up the web server. Error: ${err}.`);
}
this.log(`Web server is listening on ${this.httpPort}.`);
});
},
requestHandler: function (request, response) {
var rawURL = request.url.toLowerCase(); //e.g. /start?sensorName=sensorName or /stop?sensorName=sensorName
var searchDictionary = (function (rawURL) {
var dictionary = {};
if (!rawURL || rawURL.indexOf("?") == -1) {
return dictionary;
}
var search = rawURL.split("?")[1];
var searchData = search.split("&");
for (var i = 0, l = searchData.length; i < l; i++) {
var keyValuePair = searchData[i].split("=");
var key = decodeURIComponent(keyValuePair[0]);
var value = decodeURIComponent(keyValuePair.length > 1 ? keyValuePair[1] : "");
dictionary[key.toLowerCase()] = value;
}
return dictionary;
})(rawURL);
var pathname = rawURL.split("?")[0].toLowerCase();
this.log(`Handled request URL: ${rawURL}.`);
this.log(`Handled request pathname: ${pathname}.`);
this.log(`Handled request search data: ${JSON.stringify(searchDictionary)}.`);
if (searchDictionary && !searchDictionary.sensorname) {
this.log("No sensorName parameter in URL, cannot process.");
return;
}
switch (pathname) {
case "/start":
this.updateSensorState(true, searchDictionary.sensorname);
break;
case "/stop":
this.updateSensorState(false, searchDictionary.sensorname);
break;
}
response.end("Pong");
}
}
/*
* Accessory code
* */
function MotionSensorWeb(log, config) {
this.log = log;
this.name = config["name"];
this.manufacturer = config["manufacturer"];
this.model = config["model"];
this.serial = config["serial"] || "Non-defined serial";
this.startAfterStopFuseMs = config["startAfterStopFuseMs"] || 750;
this.stopDelayMs = config["stopDelayMs"] || 1500;
this.startFuseActive = false;
this.motionDetected = false;
this.stopDelayTimeoutID = -1;
}
MotionSensorWeb.prototype = {
getState: function (callback) {
this.log(`getState called and motionDetected: ${this.motionDetected}.`);
callback(null, this.motionDetected);
},
getServices: function () {
var services = [];
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(this.informationService);
this.motionService = new Service.MotionSensor(this.name);
this.motionService
.getCharacteristic(Characteristic.MotionDetected)
.on("get", this.getState.bind(this));
services.push(this.motionService);
return services;
},
/*
* Scenarios:
* Start and is currently stopped - trigger immediately
* Start and stop is queued - cancel stop if fuse has passed. This accounts for the lights turning off and triggering the motion.
* Stop after delay - this is so the light can stay on for a while after the motion has finished
*/
updateState: function (motionDetected) {
motionDetected = !!motionDetected;
if (motionDetected == this.motionDetected) {
this.log("Update state fired but hasn't changed, so didn't update.");
return;
}
if (motionDetected && !this.startFuseActive && this.stopDelayTimeoutID > -1) {
this.log("A motion start event fired while stop was queued, cleared stop queue.");
clearTimeout(this.stopDelayTimeoutID);
return;
} else if (motionDetected && this.startFuseActive) {
this.log("A motion start event fired but fuse is running, didn't send start motion event.");
return;
}
if (!motionDetected) {
this.log("A motion end event fired. Stop event has now been queued.");
this.stopDelayTimeoutID = setTimeout(() => {
this.setState(false);
this.log("A motion end event set. Event sent.");
this.stopDelayTimeoutID = -1;
}, this.stopDelayMs);
this.startFuseActive = true;
this.log("Fuse started.");
setTimeout(() => {
this.startFuseActive = false;
this.log("Fuse cleared.");
}, this.startAfterStopFuseMs);
return;
}
this.setState(motionDetected);
},
setState: function (motionDetected) {
this.motionDetected = !!motionDetected;
this.motionService.getCharacteristic(Characteristic.MotionDetected)
.updateValue(this.motionDetected, null, "updateState");
this.log(`Motion state updated to ${this.motionDetected}.`);
}
};