123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227 |
- /* eslint-disable node/no-deprecated-api */
- /* eslint-disable no-undef */
- 'use strict'
- // import AsyncLock from 'async-lock';
- import {
- app,
- BrowserWindow,
- Menu
- } from 'electron'
- import '../renderer/store'
- const fs = require('fs')
- const ipcMain = require('electron').ipcMain
- // const SerialPort = require('serialport')
- // const SerialPort = require('serialport')
- const SerialPort = require('serialport')
- const WebSocketServer = require('ws').Server
- // 安装thrift
- const thrift = require('thrift')
- const printService = require('./HrUtilService.js')
- // const cp = require('child_process')
- const exec = require('child_process').exec
- const execFile = require('child_process').execFile
- const RingBuffer = require('ringbufferjs')
- const dgram = require('dgram')
- let server = dgram.createSocket('udp4')
- const cmd = require('node-cmd')
- // var lock = new AsyncLock();
- let ceshi = []
- // SerialPort.list().then(
- // ports => {
- // console.log(ports)
- // }
- // )
- //第一个指定用户
- const ring = new RingBuffer(1024)
- //第二个指定用户
- const ringTwo = new RingBuffer(1024)
- // const portName = 'COM4'
- let portName = 'COM1'
- let serialPort
- const smallPackageLen = 8
- const bigPackageLen = 36
- let working = false
- const minPackageLen = 11
- // 轴加速度
- //控制传输数据的开关
- let wssSwitch = true;
- // 温度
- let Temperature = 0
- let arrCom = []
- let mainWindow
- let loadingWindow
- //pyhton 定时器
- let python_time = null;
- //jar包启动定时器
- let jar_time = null;
- //设置访问路径
- //打包访问的路径
- // let serveUrl = "./resources/exe"
- // let serveUrlJar = '\\resources\\exe'
- //本地访问的路径
- let serveUrl = "./exe"
- let serveUrlJar = '\\exe'
- /**
- * Set `__static` path to static files in production
- * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
- */
- if (process.env.NODE_ENV !== 'development') {
- global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
- }
- // process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
- const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9085` : `file://${__dirname}/index.html`
- //const winURL = process.env.NODE_ENV === 'development' ? `https://www.baidu.com` : `file://${__dirname}/index.html`
- const loadingURL =
- process.env.NODE_ENV === "development" //加载loading.html页面地址
- ? require("path").join(__static, "loading.html")
- : `file://${__static}/loading.html`;
- function createLoadingWindow() { //加载页面窗口
- loadingWindow = new BrowserWindow({
- height: 300,
- useContentSize: true,
- width: 400,
- show: true,
- transparent: false,
- maximizable: false, //禁止双击放大
- frame: false, // 去掉顶部操作栏
- // allowRunningInsecureContent:true,
- webPreferences: {
- contextIsolation: false, // 设置此项为false后,才可在渲染进程中使用electron api
- nodeIntegration: true,
- partition: String(+new Date()) //每次都以隐私窗口打开
- }
- })
- loadingWindow.loadURL(loadingURL)
- Menu.setApplicationMenu(null)
- loadingWindow.on('closed', () => {
- loadingWindow = null
- })
- }
- // eslint-disable-next-line space-before-function-paren
- function createWindow() {
- /**
- * Initial window options
- */
- mainWindow = new BrowserWindow({
- height: 720,
- useContentSize: true,
- center: true,
- width: 1280,
- icon: require('path').join(__dirname, '/static/icon.ico'), // icon
- // webPreferences: {
- // contextIsolation: false, // 设置此项为false后,才可在渲染进程中使用electron api
- // nodeIntegration: true
- // }
- frame: false,
- webPreferences: {
- contextIsolation: false,
- nodeIntegration: true,
- // allowRendererProcessReuse: true
- // 禁用窗口同源策略
- webSecurity: false
- }
- })
- mainWindow.setMinimumSize(1280, 720)
- //mainWindow.loadURL(loadingURL)
- mainWindow.loadURL(winURL)
- mainWindow.setMenu(null)
- // mainWindow.webContents.openDevTools();
- mainWindow.on('closed', () => {
- mainWindow = null
- })
- }
- const gotTheLock = app.requestSingleInstanceLock()
- if (!gotTheLock) {
- app.quit()
- } else {
- app.on('second-instance', (event, commandLine, workingDirectory) => {
- // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
- if (loadingWindow) {
- if (loadingWindow.isMinimized()) loadingWindow.restore()
- loadingWindow.focus()
- loadingWindow.show()
- }
- if (mainWindow) {
- if (mainWindow.isMinimized()) mainWindow.restore()
- mainWindow.focus()
- mainWindow.show()
- }
- // if (mainWindow) {
- // if (mainWindow.isMinimized()) mainWindow.restore()
- // mainWindow.focus()
- // mainWindow.show()
- // }
- })
- }
- // app.on('ready', () => {
- // createLoadingWindow()
- // createWindow()
- // mainWindow.hide()
- // })
- app.on('ready', () => {
- createLoadingWindow()
- createWindow()
- mainWindow.hide()
- })
- ipcMain.on('close-loading-window', (e, res) => {
- if (res.isClose) {
- if (loadingWindow === null) {
- } else {
- loadingWindow.close()
- }
- mainWindow.show()
- }
- })
- //
- // ipcMain.on('window-close', function () {
- // if (process.platform !== 'darwin') {
- // wssSwitch = false
- // cmd.runSync('taskkill /f /im javaw.exe')
- // app.quit()
- // }
- // // mainWindow.close();
- // })
- // app.on('activate', () => {
- // if (mainWindow === null) {
- // createWindow()
- // }
- // })
- // setTimeout(() => {
- // console.log("123")
- // console.log(loadingWindow)
- // loadingWindow.close()
- // mainWindow.show()
- // }, 4500);
- // 打开websocket服务
- let ws_public = null;
- // 查询54378 端口是否开了,开了的话就不调用了
- let pluseStart = cmd.runSync('netstat -ano|findstr 54378')
- // 没开的话就调用//防止二次点击时报错
- if (pluseStart.data === null || pluseStart.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- const wss = new WebSocketServer({
- port: 54378
- })
- wss.on('connection', (ws) => {
- // 有客户端连接时, 打印一条日志
- // 并且创建'message'监听
- ws.on('message', (message) => {
- // 直接将消息打印出来
- // console.log(message)
- if (wssSwitch) {
- mainWindow.webContents.send('asynchronous-message', message)
- }
- })
- ws_public = ws
- // setTimeout(() => {
- //
- // }, 1000);
- // setInterval(()=>{
- // ws.send('PULSE-STOP')
- // },1000)
- })
- }
- ipcMain.on('pulse-message-stop', function (event, arg) {
- // console.log(arg) // prints "ping"
- ws_public.send('PULSE-STOP')
- // 返回值后向渲染进程传输数据
- // eslint-disable-next-line eol-last
- })
- // sendStop(
- // ws.send('PULSE-STOP')
- // )
- // 测试虚拟AR数据
- server.on('error', (err) => {
- console.log(err)
- })
- server.on('listening', () => {
- console.log('socket正在监听...')
- })
- server.on('message', (msg, rinfo) => {
- let buf = msg
- let bufto = buf.toString()
- // server.send(`welcome ${rinfo.address}:${rinfo.port}`, rinfo.port, rinfo.address)
- // 将指令发送到渲染进程,渲染进程保持一个全局状态
- mainWindow.webContents.send('send-message-to-render-vr', bufto)
- // mainWindow.webContents.on('did-finish-load', () => {
- // mainWindow.webContents.send('send-message-to-render-vr', bufto)
- // })
- })
- server.bind(4960)
- // 加速度代码
- // 创建
- // 创建
- // creatSerialPort()
- // serialPortOpen(serialPort)
- // 向渲染进程发送数据
- // //写方法把a传过去到渲染进程
- // function accelerationData() {
- // }
- // 在缓冲数据中找到第一个包的起始位置
- // eslint-disable-next-line space-before-function-paren
- function getComList() {
- arrCom = []
- // eslint-disable-next-line padded-blocks
- SerialPort.list().then(ports => {
- ports.forEach(function (port) {
- // console.log(port.path)
- // 获取到com端口后将数据告诉主进程
- arrCom.push(port.path)
- console.log(arrCom)
- // 等待渲染进程执行完成才往渲染进程发送数据
- })
- mainWindow.webContents.send('send-message-to-render-test', arrCom)
- })
- }
- //启动后再进行初始化
- const host = '0.0.0.0';
- const port = 50300;
- var connection = null;
- var client = null;
- // 启动操作
- // const starServer = function () {
- // let syncData = cmd.runSync('netstat -ano|findstr 50300')
- // if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- // let cp = require('child_process')
- // //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
- // // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
- // console.log(process.cwd() + serveUrlJar)
- // let path = process.cwd() + serveUrlJar;
- // cp.spawn('start', ['/b', 'rpc_thrift.exe'], { cwd: path, shell: true, windowsHide: true })
- // do {
- // syncData = cmd.runSync('netstat -ano|findstr 50300')
- // // console.log('find')
- // } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
- // console.log('OK')
- // } else {
- // console.log('50300')
- // }
- // loadingWindow.webContents.send('main-windows-python-exe-run', {
- // type: "exe",
- // })
- // connection = thrift.createConnection(host, port, 0)
- // client = thrift.createClient(printService, connection)
- // connection.on('error', function (e) {
- // console.log(e)
- // })
- // }
- //启动pyhton 程序优化
- function starServer() {
- let syncData = cmd.runSync('netstat -ano|findstr 50300')
- //判断该程序是否是起来了
- //假如没有起来
- if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- ///需要调用启动服务的命令
- let cp = require('child_process')
- let path = process.cwd() + serveUrlJar;
- cp.spawn('start', ['/b', 'rpc_thrift.exe'], { cwd: path, shell: true, windowsHide: true })
- //创造个定时器 固定判断一直判断服务是否已经起来了
- //没1秒判断一次 如果已经起来了就启动下一个服务
- python_time = setInterval(() => {
- syncData = cmd.runSync('netstat -ano|findstr 50300')
- //假如服务不存在就定时器一直循环
- if (!(syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)) {
- //
- //假如服务存在就不循环服务了--清除掉定时器
- clearInterval(python_time)
- //开始执行下一个启动命令
- //服务存在后就应该进行后续的启动了
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "exe",
- })
- connection = thrift.createConnection(host, port, 0)
- client = thrift.createClient(printService, connection)
- connection.on('error', function (e) {
- console.log(e)
- })
- }
- }, 1000)
- } else {
- console.log('50300')
- //在此时证明该服务已存在
- //服务存在后就应该进行后续的启动了
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "exe",
- })
- connection = thrift.createConnection(host, port, 0)
- client = thrift.createClient(printService, connection)
- connection.on('error', function (e) {
- console.log(e)
- })
- }
- }
- const startJiZhantoothServer = function () {
- let syncData = cmd.runSync('netstat -ano|findstr 1883')
- if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- let cp = require('child_process')
- //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
- // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
- let path = process.cwd() + serveUrlJar + '/ct1/emqx-5.3.2-windows-amd64/bin';
- // cp.spawn('start', ['/b', 'blueMain.exe'], { cwd: path, shell: true, windowsHide: true })
- cp.spawn('emqx start', ['/b'], { cwd: path, shell: true, windowsHide: true })
- do {
- syncData = cmd.runSync('netstat -ano|findstr 1883')
- // console.log('find')
- } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
- console.log('OK')
- } else {
- console.log('1883')
- }
- }
- startJiZhantoothServer();
- const startBluetoothServer = function () {
- let syncData = cmd.runSync('netstat -ano|findstr 8000')
- if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- let cp = require('child_process')
- //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
- // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
- let path = process.cwd() + serveUrlJar + '/ct1';
- cp.spawn('start', ['/b', 'blueMain.exe'], { cwd: path, shell: true, windowsHide: true })
- do {
- syncData = cmd.runSync('netstat -ano|findstr 8000')
- // console.log('find')
- } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
- console.log('OK')
- } else {
- console.log('8000')
- }
- }
- //增加pyhton中解析心电代码
- //用户1的心电处理方法
- // 心电数据处理新方法
- ipcMain.on('asynchronoushrv-filter_ecg', function (event, arg) {
- // prints "ping"
- let newData = {
- data1: {},
- data2: {}
- }
- let rawData = arg
- client.get_hrv_features_ecg(rawData, 100, function (err, res) {
- if (err) {
- console.log(err)
- } else {
- newData.data1 = res;
- console.log(newData.data1);
- // mainWindow.webContents.send('ecg_data', newData);
- }
- })
- client.get_hrv_features_ecg2(rawData, 100, function (err, res) {
- if (err) {
- console.log(err)
- console.log("算法解析错误")
- mainWindow.webContents.send('ecg_data', "");
- } else {
- newData.data2 = res;
- mainWindow.webContents.send('ecg_data', newData);
- }
- })
- })
- //用户2的心电处理方法
- // 心电数据处理新方法
- ipcMain.on('asynchronoushrv-filter_two_ecg', function (event, arg) {
- // prints "ping"
- let newData = {
- data1: {},
- data2: {}
- }
- let rawData = arg
- client.get_hrv_features_ecg(rawData, 100, function (err, res) {
- if (err) {
- console.log(err)
- } else {
- newData.data1 = res;
- console.log(newData.data1);
- // mainWindow.webContents.send('ecg_data', newData);
- }
- })
- client.get_hrv_features_ecg2(rawData, 100, function (err, res) {
- if (err) {
- console.log(err)
- console.log("算法解析错误")
- mainWindow.webContents.send('ecg_two_data', "");
- } else {
- newData.data2 = res;
- mainWindow.webContents.send('ecg_two_data', newData);
- }
- })
- })
- // 配置thrift的connection信息
- // eslint-disable-next-line no-undef
- //-----------------------------------
- // const host = '0.0.0.0'
- // const port = 50300
- // var connection = thrift.createConnection(host, port, 0)
- // // 根据配置的connection创建client
- // var client = thrift.createClient(printService, connection)
- // 3秒以后执行这个方法块
- //----------------------------------------
- // connection =
- // client = thrift.createClient(printService, connection)
- //监听connection是否error
- const rawData = [33145, 36804, 36755, 36832, 36907, 36963, 36986, 37078, 37100, 37098, 37198, 37166, 37194, 37189, 37220, 37271, 37313, 37303, 37364, 37368, 37443
- ]
- let aa = ''
- ipcMain.on('asynchronoushrv-message', function (event, arg) {
- // console.log(arg) // prints "ping"
- let rawData = arg
- client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
- if (err) {
- console.log(err)
- }
- console.log(res)
- aa = res
- // eslint-disable-next-line eol-last
- mainWindow.webContents.send('send-message-to-render-hrv', aa)
- })
- // 返回值后向渲染进程传输数据
- // eslint-disable-next-line eol-last
- })
- let pluseAll = ''
- ipcMain.on('asynchronoushrv-message-pluseAll', function (event, arg) {
- // console.log(arg) // prints "ping"
- let rawData = arg
- client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
- if (err) {
- console.log(err)
- }
- console.log(res)
- pluseAll = res
- // eslint-disable-next-line eol-last
- mainWindow.webContents.send('send-message-to-render-hrv-pluseAll', pluseAll)
- })
- // 返回值后向渲染进程传输数据
- // eslint-disable-next-line eol-last
- })
- //测试脉搏时传输的数据
- ipcMain.on('asynchronoushrv-message-pluse-realtime', function (event, arg) {
- let pluse = ''
- // console.log(arg) // prints "ping"
- let rawData = arg
- client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
- if (err) {
- console.log(err)
- }
- console.log(res)
- pluse = res
- // eslint-disable-next-line eol-last
- mainWindow.webContents.send('send-message-to-render-hrv-realtime', pluse)
- })
- // 返回值后向渲染进程传输数据
- // eslint-disable-next-line eol-last
- })
- // 得到发来的指令 然后传输数据
- let bb = ''
- ipcMain.on('asynchronoushrv-filter_signal', function (event, arg) {
- console.warn('滤波返回事件')
- let rawData = arg
- client.filter_signal(rawData, [0.5, 4.5], 'bandpass', 100, 3, false, function (err, res) {
- if (err) {
- console.log(err)
- }
- bb = res
- console.log('滤波返回事件')
- mainWindow.webContents.send('send-message-to-render-pluse', bb)
- })
- })
- ipcMain.on('window-min', function () {
- mainWindow.minimize();
- })
- ipcMain.on('window-max', function () {
- if (mainWindow.isMaximized()) {
- mainWindow.restore();
- } else {
- mainWindow.maximize();
- }
- })
- //退出程序
- function stopPid(syncData) {
- const cmd = require("node-cmd");
- let a = syncData.data.split("TCP");
- let b = a[1].split("LISTENING");
- let pid = b[1].trim();
- console.log("taskkill -pid " + pid + " -f");
- cmd.runSync("taskkill -pid " + pid + " -f");
- }
- ipcMain.on('window-close', function () {
- if (process.platform !== 'darwin') {
- //退出时关闭端口50300
- let syncData = cmd.runSync("netstat -ano|findstr 50300");
- //在的话判断是不是已经启动了是不是listening
- if (syncData.data != null) {
- if (syncData.data.includes("LISTENING")) {
- stopPid(syncData)
- }
- }
- let syncBlueData = cmd.runSync("netstat -ano|findstr 8000");
- //在的话判断是不是已经启动了是不是listening
- if (syncBlueData.data != null) {
- if (syncBlueData.data.includes("LISTENING")) {
- stopPid(syncBlueData)
- }
- }
- let syncJzData = cmd.runSync("netstat -ano|findstr 1883");
- //在的话判断是不是已经启动了是不是listening
- if (syncJzData.data != null) {
- if (syncJzData.data.includes("LISTENING")) {
- stopPid(syncJzData)
- }
- }
- wssSwitch = false
- cmd.runSync('taskkill /f /im javaw.exe')
- app.quit()
- }
- // mainWindow.close();
- })
- // 获取到com端口
- // eslint-disable-next-line space-before-function-paren
- //
- ipcMain.on('asynchronoushrv-listcom', function (event, arg) {
- console.log(arg)
- // console.log(arg) // prints "ping"
- console.log('来通知主进程了')
- if (arg == 'aa') {
- getComList()
- } else {
- portName = arg
- console.log('串口选择发过来了')
- // creatSerialPort()
- openEEG(creatEEG())
- }
- })
- //第一个用户传输的入库
- ipcMain.on('asynchronoushrv-naoData', function (event, arg) {
- let data = arg.value;
- let mac = arg.mac;
- let buf = Buffer.from(data, 'hex')
- // let buf= hex2ArrayBuffer(data);
- // var array = ['1a', '2b', '3c', '4d', '5e', '6f']
- // var hex_array = array.map(el => parseInt(el, 16))
- // var uarray = new Uint8Array(hex_array)
- // var buf = Buffer.from(data)
- console.log(buf)
- // console.log('来通知主进程了')
- //进到这里边
- buf.forEach(q => {
- ring.enq(q)
- handleByte(mac)
- })
- })
- //第二个用户传输的入库
- ipcMain.on('asynchronoushrv-naoTwoData', function (event, arg) {
- let data = arg.value;
- let mac = arg.mac;
- let buf = Buffer.from(data, 'hex')
- // let buf= hex2ArrayBuffer(data);
- // var array = ['1a', '2b', '3c', '4d', '5e', '6f']
- // var hex_array = array.map(el => parseInt(el, 16))
- // var uarray = new Uint8Array(hex_array)
- // var buf = Buffer.from(data)
- console.log(buf)
- // console.log('来通知主进程了')
- //进到这里边
- buf.forEach(q => {
- ringTwo.enq(q)
- handleByteTwo(mac)
- })
- })
- // function hex2ArrayBuffer(hex_str) {
- // // let hex_str = 'AA5504B10000B5'
- // let typedArray = new Uint8Array(hex_str.match(/[\da-f]{2}/gi).map(function (h) {
- // return parseInt(h, 16)
- // }))
- // let buffer = typedArray.buffer
- // return buffer
- // }
- // ipcMain.on('asynchronousselect-message123', function (event, arg) {
- // // console.log(arg) // prints "ping"
- // console.log("串口选择发过来了");
- // portName = arg
- // creatSerialPort()
- // serialPortOpen(serialPort)
- // // 回应同步消息
- // })
- function creatEEG() {
- // 创建
- if (serialPort) {
- if (serialPort.isOpen) {
- serialPort.close()
- }
- }
- serialPort = new SerialPort(portName, {
- // 波特率
- baudRate: 115200,
- autoOpen: false,
- dataBits: 8, // 数据位
- parity: 'none', // 奇偶校验
- stopBits: 1, // 停止位
- flowControl: false
- }, false)
- return serialPort
- }
- function openEEG(serialPort) {
- serialPort.open(function (error) {
- if (error) {
- console.log('打开端口' + portName + '错误' + error)
- } else {
- console.log('打开端口成功,正在监听数据中')
- serialPort.on('data', function (data) {
- // console.log(data)
- data.forEach(q => {
- ring.enq(q)
- handleByte(q)
- })
- })
- }
- })
- }
- function handleByte(mac) {
- let parseList
- if (ring.size() >= bigPackageLen) {
- const arr = ring.peekN(3)
- if (arr[0] !== 0xaa || arr[1] !== 0xaa) {
- ring.deq()
- } else {
- if (arr[2] === 0x04) {
- parseList = ring.deqN(smallPackageLen)
- } else {
- parseList = ring.deqN(bigPackageLen)
- }
- const temp = new Buffer(parseList)
- decodeByte(temp, mac)
- // console.log(parseList)
- }
- }
- }
- //第二个用户传输的得到的
- function handleByteTwo(mac) {
- let parseList
- if (ringTwo.size() >= bigPackageLen) {
- const arr = ringTwo.peekN(3)
- if (arr[0] !== 0xaa || arr[1] !== 0xaa) {
- ringTwo.deq()
- } else {
- if (arr[2] === 0x04) {
- parseList = ringTwo.deqN(smallPackageLen)
- } else {
- parseList = ringTwo.deqN(bigPackageLen)
- }
- const temp = new Buffer(parseList)
- decodeByteTwo(temp, mac)
- // console.log(parseList)
- }
- }
- }
- function decodeByte(temp, mac) {
- const utcTime = getUtcTime()
- // console.log(temp)
- const map = new Map()
- // map.set("utcTime",utcTime)
- // 判断大小包
- if (temp.length === smallPackageLen) { // 先校验再计算rawWaveData
- if (byteSumCheck(temp)) {
- const rawWaveData = getRawWaveValue(temp[5], temp[6])
- map.set('rawWaveData', rawWaveData)
- }
- } else {
- if (byteSumCheck(temp)) {
- // console.log(temp)
- map.set('Signal', temp.readUInt8(4))
- map.set('Delta', temp.readUInt8(7) * 256 * 256 + temp.readUInt8(8) * 256 + temp.readUInt8(9))
- map.set('Theta', temp.readUInt8(10) * 256 * 256 + temp.readUInt8(11) * 256 + temp.readUInt8(12))
- map.set('LowAlpha', temp.readUInt8(13) * 256 * 256 + temp.readUInt8(14) * 256 + temp.readUInt8(15))
- map.set('HighAlpha', temp.readUInt8(16) * 256 * 256 + temp.readUInt8(17) * 256 + temp.readUInt8(18))
- map.set('LowBeta', temp.readUInt8(19) * 256 * 256 + temp.readUInt8(20) * 256 + temp.readUInt8(21))
- map.set('HighBeta', temp.readUInt8(22) * 256 * 256 + temp.readUInt8(23) * 256 + temp.readUInt8(24))
- map.set('LowGamma', temp.readUInt8(25) * 256 * 256 + temp.readUInt8(26) * 256 + temp.readUInt8(27))
- map.set('MiddleGamma', temp.readUInt8(28) * 256 * 256 + temp.readUInt8(29) * 256 + temp.readUInt8(30))
- map.set('Attention', temp.readUInt8(32))
- map.set('Meditation', temp.readUInt8(34))
- }
- }
- // console.log(map.size)
- for (const [key, value] of map) {
- // console.log(key + ' ============ ' + value)
- }
- mainWindow.webContents.send('accelerationData-message', { arg: map, mac: mac })
- return map
- }
- function decodeByteTwo(temp, mac) {
- const utcTime = getUtcTime()
- // console.log(temp)
- const map = new Map()
- // map.set("utcTime",utcTime)
- // 判断大小包
- if (temp.length === smallPackageLen) { // 先校验再计算rawWaveData
- if (byteSumCheckTwo(temp)) {
- const rawWaveData = getRawWaveValueTwo(temp[5], temp[6])
- map.set('rawWaveData', rawWaveData)
- }
- } else {
- if (byteSumCheckTwo(temp)) {
- // console.log(temp)
- map.set('Signal', temp.readUInt8(4))
- map.set('Delta', temp.readUInt8(7) * 256 * 256 + temp.readUInt8(8) * 256 + temp.readUInt8(9))
- map.set('Theta', temp.readUInt8(10) * 256 * 256 + temp.readUInt8(11) * 256 + temp.readUInt8(12))
- map.set('LowAlpha', temp.readUInt8(13) * 256 * 256 + temp.readUInt8(14) * 256 + temp.readUInt8(15))
- map.set('HighAlpha', temp.readUInt8(16) * 256 * 256 + temp.readUInt8(17) * 256 + temp.readUInt8(18))
- map.set('LowBeta', temp.readUInt8(19) * 256 * 256 + temp.readUInt8(20) * 256 + temp.readUInt8(21))
- map.set('HighBeta', temp.readUInt8(22) * 256 * 256 + temp.readUInt8(23) * 256 + temp.readUInt8(24))
- map.set('LowGamma', temp.readUInt8(25) * 256 * 256 + temp.readUInt8(26) * 256 + temp.readUInt8(27))
- map.set('MiddleGamma', temp.readUInt8(28) * 256 * 256 + temp.readUInt8(29) * 256 + temp.readUInt8(30))
- map.set('Attention', temp.readUInt8(32))
- map.set('Meditation', temp.readUInt8(34))
- }
- }
- // console.log(map.size)
- for (const [key, value] of map) {
- // console.log(key + ' ============ ' + value)
- }
- mainWindow.webContents.send('accelerationData-message-two', { arg: map, mac: mac })
- return map
- }
- function getRawWaveValue(highOrderByte, lowOrderByte) {
- const hi = highOrderByte
- const lo = lowOrderByte & 0xFF
- let value = (hi << 8) | lo
- if (value > 32768) {
- value = value - 65536
- }
- return value
- }
- function getRawWaveValueTwo(highOrderByte, lowOrderByte) {
- const hi = highOrderByte
- const lo = lowOrderByte & 0xFF
- let value = (hi << 8) | lo
- if (value > 32768) {
- value = value - 65536
- }
- return value
- }
- // 检查校验和aa aa 04 80 02 ff f8 86sum = ((0x80 + 0x02 + xxHigh + xxLow)^ 0xFFFFFFFF) & 0xFF
- function byteSumCheck(byteTemp) {
- if (byteTemp.length === smallPackageLen) {
- if ((((byteTemp[3] + byteTemp[4] + byteTemp[5] + byteTemp[6]) ^ 0xffffffff) & 0xff) === byteTemp[7]) {
- // console.log('sum check ok!');
- return true
- } else {
- // console.log('sum check false!');
- return false
- }
- } else {
- if (byteTemp[0] === 0xaa && byteTemp[1] === 0xaa && byteTemp[2] === 0x20 && byteTemp[3] === 0x02 &&
- byteTemp[5] === 0x83 && byteTemp[6] === 0x18 && byteTemp[31] === 0x04 && byteTemp[33] === 0x05) {
- return true
- } else {
- return false
- }
- }
- }
- // 检查校验和aa aa 04 80 02 ff f8 86sum = ((0x80 + 0x02 + xxHigh + xxLow)^ 0xFFFFFFFF) & 0xFF
- function byteSumCheckTwo(byteTemp) {
- if (byteTemp.length === smallPackageLen) {
- if ((((byteTemp[3] + byteTemp[4] + byteTemp[5] + byteTemp[6]) ^ 0xffffffff) & 0xff) === byteTemp[7]) {
- // console.log('sum check ok!');
- return true
- } else {
- // console.log('sum check false!');
- return false
- }
- } else {
- if (byteTemp[0] === 0xaa && byteTemp[1] === 0xaa && byteTemp[2] === 0x20 && byteTemp[3] === 0x02 &&
- byteTemp[5] === 0x83 && byteTemp[6] === 0x18 && byteTemp[31] === 0x04 && byteTemp[33] === 0x05) {
- return true
- } else {
- return false
- }
- }
- }
- // 获取到com端口
- SerialPort.list().then(ports => {
- ports.forEach(function (port) {
- console.log(port.path)
- // console.log(port.pnpId);
- })
- })
- // 获取到格林尼治时间
- function getUtcTime() {
- // 取本地时间
- let localtime = new Date()
- // 取本地毫秒数
- let localmesc = localtime.getTime()
- // 取本地时区与格林尼治所在时区的偏差毫秒数
- let localOffset = localtime.getTimezoneOffset() * 60000
- // 反推得到格林尼治时间
- let utc = localOffset + localmesc
- // 得到指定时区时间
- // let calctime = utc + (3600000 * offset);
- return utc
- };
- ipcMain.on('asynchronoushrv-controlpanel', function (event, arg) {
- // console.log(arg) // prints "ping"
- mainWindow.webContents.send('send-message-control', process.cwd())
- // 回应同步消息
- })
- //------------------------是否需要初始化数据库------------------------------//
- function dbInit() {
- let syncData = null;
- syncData = cmd.runSync('sc query ct')
- if (syncData.data == null) {
- //cmd.runSync('powershell.exe Start-Process -FilePath "./resources/exe/mysql/mysql-5.7.24-winx64/bin/mysql_init.bat" -WindowStyle Hidden -Verb runAs')
- let dbInitScript = 'powershell.exe Start-Process -FilePath "' + serveUrl + '/mysql/mysql-5.7.24-winx64/bin/mysql_init.bat" -WindowStyle Hidden -Verb runAs'
- cmd.runSync(dbInitScript)
- do {
- syncData = cmd.runSync('sc query ct')
- // console.log("数据库服务正在初始化")
- } while (syncData.data == null)
- } else {
- // console.log("数据库初始化完成")
- }
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "dbInit",
- })
- // console.log("数据库初始化完成")
- }
- // // 调用方法,同步执行,阻塞后续程序的执行;
- // sleep(2000)
- // console.log("数据库服务初始化完成")
- // //要设置启动项目时需要启动jar包,和nginx服务
- // //假如数据库已初始化完成
- // //则启动jar包
- function execute_path(cmd, id, path) {
- var exec = require("child_process").exec;
- // var iconv = request('iconv-lite')
- // var encoding ='cp936'
- var binaryEncoding = "utf-8";
- let _this = this;
- exec(cmd, { cwd: path, windowsHide: true }, function (error, stdout, stderr) {
- if (error) {
- //_this.judge_false(id);
- console.log(error);
- } else {
- console.log(id + ": success");
- }
- });
- }
- function execute(cmd, id) {
- var exec = require("child_process").exec;
- let _this = this;
- exec(cmd, function (error, stdout, stderr) {
- if (error) {
- console.log(error + "错误");
- //return stderr;
- } else {
- console.log("" + stdout.toString());
- //return stdout;
- }
- });
- }
- //------------------------需要启动数据库服务-------------------------//
- function startDb() {
- let syncDataDb = null;
- syncDataDb = cmd.runSync('sc query ct')
- if (!(syncDataDb.data.includes("RUNNING"))) {
- execute(
- "powershell -Command \"Start-Process cmd -Verb RunAs -ArgumentList '/c net start ct && command -argument'\"",
- 1
- );
- do {
- syncDataDb = cmd.runSync('sc query ct')
- } while (!(syncDataDb.data.includes("RUNNING")))
- } else {
- }
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "startDb",
- })
- }
- //startDb()
- // //数据库启动完成后
- //---------------------------需要启动jar包-------------------------//
- function startJar() {
- let syncDataJar = null;
- syncDataJar = cmd.runSync("netstat -ano|findstr 8088");
- //判断jar包是否启动
- //如果未启动就开始执行启动命令
- if (syncDataJar.data == null || syncDataJar.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
- execute_path(
- "start jre/bin/javaw -Dfile.encoding=utf-8 -jar confrontation-training-java-0.0.1-SNAPSHOT.jar",
- 2,
- process.cwd() +
- //"\\resources\\exe\\jar"
- //"\\exe\\jar"
- serveUrlJar + "\\jar"
- );
- //执行完成以后
- //需要开个定时器去监听是否是启动成功了
- jar_time = setInterval(() => {
- syncDataJar = cmd.runSync("netstat -ano|findstr 8088");
- if (!(syncDataJar.data == null || syncDataJar.data.trim().toUpperCase().indexOf('LISTENING') <= 0)) {
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "startJar",
- })
- console.log("后台服务已启动");
- clearInterval(jar_time)
- }
- }, 1000)
- } else {
- loadingWindow.webContents.send('main-windows-python-exe-run', {
- type: "startJar",
- })
- console.log("后台服务已启动");
- }
- }
- //startJar()
- //获取本机ip
- // function getNetworkIp() {
- // var os = require("os");
- // let needHost = "";
- // try {
- // // 获得网络接口列表
- // let network = os.networkInterfaces();
- // for (let item in network) {
- // let iface = network[item];
- // for (let i = 0; i < iface.length; i++) {
- // let alias = iface[i];
- // if (
- // alias.family === "IPv4" &&
- // alias.address !== "127.0.0.1" &&
- // !alias.internal
- // ) {
- // needHost = alias.address;
- // }
- // }
- // }
- // } catch (e) {
- // console.log(e);
- // }
- // return needHost;
- // }
- //获取本机ip 改变util文件夹中的global的ip
- //获取本机ip
- function getNetworkIp() {
- var os = require("os");
- let needHost = "";
- try {
- // 获得网络接口列表
- let network = os.networkInterfaces();
- for (let item in network) {
- let iface = network[item];
- for (let i = 0; i < iface.length; i++) {
- let alias = iface[i];
- if (
- alias.family === "IPv4" &&
- alias.address !== "127.0.0.1" &&
- !alias.internal
- ) {
- needHost = alias.address;
- }
- }
- }
- } catch (e) {
- console.log(e);
- }
- return needHost;
- }
- //调用方法,改动web服务global.js
- function webGlobal() {
- let ipv4 = getNetworkIp();
- console.log(ipv4)
- ipv4 = 'localhost'
- let urlPath = process.cwd() +
- serveUrlJar + "\\global.js"
- // let urlPath = __static + "\\global.js"
- let str = "var env = {dev: 'http://" +
- ipv4 +
- ":8088/',prod: 'http://" +
- ipv4 +
- ":8088/'};export default env;";
- console.log(urlPath)
- console.log(str)
- fs.writeFile(
- urlPath,
- str,
- (error) => {
- if (error) {
- // printInfo("PC写入global文件失败,原因是" + error.message, "warn");
- return console.log("写入global文件失败,原因是" + error.message);
- } else {
- // printInfo("PC写入global文件失败,原因是", "info");
- console.log("写入成功");
- }
- }
- );
- }
- webGlobal()
- //启动docker
- function startDocker() {
- execFile("C:/nlp/nlp" + '/start1.bat', function (err, data) {
- console.log(err)
- console.log(data.toString())
- })
- }
- // startDocker()
- //关闭docker
- //启动docker
- function stopDocker() {
- execFile("C:/nlp/nlp" + '/stop.bat', function (err, data) {
- console.log(err)
- console.log(data.toString())
- })
- }
- //stopDocker()
- //异步
- ipcMain.on('main-windows-python-exe', function (event, arg) {
- console.log(arg)
- console.log(arg.type)
- if (arg.type === 'exe') {
- console.log("app.vue传值了")
- //调用.exe
- starServer()
- }
- if (arg.type === "dbInit") {
- dbInit()
- }
- if (arg.type === 'startDb') {
- startDb()
- }
- if (arg.type === 'startJar') {
- startJar()
- startBluetoothServer();
- }
- if (arg.type === 'webGlobal') {
- //
- // webGlobal()
- }
- })
|