-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (125 loc) · 3.99 KB
/
server.js
File metadata and controls
145 lines (125 loc) · 3.99 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
const express = require('express')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const path = require('path')
const fs = require('fs')
const Gpio = require('onoff').Gpio
const button = new Gpio(17, 'in', 'falling', { debounceTimeout: 50 })
const infoFile = 'info.json'
const databaseFile = 'database.csv'
const perUnitTime = 60000
const bracket = []
let boxCount = 0
const boxMaxItemCount = 1000
const serverGreeting = 'connected to server...'
let info = {
isRecording: false,
fillingLine: '',
product: '',
startTimestamp: '',
endTimestamp: '',
count: '',
totalTime: '',
average: '',
runningAverage: '',
bracketSizeRunningAverage: 5
}
app.use(express.static(__dirname))
app.use(express.static(path.join(__dirname, '/database.csv')))
app.use(express.static(path.join(__dirname, '/node_modules')))
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname, '/index.html'))
})
fs.access(infoFile, (err) => {
if (err) {
fs.appendFile(infoFile, JSON.stringify(info) + '\n', function (err) {
if (err) throw err
})
} else {
info = JSON.parse(fs.readFileSync(infoFile))
}
})
fs.access(databaseFile, (err) => {
if (err) {
fs.appendFile(databaseFile, '', function (err) {
if (err) throw err
})
}
})
function writeToDatabase(data) {
fs.appendFile(databaseFile, data + '\n', function (err) {
if (err) throw err
})
}
function updateBracket(timeStamp) {
if (bracket.length >= info.bracketSizeRunningAverage + 1) {
bracket.shift()
}
bracket.push(timeStamp)
}
function deltaTimestamp(initial, final) {
return (new Date(final) - new Date(initial))
}
function runningAverage() {
return Math.round(info.bracketSizeRunningAverage * perUnitTime / deltaTimestamp(bracket[0], bracket[info.bracketSizeRunningAverage]))
}
function average() {
return Math.round(info.count * perUnitTime / deltaTimestamp(info.startTimestamp, new Date()))
}
function updateInfo() {
if (info.isRecording && info.startTimestamp !== '') {
info.totalTime = deltaTimestamp(info.startTimestamp, new Date())
info.average = average()
}
io.emit('updateInfo', info)
fs.writeFile(infoFile, JSON.stringify(info) + '\n', function (err) {
if (err) throw err
})
}
setInterval(updateInfo, 100)
function formatTimestamp(timeStamp) {
const dateTime = new Date(timeStamp)
const formatedDate = `${dateTime.getDate()}/${dateTime.getMonth() + 1}/${dateTime.getFullYear()}`
const formatedTime = dateTime.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false })
return String('[ ' + formatedTime + ' - ' + formatedDate + ' ]')
}
function pushToClients(data) {
if (boxCount >= boxMaxItemCount) {
boxCount = 0
io.emit('clearBox')
}
io.emit('broad', data)
boxCount++
}
io.on('connection', function (client) {
client.emit('broad', serverGreeting)
client.on('start', function () {
info.isRecording = true
})
client.on('stop', function () {
if (!info.isRecording) return
info.isRecording = false
info.endTimestamp = new Date()
info.totalTime = deltaTimestamp(info.startTimestamp, info.endTimestamp)
info.average = Math.round((info.count * perUnitTime / deltaTimestamp(info.startTimestamp, info.endTimestamp)))
})
})
button.watch((err) => {
if (err) throw err
if (info.isRecording) {
const timeStamp = new Date()
info.count++
if (info.count === 1) {
info.startTimestamp = new Date()
}
updateBracket(timeStamp)
info.runningAverage = runningAverage()
writeToDatabase(String(info.count + ',' + info.runningAverage + ',' + timeStamp.getHours() + ',' + timeStamp.getMinutes() + ',' + timeStamp.getSeconds() + ',' + timeStamp.getDate() + ',' + Number(timeStamp.getMonth() + 1) + ',' + timeStamp.getFullYear()) + ',' + timeStamp)
pushToClients('[ ' + info.count + ' ]' + '[ ' + info.runningAverage + ' ]' + formatTimestamp(timeStamp))
}
})
process.on('SIGINT', _ => {
button.unexport()
})
server.listen(3000)