-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·295 lines (263 loc) · 9.41 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
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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
// no more cache
app.use((req, res, next) => {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, private');
next();
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// app.use(express.static(__dirname + '/ressources'));
app.set('view engine', 'ejs')
app.set('views', './views')
// var url = require('url');
// const xlsx = require('xlsx');
const firebase = require('firebase');
const admin = require('firebase-admin');
const server = require('http').Server(app);
const io = require('socket.io')(server);
const fs = require('fs');
// const peerjs = require('peerjs');
// var p2p = require('socket.io-p2p')
// var player = require('play-sound')(opts = {});
server.listen(8080);
// server.listen(process.env.PORT || 8080);
admin.initializeApp({
credential: admin.credential.cert(require("./streamopencv-firebase-adminsdk-1m2qi-a0f9a36b01.json")),
databaseURL: "https://streamopencv.firebaseio.com"
});
// Initialize Firebase
firebase.initializeApp({
apiKey: "AIzaSyAgb7aS-JDiAqldfh-9kx247333P2VsIPk",
authDomain: "streamopencv.firebaseapp.com",
databaseURL: "https://streamopencv.firebaseio.com",
projectId: "streamopencv",
storageBucket: "bucket.appspot.com",
messagingSenderId: "799475033788",
appId: "1:799475033788:web:356bbc071b9b50c63452b9",
measurementId: "G-7JYQ524WQY"
});
var nspStream = io.of('/VideoStream');
var nspBrowser = io.of('/Browser');
// var nspEsp = io.of('/Esp8266');
var refCrossLog = firebase.database().ref('/crosslock');
var reffilter_Sensor = firebase.database().ref().orderByKey().startAt('sens');
// esp
io.on('connection', socket => {
socket.on('sensor', objData => {
var MAC = Object.keys(objData).toString();
reffilter_Sensor.once('value')
.then(arrSensor => {
arrSensor.forEach(sensor => {
if (MAC == sensor.val().MAC) {
var update = {};
var now = new Date();
var refLOG = firebase.database().ref('/' + sensor.key + '/logs');
update[now.getTime()] = objData[MAC];
refLOG.update(update);
}
});
});
});
});
nspStream.on('connection', socket => {
console.log('Python Socket has connected');
// if btn then which socket id can emit
socket.on('stream', data => nspBrowser.emit('stream', data));
socket.on('auth', (queryCamID, password, uniqueSensorID) => {
firebase.database().ref('/' + uniqueSensorID + '/camera').once('value', snap => {
if ((snap.val().id == queryCamID) && (snap.val().pass == password)) {
socket.emit('auth', 'GRANTED');
}
else { socket.emit('auth', 'DENIED'); }
});
});
});
// When home.ejs has been rendered
nspBrowser.on('connection', socket => {
socket.on('disconnect', () => {
nspStream.emit('onunload');
refCrossLog.off();
reffilter_Sensor.off();
});
socket.on('setDBStatus', objCrossLock => {
refCrossLog.set(objCrossLock);
});
socket.once('onload', () => {
var arrLocation = [];
reffilter_Sensor.on('value', arrSensor => {
arrSensor.forEach(sensor => {
firebase.database().ref('/' + sensor.key + '/location').once('value', location => {
arrLocation.push(location.val());
});
});
});
var objID = {};
reffilter_Sensor.on('value', arrSensor => {
arrSensor.forEach(sensor => {
objID[sensor.key] = sensor.val().camera.id
});
});
var objStatus = {};
refCrossLog.on('value', snap => {
console.log(snap.val());
nspStream.emit('crosslock', snap.val());
objStatus = snap.val();
socket.emit('dbInfo', objID, objStatus, arrLocation);
});
// setInterval(() => {
//
// }, 1000);
// refCrossLog.on('value', snap => {
// nspStream.emit('crosslock', snap.val());
// objStatus = snap.val();
// });
// setTimeout(() => {
// console.log(objStatus);
// socket.emit('dbInfo', objID, objStatus, arrLocation);
// }, 500);
});
});
// Web Routing
app.get('/', (req, res) => {
res.redirect('/login');
console.log('Welcome ' + req.connection.remoteAddress);
// player.play('./facebook_messenger.mp3', function(err){if (err) throw err});
});
app.post('/auth', (req, res) => {
firebase.auth().signInWithEmailAndPassword(req.body.id, req.body.pass).then(() => {
res.send('Access Granted');
}).catch(err => { res.send(err.message) });
});
// Set an authentication state observer and get user data
firebase.auth().onAuthStateChanged(user => {
this.user = user;
app.get('/home', (req, res) => {
if (this.user) { res.render('home', { userName: this.user.email }); }
});
// Route devices
// app.get('/devices', (req, res) => {
// if (this.user) { res.render('devices'); }
// });
app.get('/users', (req, res) => {
if (this.user) { res.render('users'); }
});
});
// Route logout
app.get('/login', (req, res) => {
firebase.auth().signOut();
res.render('login');
});
var qrLocat;
reffilter_Sensor.once('value')
.then(arrSensor => {
arrSensor.forEach(sensor => {
firebase.database().ref('/' + sensor.key + '/logs').on('child_added', snap => {
if (qrLocat == sensor.val().location.address) {
nspBrowser.emit('cloudVal', snap.key, snap.val());
}
});
});
}).catch(err => console.log(err));
app.post('/calendar', (req, res) => {
qrLocat = req.body.locatCombobox;
reffilter_Sensor.once('value')
.then(arrSensor => {
arrSensor.forEach(sensor => {
if (qrLocat == sensor.val().location.address) {
if (req.body.download) { res.status(201).send(sensor.val().logs); } // download request
// view request from #btnViewLog or #btnToday
else {
res.status(202).send(sensor.val().logs);
fs.readFile('views/' + qrLocat + '.html', (err, html) => {
nspBrowser.emit('card', html.toString());
});
}
}
});
}).catch(err => console.log(err));
});
// let demo = new Promise((resolve, reject) => {
// let arrLocation = [];
// let objID = {};
// let objStatus = new Object();
// reffilter_Sensor.once('value')
// .then(arrSensor => {
// arrSensor.forEach(sensor => {
// objID[sensor.key] = sensor.val().camera.id
// });
// })
// .then(refCrossLog.once('value')
// .then(snap => {
// // nspStream.emit('crosslock', snap.val());
// // objStatus = snap.val();
// Object.assign(objStatus, snap.val());
// // console.log(objStatus);
// // console.log(snap.val());
// // socket.emit('dbInfo', objID, objStatus, arrLocation);
// }))
// .then(reffilter_Sensor.once('value')
// .then(arrSensor => {
// arrSensor.forEach(sensor => {
// firebase.database().ref('/' + sensor.key + '/location').once('value', location => {
// arrLocation.push(location.val());
// });
// });
// }))
// .then(resolve({objID, objStatus, arrLocation}))
// });
// ////////////////////////
// let demo3 = new Promise((resolve, reject) => {
// let arrLocation = [];
// let objID = {};
// let objStatus = new Object();
// reffilter_Sensor.once('value')
// .then(arrSensor => {
// arrSensor.forEach(sensor => {
// objID[sensor.key] = sensor.val().camera.id
// });
// return refCrossLog.once('value')
// })
// .then(snap => {
// Object.assign(objStatus, snap.val());
// return reffilter_Sensor.once('value')
// })
// .then(arrSensor => {
// arrSensor.forEach(sensor => {
// firebase.database().ref('/' + sensor.key + '/location').once('value', location => {
// arrLocation.push(location.val());
// });
// });
// })
// .then(resolve({objID, objStatus, arrLocation}))
// });
// // demo3.then(msg => console.log(msg));
// ////////////////////
// async function demo2() {
// const LOCAT = await reffilter_Sensor.once('value')
// .then(arrSensor => {
// let arrLocation = [];
// arrSensor.forEach(sensor => {
// firebase.database().ref('/' + sensor.key + '/location').once('value')
// .then(location => {
// arrLocation.push(location.val());
// });
// });
// return arrLocation
// });
// const ID = await reffilter_Sensor.once('value')
// .then(arrSensor => {
// let objID = {};
// arrSensor.forEach(sensor => {
// objID[sensor.key] = sensor.val().camera.id
// });
// return objID;
// });
// const STT = await refCrossLog.once('value')
// .then(snap => {
// // Object.assign(objStatus, snap.val());
// return snap.val();
// });
// }