实现多开并且串口数据处理后传递至渲染端
This commit is contained in:
89
src/main/api/NMEAHandler.js
Normal file
89
src/main/api/NMEAHandler.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import nmea from 'nmea'
|
||||
|
||||
let events = {}
|
||||
const on = (eventName, callback) => {
|
||||
if (!(eventName in events)) {
|
||||
events[eventName] = []
|
||||
}
|
||||
|
||||
events[eventName].push(callback)
|
||||
}
|
||||
|
||||
const emit = (eventName, ...args) => {
|
||||
if (!(eventName in events)) {
|
||||
return
|
||||
}
|
||||
|
||||
const cbs = events[eventName]
|
||||
|
||||
cbs.forEach(cb => {
|
||||
cb.call(this, ...args)
|
||||
});
|
||||
}
|
||||
|
||||
const handle = (value) => {
|
||||
if (value.length <= 0) return
|
||||
|
||||
emit('received', value)
|
||||
|
||||
const strArr = value.split('\r\n')
|
||||
for (let i = 0; i < strArr.length; i++) {
|
||||
const element = strArr[i];
|
||||
doHandle(element)
|
||||
}
|
||||
|
||||
emit('handled', nmeaObj)
|
||||
nmeaObj = resetNMEAObj()
|
||||
}
|
||||
|
||||
const doHandle = (value) => {
|
||||
if (!verify(value)) {
|
||||
return
|
||||
}
|
||||
|
||||
const result = nmea.parse(value)
|
||||
switch (result.sentence) {
|
||||
case 'RMC':
|
||||
nmeaObj.RMC = result
|
||||
break;
|
||||
case 'GGA':
|
||||
nmeaObj.GGA = result
|
||||
break;
|
||||
case 'GSA':
|
||||
if (result.talker_id === 'BD') nmeaObj.GSA.push(result)
|
||||
break;
|
||||
case 'GSV':
|
||||
if (result.talker_id === 'BD') nmeaObj.GSV.push(result)
|
||||
break;
|
||||
case 'TXT':
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const verify = (value) => {
|
||||
if (!value) {
|
||||
return false
|
||||
}
|
||||
|
||||
const dollar_index = value.indexOf('$')
|
||||
const star_index = value.indexOf('*')
|
||||
if (value.indexOf('$') !== 0 || star_index <= dollar_index) {
|
||||
return false
|
||||
}
|
||||
|
||||
const [sentence, checksum, ] = value.split('*')
|
||||
return nmea.Helpers.verifyChecksum(sentence, checksum)
|
||||
}
|
||||
|
||||
const resetNMEAObj = () => {
|
||||
return {
|
||||
'RMC': null,
|
||||
'GGA': null,
|
||||
'GSA': [],
|
||||
'GSV': []
|
||||
}
|
||||
}
|
||||
|
||||
let nmeaObj = resetNMEAObj()
|
||||
|
||||
module.exports = { on, handle }
|
||||
Reference in New Issue
Block a user