-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (64 loc) · 2.02 KB
/
app.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
const app = require('express')()
const server = require('http').Server(app)
const tcpPort = process.env.PORT || 4113
const io = require('socket.io')(server)
const SerialPort = require('serialport')
const SimpleNodeLogger = require('simple-node-logger')
const config = require('./config.json')
let env = process.argv[2] || 'dev'
switch (env) {
case 'dev':
console.log(`starting in development mode`)
break;
case 'prod':
console.log(`starting in production mode`)
break;
}
process.on('uncaughtException', err => {
console.log('err: ', err)
console.log('stack trace: ', err.stack)
setInterval(() => {}, 2000)
})
// create logger instance
let log = SimpleNodeLogger.createSimpleLogger({
logFilePath:'general.log',
timestampFormat:'YYYY-MM-DD HH:mm:ss'
})
// start listening
server.listen(tcpPort, () => log.info(`listening on http://localhost:${tcpPort}`))
if (env === 'dev') {
app.get('/', (req, res) => {
res.sendFile('./public/index.html', { root: __dirname })
})
io.on('connection', socket => {
log.info('connected')
socket.on('getValue', () => {
let num = Math.floor(Math.random() * 51)
console.log(`emitting ${num}`)
io.emit('newWeight', {weight: num})
})
})
}
else {
const Readline = SerialPort.parsers.Readline
const parser = new Readline()
const port = new SerialPort(config.path, config)
port.open(err => {
if (err) {
log.warn(err)
return
}
})
port.pipe(parser)
io.on('connection', socket => {
log.info('connected')
port.on('open', () => log.info('Port is open'))
port.on('error', err => log.info(`Error: ${err.message}`))
parser.on('data', data => {
let currentBalanceData = 0
currentBalanceData = data.toString().trim()
log.info(currentBalanceData)
socket.emit('newWeight', {weight: currentBalanceData})
})
})
}