-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
181 lines (162 loc) · 4.65 KB
/
run.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
#!/usr/bin/env node
// yargs
const argv = require('yargs')(process.argv.slice(2))
// help text
.alias('h', 'help')
.help('help')
.usage('Usage: $0 -p [tty]')
// tty port
.option('p', {
alias : 'port',
describe: 'tty port',
type: 'string',
nargs: 1,
demand: true,
demand: 'tty port is required',
//default: '/dev/ttyACM0',
requiresArg:true
})
.option('t', {
alias : 'timestamp',
describe: 'show timestamp',
nargs: 0,
//default: false,
requiresArg: false
})
.option('v', {
alias : 'verbose',
describe: 'show verbose',
count: true,
nargs: 0,
//default: false,
requiresArg: false
})
.option('m', {
alias : 'mqtt',
describe: 'mqtt server address',
type: 'string',
nargs: 1,
default: 'localhost',
requiresArg: true
})
.option('l', {
alias : 'location',
describe: 'reader location',
type: 'string',
nargs: 1,
default: 'GDL',
requiresArg: true
}).argv
// configuration
var {tags, readerLocation, mqttAddress} = require('./env')
readerLocation = argv.location || readerLocation
const tty = argv.port || '/dev/ttyACM0'
mqttAddress = argv.mqtt || mqttAddress
const showTimestamp = (argv.timestamp ? true : false)
// verbose
VERBOSE_LEVEL = argv.verbose;
function WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }
function INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }
function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }
// dayjs
const dayjs = require('dayjs')
// fs
const fs = require("fs")
fs.access(tty, (err) => {
if (err) {
console.log('error', err)
process.exit(1)
}
})
// serial
const SerialPort = require('serialport')
const Delimiter = require('@serialport/parser-delimiter')
const port = new SerialPort(tty, { baudRate: 9600, highWaterMark: 1 })
const parser_sp = port.pipe(new Delimiter({ delimiter: '\x03' }))
// mqtt
const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://' + mqttAddress)
// keepalive
var lastMsgDate = new Date()
// start msg
console.log(getTime() + 'node-rdm6300-reader starting ...')
console.log(getTime() + 'tty: '+ tty + ', mqtt: ' + mqttAddress + ', location: ' + readerLocation)
// serialport
port.on('open', () => {
console.log(getTime() + 'serial port opened')
//setTimeout(keepAlive, 1 * 60 * 1000)
})
port.on('close', () => {
console.log(getTime() + 'serial port closed')
})
port.on('pause', () => {
INFO(getTime() + 'serial port paused 10s')
setTimeout(function(){
INFO(getTime() + 'serial port resumed')
port.flush()
port.resume()
}, 10000)
})
port.on('error', (err) => {
console.log(getTime() + 'serial port error')
console.log('error', err)
//reConnect()
})
parser_sp.on('data', data =>{
DEBUG(getTime() + 'data: ' + JSON.stringify(data))
let buf = data
data = data.toString("utf-8")
DEBUG(getTime() + 'data utf8: ' + JSON.stringify(data))
data = data.replace(/(\r\n|\n|\r|\x02|\x03)/gm,"").trim()
if (buf.indexOf('\x02') == 0 && data.length == 12){
DEBUG(getTime() + 'data: ok')
let data_json = new Object()
//data_json.tag = tags[data.toString()]
data_json.key = data.toString()
data_json.location = readerLocation
if (tags[data.toString()]){
console.log(getTime() + 'Welcome ' + tags[data.toString()])
//client.publish('rfid/' + data_json.location + '/json', JSON.stringify(data_json))
data_json.tag = tags[data.toString()]
client.publish('rfid/json', JSON.stringify(data_json))
} else {
console.log(getTime() + 'Denied ' + data.toString())
client.publish('rfid/json', JSON.stringify(data_json))
}
} else {
console.log(getTime() + 'Wrong data string ' + data.toString())
}
port.pause()
})
// check for connection errors or drops and reconnect
var reConnect = function () {
console.log('reconnecting ...')
port.close()
setTimeout(function(){
console.log('trying ...')
port = new SerialPort(tty, { baudRate: 9600 })
}, 5000)
}
function keepAlive() {
var keepAliveDate = new Date()
var FIVE_MIN= 5 * 60 * 1000
var TWO_MIN= 2 * 60 * 1000
var ONE_MIN= 2 * 60 * 1000
if((keepAliveDate - new Date(lastMsgDate)) > FIVE_MIN) {
WARN(getTime() + 'timeout!')
DEBUG((keepAliveDate - new Date(lastMsgDate))/1000 + " seconds")
DEBUG(((keepAliveDate - new Date(lastMsgDate))/1000)*60 + " minutes")
port.close()
//reConnect()
} else {
DEBUG(getTime() + 'MARK')
}
setTimeout(keepAlive, 1 * 60 * 1000)
}
function getTime() {
if (showTimestamp) {
return dayjs().format('HH:mm:ss.SSS ')
} else {
return ''
}
}