-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
325 lines (299 loc) · 9.92 KB
/
server.js
File metadata and controls
325 lines (299 loc) · 9.92 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var Cesium = require('cesium')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const ejs = require('ejs')
const port = process.env.PORT || 3000
const elasticsearch = require('elasticsearch')
app.set('view engine', 'ejs')
var es = new elasticsearch.Client({
host: process.env.ES_URL || 'http://localhost:9200',
httpAuth: process.env.ES_HTTP_AUTH || ''
});
es.ping({
// ping usually has a 3000ms timeout
requestTimeout: 3000
}, function (error) {
if (error) {
console.trace('ElasticSearch cluster is down!');
process.exit(1);
} else {
console.log('ElasticSearch cluster is reachable.');
}
});
// create application/json parser
var jsonParser = bodyParser.json()
app.use('/Build', express.static('node_modules/cesium/Build'))
app.use('/models', express.static('models'))
app.get('/', function (req, res) {
res.render('index')
})
var state = {
"doorCamera" : { "color" : "BLACK", "alpha" : 1.0, "alphaStart" : 1.0, "alphaEnd" : 0.5 },
"accessPoint" : { "color" : "BLACK", "alpha" : 1.0, "alphaStart" : 1.0, "alphaEnd" : 0.5 },
"doorLock" : { "color" : "BLACK", "alpha" : 1.0, "alphaStart" : 1.0, "alphaEnd" : 0.5 },
'motionDetector' : { "color" : "BLACK", "alpha" : 1.0, "alphaStart" : 1.0, "alphaEnd" : 0.5 },
'lamp1' : { "color" : "BLACK", "alpha" : 1.0, "alphaStart" : 1.0, "alphaEnd" : 0.5 },
'undeerground' : { "color" : "WHITE", "alpha" : 0.5, "alphaStart" : 0.5, "alphaEnd" : 0.25 }
}
/* Need to eventually instrument these as well:
'miniCamera' : { "color" : "BLACK", "alpha" : 1.0 },
'blueRange1' : { "color" : "BLACK", "alpha" : 1.0 },
'alexa' : { "color" : "BLACK", "alpha" : 1.0 }
*/
app.get('/state', function (req, res) {
res.status(200).json({ success: true, state: state })
})
var updateStateInterval;
function updateState() {
/*
// If unknown client attaches to the accessPoint, turn it YELLOW, otherwise make it BLACK
var underground_query = {
index: 'underground-ap-devices-*',
type: 'webhook',
body: {
query: {
range: { timestamp: { gte: 'now-60s', lt: 'now' } }
}
}
}
es.search(underground_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(underground_query)) }
state["accessPoint"]["color"] = ( hits > 0 ? "YELLOW" : "BLACK" )
}, function (err) {
if(err) {
console.trace(err.message);
}
})
var now_in_milliseconds = (new Date).getTime();
// If person is detected by the doorCamera (persondetect), turn it WHITE, otherwise make it BLACK
var persondetect_query = {
index: 'persondetect',
type: '_doc',
body: {
query: {
range: { DeviceTime: { gte: now_in_milliseconds - 10000, lt: now_in_milliseconds } }
}
}
};
es.search(persondetect_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(persondetect_query)) }
state["doorCamera"]["persondetect"] = hits
if(("webcam-pcap" in state["doorCamera"]) && state["doorCamera"]["webcam-pcap"] <= 0) {
state["doorCamera"]["color"] = ( hits > 0 ? "WHITE" : "BLACK" )
}
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If unauthorized connection to the doorCamera occurs (webcam-pcap-*), turn it YELLOW, otherwise make it BLACK
var webcam_query = {
index: 'webcam-pcap-*',
type: 'webhook',
body: {
query: {
range: { timestamp: { gte: 'now-60s', lt: 'now' } }
}
}
}
es.search(webcam_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(webcam_query)) }
state["doorCamera"]["color"] = ( hits > 0 ? "YELLOW" : "BLACK" )
state["doorCamera"]["webcam-pcap"] = hits
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If doorLock is manually unlocked (door-lock-*), turn it YELLOW, otherwise make it BLACK
var doorlock_query = {
index: 'door-lock-*',
type: 'webhook',
body: {
sort: { timestamp: { order: "desc" }},
query: {
bool: {
must: { match: { user: "Manual Unlock" } },
filter: [ { range: { timestamp: { gte: 'now-60s', lt: 'now' } } } ]
}
}
}
}
es.search(doorlock_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(doorlock_query)) }
state["doorLock"]["door-lock"] = hits
if(hits>0) {
if(state["doorLock"]["color"] == "WHITE" || state["doorLock"]["color"] == "BLACK") {
state["doorLock"]["color"] = "YELLOW"
}
}
}, function (err) {
if(err) {
console.trace(err.message);
}
})
var ifttt_query = {
index: 'ifttt-*',
type: 'webhook',
body: {
sort: { timestamp: { order: "desc" }},
query: {
bool: {
must: { match: { user: "Manual Unlock" } },
filter: [ { range: { timestamp: { gte: 'now-60s', lt: 'now' } } } ]
}
}
}
}
es.search(ifttt_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(ifttt_query)) }
state["doorLock"]["ifttt"] = hits
if(("door-lock" in state["doorLock"]) && state["doorLock"]["door-lock"] <= 0) {
state["doorLock"]["color"] = ( hits > 0 ? "YELLOW" : "BLACK" )
}
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If doorLock is NOT manually unlocked (door-lock-*), turn it WHITE, otherwise make it BLACK
var doorlock_notquery = {
index: 'door-lock-*',
type: 'webhook',
body: {
sort: { timestamp: { order: "desc" }},
query: {
bool: {
must_not: { match: { user: "Manual Unlock" } },
filter: [ { range: { timestamp: { gte: 'now-10s', lt: 'now' } } } ]
}
}
}
}
es.search(doorlock_notquery).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(doorlock_notquery)) }
state["doorLock"]["notdoor-lock"] = hits
if((("ifttt" in state["doorLock"]) && state["doorLock"]["ifttt"] <= 0) &&
(("door-lock" in state["doorLock"]) && state["doorLock"]["door-lock"] <= 0)) {
if(state["doorLock"]["color"] == "BLACK") {
if(hits > 0) { state["doorLock"]["color"] = "WHITE" }
}
}
}, function (err) {
if(err) {
console.trace(err.message);
}
})
var ifttt_notquery = {
index: 'ifttt-*',
type: 'webhook',
body: {
sort: { timestamp: { order: "desc" }},
query: {
bool: {
must_not: { match: { user: "Manual Unlock" } },
filter: [ { range: { timestamp: { gte: 'now-10s', lt: 'now' } } } ]
}
}
}
}
es.search(ifttt_notquery).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(ifttt_notquery)) }
state["doorLock"]["notifttt"] = hits
if((("ifttt" in state["doorLock"]) && state["doorLock"]["ifttt"] <= 0) &&
(("door-lock" in state["doorLock"]) && state["doorLock"]["door-lock"] <= 0)) {
if(("notdoor-lock" in state["doorLock"]) && state["doorLock"]["notdoor-lock"] <= 0) {
state["doorLock"]["color"] = ( hits > 0 ? "WHITE" : "BLACK" )
}
}
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If motionDetector has triggered (domoticz), turn it WHITE, otherwise make it BLACK
var domoticz_query = {
index: 'domoticz*',
type: 'notification',
body: {
query: {
bool: {
must: { match: { message: "Motion" } },
filter: [ { range: { timestamp: { gte: 'now-10s', lt: 'now' } } } ]
}
}
}
};
es.search(domoticz_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(domoticz_query)) }
state["motionDetector"]["color"] = ( hits > 0 ? "WHITE" : "BLACK" )
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If Alexa has triggered the lamp (alexa-trigger-*), turn it WHITE, otherwise make it BLACK
var alexatrigger_query = {
index: 'alexa-trigger-*',
type: 'webhook',
body: {
query: {
bool: {
must: { match: { target: "lamp" } },
filter: [ { range: { timestamp: { gte: 'now-10s', lt: 'now' } } } ]
}
}
}
};
es.search(alexatrigger_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(alexatrigger_query)) }
state["lamp1"]["color"] = ( hits > 0 ? "WHITE" : "BLACK" )
}, function (err) {
if(err) {
console.trace(err.message);
}
})
// If David's underground algorithm has triggered (sfalgo), turn the underground RED, otherwise make it WHITE
var sfalgo_query = {
index: 'sfalgo',
type: '_doc',
body: {
query: {
range: { DateTime: { gte: now_in_milliseconds - 10000, lt: now_in_milliseconds } }
}
}
};
es.search(sfalgo_query).then(function (resp) {
var hits = (resp.hits && resp.hits.hits.length) || 0;
if(hits > 0) { console.log(`${hits} ` + JSON.stringify(sfalgo_query)) }
var seconds = Math.floor(new Date().getTime() / 1000);
state["underground"]["color"] = ( (hits > 0 ) && ( seconds % 2 == 0 ) ? "RED" : "WHITE" )
}, function (err) {
if(err) {
console.trace(err.message);
}
})
*/
}
updateStateInterval = setInterval(updateState, 1000);
app.post('/', jsonParser, (req, res) => {
console.log('POST /')
console.log(req.body)
res.status(200).json({ success: true })
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})