diff --git a/package.json b/package.json index 5fb0431..a4f5ee4 100644 --- a/package.json +++ b/package.json @@ -13,11 +13,11 @@ }, "dependencies": { "echarts": "^5.2.2", - "echarts-china-cities-js": "^0.1.1", "echarts-gl": "^2.0.8", "echarts-liquidfill": "^3.1.0", "electron-store": "^8.0.1", - "serialport": "^9.2.5", + "nmea": "git+https://e.coding.net/hwasmart/beidou-satellite-data-monitor/node-nmea.git", + "serialport": "9.2.4", "vue": "^3.0.5", "vue-echarts": "^6.0.0" }, diff --git a/src/main/api/NMEAHandler.js b/src/main/api/NMEAHandler.js new file mode 100644 index 0000000..ce07966 --- /dev/null +++ b/src/main/api/NMEAHandler.js @@ -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 } \ No newline at end of file diff --git a/src/main/app.ts b/src/main/app.ts index 325c976..cc43046 100644 --- a/src/main/app.ts +++ b/src/main/app.ts @@ -2,36 +2,62 @@ import { app, BrowserWindow } from 'electron' import {join} from "path"; import './load-serialport' -function createWindow() { - const win = new BrowserWindow({ - width: 800, - height: 600, - webPreferences: { - nodeIntegration: true, - preload: join(__dirname, 'preload.js') - } - }) - if (process.env.NODE_ENV === 'development') { - win.loadURL('http://localhost:3000/') - win.webContents.openDevTools() - } else { - win.loadFile('dist/render/index.html') + +function createWindow(page: String) { + + const win = new BrowserWindow({ + fullscreen: true, + frame: false, + webPreferences: { + // webSecurity: false, + nodeIntegration: true, + preload: join(__dirname, 'preload.js') } + }) + if (process.env.NODE_ENV === 'development') { + win.loadURL(`http://localhost:3000/${page}.html`) + win.webContents.openDevTools() + } else { + win.loadFile(`dist/render/${page}.html`) + win.webContents.openDevTools() + } + // if (process.env.NODE_ENV === 'development') { + // win.loadURL(`http://localhost:3000/?page=${page}`) + // win.webContents.openDevTools() + // } else { + // win.loadFile(`dist/render/index.html?page=${page}`) + // // win.loadURL(`file://${__dirname}/dist/render/index.html?page=${page}`); + // win.webContents.openDevTools() + // } } -app.whenReady().then(createWindow) +function launch() { + + createWindow('satellite-data-monitor-platform') + createWindow('satellite-info-monitor-platform') + createWindow('satellite-signal-monitor-platform') + createWindow('satellite-state-monitor-platform') + + + // createWindow('DataMonitorPage') + // createWindow('InfoMonitorPage') + // createWindow('SignalMonitorPage') + // createWindow('StateMonitorPage') +} + +app.whenReady().then(launch) app.on('window-all-closed', () => { - if (process.platform !== 'darwin') { - app.quit() - } + if (process.platform !== 'darwin') { + app.quit() + } }) app.on('activate', () => { - if (BrowserWindow.getAllWindows().length === 0) { - createWindow() - } + if (BrowserWindow.getAllWindows().length === 0) { + launch() + } }) \ No newline at end of file diff --git a/src/main/load-serialport.js b/src/main/load-serialport.js index f23d7e1..6df4032 100644 --- a/src/main/load-serialport.js +++ b/src/main/load-serialport.js @@ -1,8 +1,11 @@ const { ipcMain } = require('electron') const SerialPort = require('serialport') -const Readline = require('@serialport/parser-readline') +// const Readline = require('@serialport/parser-readline') +const InterByteTimeout = require('@serialport/parser-inter-byte-timeout') const config = require('./config') +const NMEAHandler = require('./api/NMEAHandler') + const DEFAULT_BAUDRATE = 9600 const getSerialPortConstructorArguments = (ports) => { let serialport = config.get('serialport') @@ -26,9 +29,9 @@ SerialPort.list().then((ports, err) => { return } - console.log(ports, '=====================') + // console.log(ports, '=====================') - // 过滤到COM1口,很多电脑自带COM1接口 + // 过滤掉COM1口,很多电脑自带COM1接口 ports.filter((port) => port.path != 'COM1') if (ports.length == 0) { console.error('设备为连接') @@ -38,8 +41,15 @@ SerialPort.list().then((ports, err) => { try { const { path, baudRate } = getSerialPortConstructorArguments(ports) const serialPort = new SerialPort(path, { baudRate }) - const parser = serialPort.pipe(new Readline({ delimiter: '\r\n' })) - parser.on('data', (data) => { sendMsg('NMEA', data) }) + + // const parser = serialPort.pipe(new Readline({ delimiter: '\r\n' })) + const parser = serialPort.pipe(new InterByteTimeout({interval: 30})) + + NMEAHandler.on('handled' , (nmeaObj) => { + sendMsg('nmea', JSON.stringify(nmeaObj)) + }) + + parser.on('data', (data) => NMEAHandler.handle(data.toString())) } catch (error) { console.error(error); } diff --git a/src/render/page/DataMonitorPage.vue b/src/render/page/DataMonitorPage.vue index 48b33a2..1bc337f 100644 --- a/src/render/page/DataMonitorPage.vue +++ b/src/render/page/DataMonitorPage.vue @@ -33,6 +33,16 @@ import Clock from '../components/Clock.vue' import Compass from '../components/Compass.vue' import WaterLevel from '../components/WaterLevel.vue' import Map from '../components/Map.vue' + +import { onMounted } from 'vue' + +onMounted(() => { + if ('ipcRenderer' in window) { + ipcRenderer.receive('nmea', console.log) + + ipcRenderer.send('APP_MOUNTED') + } +})