-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (93 loc) · 2.79 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
const express = require('express');
const nodeWebcam = require('node-webcam');
const prompt = require('prompt');
const { createServer } = require('node:http');
const { Server } = require('socket.io');
const fs = require('fs');
const cors = require('cors')
require('dotenv').config()
const app = express();
const server = createServer(app);
const io = new Server(server);
let data = [];
let currentData = {
temperature: 0, // add sensor values
light: 0, // add sensor values
humidity: 0, // add sensor values
winddirection: 'something', // add sensor values
windspeed: 0, // add sensor values
precipitation: 0 // add sensor values
};
fs.readFile('db.json', (err, fileData) => {
if(err) throw err;
data = JSON.parse(fileData);
//console.log(data);
});
var opts = {
width: 1280,
height: 720,
quality: 100,
frames: 60,
delay: 0,
saveShots: true,
output: "jpeg",
device: false,
callbackReturn: "location",
verbose: false
};
var webcam = nodeWebcam.create(opts);
var length;
var webcams = [];
webcam.list(list => {
list.forEach((i, index) => {
console.log(index, i);
length = index;
webcams.push(i);
});
});
prompt.start();
prompt.get(['camera'], (err, result) => {
if(result.camera == parseInt(result.camera) && parseInt(result.camera) <= length) {
opts.device = webcams[parseInt(result.camera)].substring(3, webcams[parseInt(result.camera)].length);
webcam = nodeWebcam.create(opts);
console.log('using webcam', opts.device)
}else {
console.log('invalid input:', result)
return
}
if(err) throw err
server.listen(process.env.PORT || 80, () => {
console.log(`server listening on port ${process.env.PORT || 80}`);
});
setInterval(() => {
webcam.capture('public/cam');
}, 5000);
});
webcam.capture('test-picture');
app.use(express.static('public'));
app.use(express.json())
app.use(cors())
io.on('connection', (socket) => {
socket.on('getData', () => {
io.to(socket.id).emit('data', {data, currentData});
});
});
app.post('/setData', (req, res) => {
const { pwd } = req.body
if (pwd === process.env.PASSWORD) {
const readData = req.body.data
const { addToDB } = req.body
if(addToDB) {
data.push(readData);
fs.writeFile('db.json', JSON.stringify(data), { encoding: 'utf8' }, () => {
console.log('data saved');
});
}else {
currentData = readData;
}
res.json({info: "uploaded successfully"})
}else {
res.json({info: "wrong password, better not try hacking us"})
console.log('somebody is trying to hack us (probably)')
}
})