index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /* eslint-disable node/no-deprecated-api */
  2. /* eslint-disable no-undef */
  3. 'use strict'
  4. // import AsyncLock from 'async-lock';
  5. import {
  6. app,
  7. BrowserWindow,
  8. Menu
  9. } from 'electron'
  10. import '../renderer/store'
  11. const fs = require('fs')
  12. const ipcMain = require('electron').ipcMain
  13. // const SerialPort = require('serialport')
  14. // const SerialPort = require('serialport')
  15. const SerialPort = require('serialport')
  16. const WebSocketServer = require('ws').Server
  17. // 安装thrift
  18. const thrift = require('thrift')
  19. const printService = require('./HrUtilService.js')
  20. // const cp = require('child_process')
  21. const exec = require('child_process').exec
  22. const execFile = require('child_process').execFile
  23. const RingBuffer = require('ringbufferjs')
  24. const dgram = require('dgram')
  25. let server = dgram.createSocket('udp4')
  26. const cmd = require('node-cmd')
  27. // var lock = new AsyncLock();
  28. let ceshi = []
  29. // SerialPort.list().then(
  30. // ports => {
  31. // console.log(ports)
  32. // }
  33. // )
  34. //第一个指定用户
  35. const ring = new RingBuffer(1024)
  36. //第二个指定用户
  37. const ringTwo = new RingBuffer(1024)
  38. // const portName = 'COM4'
  39. let portName = 'COM1'
  40. let serialPort
  41. const smallPackageLen = 8
  42. const bigPackageLen = 36
  43. let working = false
  44. const minPackageLen = 11
  45. // 轴加速度
  46. //控制传输数据的开关
  47. let wssSwitch = true;
  48. // 温度
  49. let Temperature = 0
  50. let arrCom = []
  51. let mainWindow
  52. let loadingWindow
  53. //pyhton 定时器
  54. let python_time = null;
  55. //jar包启动定时器
  56. let jar_time = null;
  57. //设置访问路径
  58. //打包访问的路径
  59. // let serveUrl = "./resources/exe"
  60. // let serveUrlJar = '\\resources\\exe'
  61. //本地访问的路径
  62. let serveUrl = "./exe"
  63. let serveUrlJar = '\\exe'
  64. /**
  65. * Set `__static` path to static files in production
  66. * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
  67. */
  68. if (process.env.NODE_ENV !== 'development') {
  69. global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
  70. }
  71. // process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
  72. const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9085` : `file://${__dirname}/index.html`
  73. //const winURL = process.env.NODE_ENV === 'development' ? `https://www.baidu.com` : `file://${__dirname}/index.html`
  74. const loadingURL =
  75. process.env.NODE_ENV === "development" //加载loading.html页面地址
  76. ? require("path").join(__static, "loading.html")
  77. : `file://${__static}/loading.html`;
  78. function createLoadingWindow() { //加载页面窗口
  79. loadingWindow = new BrowserWindow({
  80. height: 300,
  81. useContentSize: true,
  82. width: 400,
  83. show: true,
  84. transparent: false,
  85. maximizable: false, //禁止双击放大
  86. frame: false, // 去掉顶部操作栏
  87. // allowRunningInsecureContent:true,
  88. webPreferences: {
  89. contextIsolation: false, // 设置此项为false后,才可在渲染进程中使用electron api
  90. nodeIntegration: true,
  91. partition: String(+new Date()) //每次都以隐私窗口打开
  92. }
  93. })
  94. loadingWindow.loadURL(loadingURL)
  95. Menu.setApplicationMenu(null)
  96. loadingWindow.on('closed', () => {
  97. loadingWindow = null
  98. })
  99. }
  100. // eslint-disable-next-line space-before-function-paren
  101. function createWindow() {
  102. /**
  103. * Initial window options
  104. */
  105. mainWindow = new BrowserWindow({
  106. height: 720,
  107. useContentSize: true,
  108. center: true,
  109. width: 1280,
  110. icon: require('path').join(__dirname, '/static/icon.ico'), // icon
  111. // webPreferences: {
  112. // contextIsolation: false, // 设置此项为false后,才可在渲染进程中使用electron api
  113. // nodeIntegration: true
  114. // }
  115. frame: false,
  116. webPreferences: {
  117. contextIsolation: false,
  118. nodeIntegration: true,
  119. // allowRendererProcessReuse: true
  120. // 禁用窗口同源策略
  121. webSecurity: false
  122. }
  123. })
  124. mainWindow.setMinimumSize(1280, 720)
  125. //mainWindow.loadURL(loadingURL)
  126. mainWindow.loadURL(winURL)
  127. mainWindow.setMenu(null)
  128. // mainWindow.webContents.openDevTools();
  129. mainWindow.on('closed', () => {
  130. mainWindow = null
  131. })
  132. }
  133. const gotTheLock = app.requestSingleInstanceLock()
  134. if (!gotTheLock) {
  135. app.quit()
  136. } else {
  137. app.on('second-instance', (event, commandLine, workingDirectory) => {
  138. // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
  139. if (loadingWindow) {
  140. if (loadingWindow.isMinimized()) loadingWindow.restore()
  141. loadingWindow.focus()
  142. loadingWindow.show()
  143. }
  144. if (mainWindow) {
  145. if (mainWindow.isMinimized()) mainWindow.restore()
  146. mainWindow.focus()
  147. mainWindow.show()
  148. }
  149. // if (mainWindow) {
  150. // if (mainWindow.isMinimized()) mainWindow.restore()
  151. // mainWindow.focus()
  152. // mainWindow.show()
  153. // }
  154. })
  155. }
  156. // app.on('ready', () => {
  157. // createLoadingWindow()
  158. // createWindow()
  159. // mainWindow.hide()
  160. // })
  161. app.on('ready', () => {
  162. createLoadingWindow()
  163. createWindow()
  164. mainWindow.hide()
  165. })
  166. ipcMain.on('close-loading-window', (e, res) => {
  167. if (res.isClose) {
  168. if (loadingWindow === null) {
  169. } else {
  170. loadingWindow.close()
  171. }
  172. mainWindow.show()
  173. }
  174. })
  175. //
  176. // ipcMain.on('window-close', function () {
  177. // if (process.platform !== 'darwin') {
  178. // wssSwitch = false
  179. // cmd.runSync('taskkill /f /im javaw.exe')
  180. // app.quit()
  181. // }
  182. // // mainWindow.close();
  183. // })
  184. // app.on('activate', () => {
  185. // if (mainWindow === null) {
  186. // createWindow()
  187. // }
  188. // })
  189. // setTimeout(() => {
  190. // console.log("123")
  191. // console.log(loadingWindow)
  192. // loadingWindow.close()
  193. // mainWindow.show()
  194. // }, 4500);
  195. // 打开websocket服务
  196. let ws_public = null;
  197. // 查询54378 端口是否开了,开了的话就不调用了
  198. let pluseStart = cmd.runSync('netstat -ano|findstr 54378')
  199. // 没开的话就调用//防止二次点击时报错
  200. if (pluseStart.data === null || pluseStart.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  201. const wss = new WebSocketServer({
  202. port: 54378
  203. })
  204. wss.on('connection', (ws) => {
  205. // 有客户端连接时, 打印一条日志
  206. // 并且创建'message'监听
  207. ws.on('message', (message) => {
  208. // 直接将消息打印出来
  209. // console.log(message)
  210. if (wssSwitch) {
  211. mainWindow.webContents.send('asynchronous-message', message)
  212. }
  213. })
  214. ws_public = ws
  215. // setTimeout(() => {
  216. //
  217. // }, 1000);
  218. // setInterval(()=>{
  219. // ws.send('PULSE-STOP')
  220. // },1000)
  221. })
  222. }
  223. ipcMain.on('pulse-message-stop', function (event, arg) {
  224. // console.log(arg) // prints "ping"
  225. ws_public.send('PULSE-STOP')
  226. // 返回值后向渲染进程传输数据
  227. // eslint-disable-next-line eol-last
  228. })
  229. // sendStop(
  230. // ws.send('PULSE-STOP')
  231. // )
  232. // 测试虚拟AR数据
  233. server.on('error', (err) => {
  234. console.log(err)
  235. })
  236. server.on('listening', () => {
  237. console.log('socket正在监听...')
  238. })
  239. server.on('message', (msg, rinfo) => {
  240. let buf = msg
  241. let bufto = buf.toString()
  242. // server.send(`welcome ${rinfo.address}:${rinfo.port}`, rinfo.port, rinfo.address)
  243. // 将指令发送到渲染进程,渲染进程保持一个全局状态
  244. mainWindow.webContents.send('send-message-to-render-vr', bufto)
  245. // mainWindow.webContents.on('did-finish-load', () => {
  246. // mainWindow.webContents.send('send-message-to-render-vr', bufto)
  247. // })
  248. })
  249. server.bind(4960)
  250. // 加速度代码
  251. // 创建
  252. // 创建
  253. // creatSerialPort()
  254. // serialPortOpen(serialPort)
  255. // 向渲染进程发送数据
  256. // //写方法把a传过去到渲染进程
  257. // function accelerationData() {
  258. // }
  259. // 在缓冲数据中找到第一个包的起始位置
  260. // eslint-disable-next-line space-before-function-paren
  261. function getComList() {
  262. arrCom = []
  263. // eslint-disable-next-line padded-blocks
  264. SerialPort.list().then(ports => {
  265. ports.forEach(function (port) {
  266. // console.log(port.path)
  267. // 获取到com端口后将数据告诉主进程
  268. arrCom.push(port.path)
  269. console.log(arrCom)
  270. // 等待渲染进程执行完成才往渲染进程发送数据
  271. })
  272. mainWindow.webContents.send('send-message-to-render-test', arrCom)
  273. })
  274. }
  275. //启动后再进行初始化
  276. const host = '0.0.0.0';
  277. const port = 50300;
  278. var connection = null;
  279. var client = null;
  280. // 启动操作
  281. // const starServer = function () {
  282. // let syncData = cmd.runSync('netstat -ano|findstr 50300')
  283. // if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  284. // let cp = require('child_process')
  285. // //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
  286. // // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
  287. // console.log(process.cwd() + serveUrlJar)
  288. // let path = process.cwd() + serveUrlJar;
  289. // cp.spawn('start', ['/b', 'rpc_thrift.exe'], { cwd: path, shell: true, windowsHide: true })
  290. // do {
  291. // syncData = cmd.runSync('netstat -ano|findstr 50300')
  292. // // console.log('find')
  293. // } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
  294. // console.log('OK')
  295. // } else {
  296. // console.log('50300')
  297. // }
  298. // loadingWindow.webContents.send('main-windows-python-exe-run', {
  299. // type: "exe",
  300. // })
  301. // connection = thrift.createConnection(host, port, 0)
  302. // client = thrift.createClient(printService, connection)
  303. // connection.on('error', function (e) {
  304. // console.log(e)
  305. // })
  306. // }
  307. //启动pyhton 程序优化
  308. function starServer() {
  309. let syncData = cmd.runSync('netstat -ano|findstr 50300')
  310. //判断该程序是否是起来了
  311. //假如没有起来
  312. if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  313. ///需要调用启动服务的命令
  314. let cp = require('child_process')
  315. let path = process.cwd() + serveUrlJar;
  316. cp.spawn('start', ['/b', 'rpc_thrift.exe'], { cwd: path, shell: true, windowsHide: true })
  317. //创造个定时器 固定判断一直判断服务是否已经起来了
  318. //没1秒判断一次 如果已经起来了就启动下一个服务
  319. python_time = setInterval(() => {
  320. syncData = cmd.runSync('netstat -ano|findstr 50300')
  321. //假如服务不存在就定时器一直循环
  322. if (!(syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)) {
  323. //
  324. //假如服务存在就不循环服务了--清除掉定时器
  325. clearInterval(python_time)
  326. //开始执行下一个启动命令
  327. //服务存在后就应该进行后续的启动了
  328. loadingWindow.webContents.send('main-windows-python-exe-run', {
  329. type: "exe",
  330. })
  331. connection = thrift.createConnection(host, port, 0)
  332. client = thrift.createClient(printService, connection)
  333. connection.on('error', function (e) {
  334. console.log(e)
  335. })
  336. }
  337. }, 1000)
  338. } else {
  339. console.log('50300')
  340. //在此时证明该服务已存在
  341. //服务存在后就应该进行后续的启动了
  342. loadingWindow.webContents.send('main-windows-python-exe-run', {
  343. type: "exe",
  344. })
  345. connection = thrift.createConnection(host, port, 0)
  346. client = thrift.createClient(printService, connection)
  347. connection.on('error', function (e) {
  348. console.log(e)
  349. })
  350. }
  351. }
  352. const startJiZhantoothServer = function () {
  353. let syncData = cmd.runSync('netstat -ano|findstr 1883')
  354. if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  355. let cp = require('child_process')
  356. //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
  357. // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
  358. let path = process.cwd() + serveUrlJar + '/ct1/emqx-5.3.2-windows-amd64/bin';
  359. // cp.spawn('start', ['/b', 'blueMain.exe'], { cwd: path, shell: true, windowsHide: true })
  360. cp.spawn('emqx start', ['/b'], { cwd: path, shell: true, windowsHide: true })
  361. do {
  362. syncData = cmd.runSync('netstat -ano|findstr 1883')
  363. // console.log('find')
  364. } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
  365. console.log('OK')
  366. } else {
  367. console.log('1883')
  368. }
  369. }
  370. startJiZhantoothServer();
  371. const startBluetoothServer = function () {
  372. let syncData = cmd.runSync('netstat -ano|findstr 8000')
  373. if (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  374. let cp = require('child_process')
  375. //execFile('./resources/exe/rpc_thrift.exe', function (err, data) {
  376. // cp.spawn(serveUrl +'/rpc_thrift.exe', [])
  377. let path = process.cwd() + serveUrlJar + '/ct1';
  378. cp.spawn('start', ['/b', 'blueMain.exe'], { cwd: path, shell: true, windowsHide: true })
  379. do {
  380. syncData = cmd.runSync('netstat -ano|findstr 8000')
  381. // console.log('find')
  382. } while (syncData.data === null || syncData.data.trim().toUpperCase().indexOf('LISTENING') <= 0)
  383. console.log('OK')
  384. } else {
  385. console.log('8000')
  386. }
  387. }
  388. //增加pyhton中解析心电代码
  389. //用户1的心电处理方法
  390. // 心电数据处理新方法
  391. ipcMain.on('asynchronoushrv-filter_ecg', function (event, arg) {
  392. // prints "ping"
  393. let newData = {
  394. data1: {},
  395. data2: {}
  396. }
  397. let rawData = arg
  398. client.get_hrv_features_ecg(rawData, 100, function (err, res) {
  399. if (err) {
  400. console.log(err)
  401. } else {
  402. newData.data1 = res;
  403. console.log(newData.data1);
  404. // mainWindow.webContents.send('ecg_data', newData);
  405. }
  406. })
  407. client.get_hrv_features_ecg2(rawData, 100, function (err, res) {
  408. if (err) {
  409. console.log(err)
  410. console.log("算法解析错误")
  411. mainWindow.webContents.send('ecg_data', "");
  412. } else {
  413. newData.data2 = res;
  414. mainWindow.webContents.send('ecg_data', newData);
  415. }
  416. })
  417. })
  418. //用户2的心电处理方法
  419. // 心电数据处理新方法
  420. ipcMain.on('asynchronoushrv-filter_two_ecg', function (event, arg) {
  421. // prints "ping"
  422. let newData = {
  423. data1: {},
  424. data2: {}
  425. }
  426. let rawData = arg
  427. client.get_hrv_features_ecg(rawData, 100, function (err, res) {
  428. if (err) {
  429. console.log(err)
  430. } else {
  431. newData.data1 = res;
  432. console.log(newData.data1);
  433. // mainWindow.webContents.send('ecg_data', newData);
  434. }
  435. })
  436. client.get_hrv_features_ecg2(rawData, 100, function (err, res) {
  437. if (err) {
  438. console.log(err)
  439. console.log("算法解析错误")
  440. mainWindow.webContents.send('ecg_two_data', "");
  441. } else {
  442. newData.data2 = res;
  443. mainWindow.webContents.send('ecg_two_data', newData);
  444. }
  445. })
  446. })
  447. // 配置thrift的connection信息
  448. // eslint-disable-next-line no-undef
  449. //-----------------------------------
  450. // const host = '0.0.0.0'
  451. // const port = 50300
  452. // var connection = thrift.createConnection(host, port, 0)
  453. // // 根据配置的connection创建client
  454. // var client = thrift.createClient(printService, connection)
  455. // 3秒以后执行这个方法块
  456. //----------------------------------------
  457. // connection =
  458. // client = thrift.createClient(printService, connection)
  459. //监听connection是否error
  460. const rawData = [33145, 36804, 36755, 36832, 36907, 36963, 36986, 37078, 37100, 37098, 37198, 37166, 37194, 37189, 37220, 37271, 37313, 37303, 37364, 37368, 37443
  461. ]
  462. let aa = ''
  463. ipcMain.on('asynchronoushrv-message', function (event, arg) {
  464. // console.log(arg) // prints "ping"
  465. let rawData = arg
  466. client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
  467. if (err) {
  468. console.log(err)
  469. }
  470. console.log(res)
  471. aa = res
  472. // eslint-disable-next-line eol-last
  473. mainWindow.webContents.send('send-message-to-render-hrv', aa)
  474. })
  475. // 返回值后向渲染进程传输数据
  476. // eslint-disable-next-line eol-last
  477. })
  478. let pluseAll = ''
  479. ipcMain.on('asynchronoushrv-message-pluseAll', function (event, arg) {
  480. // console.log(arg) // prints "ping"
  481. let rawData = arg
  482. client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
  483. if (err) {
  484. console.log(err)
  485. }
  486. console.log(res)
  487. pluseAll = res
  488. // eslint-disable-next-line eol-last
  489. mainWindow.webContents.send('send-message-to-render-hrv-pluseAll', pluseAll)
  490. })
  491. // 返回值后向渲染进程传输数据
  492. // eslint-disable-next-line eol-last
  493. })
  494. //测试脉搏时传输的数据
  495. ipcMain.on('asynchronoushrv-message-pluse-realtime', function (event, arg) {
  496. let pluse = ''
  497. // console.log(arg) // prints "ping"
  498. let rawData = arg
  499. client.get_hrv_features(rawData, 0.5, 4.5, 100, function (err, res) {
  500. if (err) {
  501. console.log(err)
  502. }
  503. console.log(res)
  504. pluse = res
  505. // eslint-disable-next-line eol-last
  506. mainWindow.webContents.send('send-message-to-render-hrv-realtime', pluse)
  507. })
  508. // 返回值后向渲染进程传输数据
  509. // eslint-disable-next-line eol-last
  510. })
  511. // 得到发来的指令 然后传输数据
  512. let bb = ''
  513. ipcMain.on('asynchronoushrv-filter_signal', function (event, arg) {
  514. console.warn('滤波返回事件')
  515. let rawData = arg
  516. client.filter_signal(rawData, [0.5, 4.5], 'bandpass', 100, 3, false, function (err, res) {
  517. if (err) {
  518. console.log(err)
  519. }
  520. bb = res
  521. console.log('滤波返回事件')
  522. mainWindow.webContents.send('send-message-to-render-pluse', bb)
  523. })
  524. })
  525. ipcMain.on('window-min', function () {
  526. mainWindow.minimize();
  527. })
  528. ipcMain.on('window-max', function () {
  529. if (mainWindow.isMaximized()) {
  530. mainWindow.restore();
  531. } else {
  532. mainWindow.maximize();
  533. }
  534. })
  535. //退出程序
  536. function stopPid(syncData) {
  537. const cmd = require("node-cmd");
  538. let a = syncData.data.split("TCP");
  539. let b = a[1].split("LISTENING");
  540. let pid = b[1].trim();
  541. console.log("taskkill -pid " + pid + " -f");
  542. cmd.runSync("taskkill -pid " + pid + " -f");
  543. }
  544. ipcMain.on('window-close', function () {
  545. if (process.platform !== 'darwin') {
  546. //退出时关闭端口50300
  547. let syncData = cmd.runSync("netstat -ano|findstr 50300");
  548. //在的话判断是不是已经启动了是不是listening
  549. if (syncData.data != null) {
  550. if (syncData.data.includes("LISTENING")) {
  551. stopPid(syncData)
  552. }
  553. }
  554. let syncBlueData = cmd.runSync("netstat -ano|findstr 8000");
  555. //在的话判断是不是已经启动了是不是listening
  556. if (syncBlueData.data != null) {
  557. if (syncBlueData.data.includes("LISTENING")) {
  558. stopPid(syncBlueData)
  559. }
  560. }
  561. let syncJzData = cmd.runSync("netstat -ano|findstr 1883");
  562. //在的话判断是不是已经启动了是不是listening
  563. if (syncJzData.data != null) {
  564. if (syncJzData.data.includes("LISTENING")) {
  565. stopPid(syncJzData)
  566. }
  567. }
  568. wssSwitch = false
  569. cmd.runSync('taskkill /f /im javaw.exe')
  570. app.quit()
  571. }
  572. // mainWindow.close();
  573. })
  574. // 获取到com端口
  575. // eslint-disable-next-line space-before-function-paren
  576. //
  577. ipcMain.on('asynchronoushrv-listcom', function (event, arg) {
  578. console.log(arg)
  579. // console.log(arg) // prints "ping"
  580. console.log('来通知主进程了')
  581. if (arg == 'aa') {
  582. getComList()
  583. } else {
  584. portName = arg
  585. console.log('串口选择发过来了')
  586. // creatSerialPort()
  587. openEEG(creatEEG())
  588. }
  589. })
  590. //第一个用户传输的入库
  591. ipcMain.on('asynchronoushrv-naoData', function (event, arg) {
  592. let data = arg.value;
  593. let mac = arg.mac;
  594. let buf = Buffer.from(data, 'hex')
  595. // let buf= hex2ArrayBuffer(data);
  596. // var array = ['1a', '2b', '3c', '4d', '5e', '6f']
  597. // var hex_array = array.map(el => parseInt(el, 16))
  598. // var uarray = new Uint8Array(hex_array)
  599. // var buf = Buffer.from(data)
  600. console.log(buf)
  601. // console.log('来通知主进程了')
  602. //进到这里边
  603. buf.forEach(q => {
  604. ring.enq(q)
  605. handleByte(mac)
  606. })
  607. })
  608. //第二个用户传输的入库
  609. ipcMain.on('asynchronoushrv-naoTwoData', function (event, arg) {
  610. let data = arg.value;
  611. let mac = arg.mac;
  612. let buf = Buffer.from(data, 'hex')
  613. // let buf= hex2ArrayBuffer(data);
  614. // var array = ['1a', '2b', '3c', '4d', '5e', '6f']
  615. // var hex_array = array.map(el => parseInt(el, 16))
  616. // var uarray = new Uint8Array(hex_array)
  617. // var buf = Buffer.from(data)
  618. console.log(buf)
  619. // console.log('来通知主进程了')
  620. //进到这里边
  621. buf.forEach(q => {
  622. ringTwo.enq(q)
  623. handleByteTwo(mac)
  624. })
  625. })
  626. // function hex2ArrayBuffer(hex_str) {
  627. // // let hex_str = 'AA5504B10000B5'
  628. // let typedArray = new Uint8Array(hex_str.match(/[\da-f]{2}/gi).map(function (h) {
  629. // return parseInt(h, 16)
  630. // }))
  631. // let buffer = typedArray.buffer
  632. // return buffer
  633. // }
  634. // ipcMain.on('asynchronousselect-message123', function (event, arg) {
  635. // // console.log(arg) // prints "ping"
  636. // console.log("串口选择发过来了");
  637. // portName = arg
  638. // creatSerialPort()
  639. // serialPortOpen(serialPort)
  640. // // 回应同步消息
  641. // })
  642. function creatEEG() {
  643. // 创建
  644. if (serialPort) {
  645. if (serialPort.isOpen) {
  646. serialPort.close()
  647. }
  648. }
  649. serialPort = new SerialPort(portName, {
  650. // 波特率
  651. baudRate: 115200,
  652. autoOpen: false,
  653. dataBits: 8, // 数据位
  654. parity: 'none', // 奇偶校验
  655. stopBits: 1, // 停止位
  656. flowControl: false
  657. }, false)
  658. return serialPort
  659. }
  660. function openEEG(serialPort) {
  661. serialPort.open(function (error) {
  662. if (error) {
  663. console.log('打开端口' + portName + '错误' + error)
  664. } else {
  665. console.log('打开端口成功,正在监听数据中')
  666. serialPort.on('data', function (data) {
  667. // console.log(data)
  668. data.forEach(q => {
  669. ring.enq(q)
  670. handleByte(q)
  671. })
  672. })
  673. }
  674. })
  675. }
  676. function handleByte(mac) {
  677. let parseList
  678. if (ring.size() >= bigPackageLen) {
  679. const arr = ring.peekN(3)
  680. if (arr[0] !== 0xaa || arr[1] !== 0xaa) {
  681. ring.deq()
  682. } else {
  683. if (arr[2] === 0x04) {
  684. parseList = ring.deqN(smallPackageLen)
  685. } else {
  686. parseList = ring.deqN(bigPackageLen)
  687. }
  688. const temp = new Buffer(parseList)
  689. decodeByte(temp, mac)
  690. // console.log(parseList)
  691. }
  692. }
  693. }
  694. //第二个用户传输的得到的
  695. function handleByteTwo(mac) {
  696. let parseList
  697. if (ringTwo.size() >= bigPackageLen) {
  698. const arr = ringTwo.peekN(3)
  699. if (arr[0] !== 0xaa || arr[1] !== 0xaa) {
  700. ringTwo.deq()
  701. } else {
  702. if (arr[2] === 0x04) {
  703. parseList = ringTwo.deqN(smallPackageLen)
  704. } else {
  705. parseList = ringTwo.deqN(bigPackageLen)
  706. }
  707. const temp = new Buffer(parseList)
  708. decodeByteTwo(temp, mac)
  709. // console.log(parseList)
  710. }
  711. }
  712. }
  713. function decodeByte(temp, mac) {
  714. const utcTime = getUtcTime()
  715. // console.log(temp)
  716. const map = new Map()
  717. // map.set("utcTime",utcTime)
  718. // 判断大小包
  719. if (temp.length === smallPackageLen) { // 先校验再计算rawWaveData
  720. if (byteSumCheck(temp)) {
  721. const rawWaveData = getRawWaveValue(temp[5], temp[6])
  722. map.set('rawWaveData', rawWaveData)
  723. }
  724. } else {
  725. if (byteSumCheck(temp)) {
  726. // console.log(temp)
  727. map.set('Signal', temp.readUInt8(4))
  728. map.set('Delta', temp.readUInt8(7) * 256 * 256 + temp.readUInt8(8) * 256 + temp.readUInt8(9))
  729. map.set('Theta', temp.readUInt8(10) * 256 * 256 + temp.readUInt8(11) * 256 + temp.readUInt8(12))
  730. map.set('LowAlpha', temp.readUInt8(13) * 256 * 256 + temp.readUInt8(14) * 256 + temp.readUInt8(15))
  731. map.set('HighAlpha', temp.readUInt8(16) * 256 * 256 + temp.readUInt8(17) * 256 + temp.readUInt8(18))
  732. map.set('LowBeta', temp.readUInt8(19) * 256 * 256 + temp.readUInt8(20) * 256 + temp.readUInt8(21))
  733. map.set('HighBeta', temp.readUInt8(22) * 256 * 256 + temp.readUInt8(23) * 256 + temp.readUInt8(24))
  734. map.set('LowGamma', temp.readUInt8(25) * 256 * 256 + temp.readUInt8(26) * 256 + temp.readUInt8(27))
  735. map.set('MiddleGamma', temp.readUInt8(28) * 256 * 256 + temp.readUInt8(29) * 256 + temp.readUInt8(30))
  736. map.set('Attention', temp.readUInt8(32))
  737. map.set('Meditation', temp.readUInt8(34))
  738. }
  739. }
  740. // console.log(map.size)
  741. for (const [key, value] of map) {
  742. // console.log(key + ' ============ ' + value)
  743. }
  744. mainWindow.webContents.send('accelerationData-message', { arg: map, mac: mac })
  745. return map
  746. }
  747. function decodeByteTwo(temp, mac) {
  748. const utcTime = getUtcTime()
  749. // console.log(temp)
  750. const map = new Map()
  751. // map.set("utcTime",utcTime)
  752. // 判断大小包
  753. if (temp.length === smallPackageLen) { // 先校验再计算rawWaveData
  754. if (byteSumCheckTwo(temp)) {
  755. const rawWaveData = getRawWaveValueTwo(temp[5], temp[6])
  756. map.set('rawWaveData', rawWaveData)
  757. }
  758. } else {
  759. if (byteSumCheckTwo(temp)) {
  760. // console.log(temp)
  761. map.set('Signal', temp.readUInt8(4))
  762. map.set('Delta', temp.readUInt8(7) * 256 * 256 + temp.readUInt8(8) * 256 + temp.readUInt8(9))
  763. map.set('Theta', temp.readUInt8(10) * 256 * 256 + temp.readUInt8(11) * 256 + temp.readUInt8(12))
  764. map.set('LowAlpha', temp.readUInt8(13) * 256 * 256 + temp.readUInt8(14) * 256 + temp.readUInt8(15))
  765. map.set('HighAlpha', temp.readUInt8(16) * 256 * 256 + temp.readUInt8(17) * 256 + temp.readUInt8(18))
  766. map.set('LowBeta', temp.readUInt8(19) * 256 * 256 + temp.readUInt8(20) * 256 + temp.readUInt8(21))
  767. map.set('HighBeta', temp.readUInt8(22) * 256 * 256 + temp.readUInt8(23) * 256 + temp.readUInt8(24))
  768. map.set('LowGamma', temp.readUInt8(25) * 256 * 256 + temp.readUInt8(26) * 256 + temp.readUInt8(27))
  769. map.set('MiddleGamma', temp.readUInt8(28) * 256 * 256 + temp.readUInt8(29) * 256 + temp.readUInt8(30))
  770. map.set('Attention', temp.readUInt8(32))
  771. map.set('Meditation', temp.readUInt8(34))
  772. }
  773. }
  774. // console.log(map.size)
  775. for (const [key, value] of map) {
  776. // console.log(key + ' ============ ' + value)
  777. }
  778. mainWindow.webContents.send('accelerationData-message-two', { arg: map, mac: mac })
  779. return map
  780. }
  781. function getRawWaveValue(highOrderByte, lowOrderByte) {
  782. const hi = highOrderByte
  783. const lo = lowOrderByte & 0xFF
  784. let value = (hi << 8) | lo
  785. if (value > 32768) {
  786. value = value - 65536
  787. }
  788. return value
  789. }
  790. function getRawWaveValueTwo(highOrderByte, lowOrderByte) {
  791. const hi = highOrderByte
  792. const lo = lowOrderByte & 0xFF
  793. let value = (hi << 8) | lo
  794. if (value > 32768) {
  795. value = value - 65536
  796. }
  797. return value
  798. }
  799. // 检查校验和aa aa 04 80 02 ff f8 86sum = ((0x80 + 0x02 + xxHigh + xxLow)^ 0xFFFFFFFF) & 0xFF
  800. function byteSumCheck(byteTemp) {
  801. if (byteTemp.length === smallPackageLen) {
  802. if ((((byteTemp[3] + byteTemp[4] + byteTemp[5] + byteTemp[6]) ^ 0xffffffff) & 0xff) === byteTemp[7]) {
  803. // console.log('sum check ok!');
  804. return true
  805. } else {
  806. // console.log('sum check false!');
  807. return false
  808. }
  809. } else {
  810. if (byteTemp[0] === 0xaa && byteTemp[1] === 0xaa && byteTemp[2] === 0x20 && byteTemp[3] === 0x02 &&
  811. byteTemp[5] === 0x83 && byteTemp[6] === 0x18 && byteTemp[31] === 0x04 && byteTemp[33] === 0x05) {
  812. return true
  813. } else {
  814. return false
  815. }
  816. }
  817. }
  818. // 检查校验和aa aa 04 80 02 ff f8 86sum = ((0x80 + 0x02 + xxHigh + xxLow)^ 0xFFFFFFFF) & 0xFF
  819. function byteSumCheckTwo(byteTemp) {
  820. if (byteTemp.length === smallPackageLen) {
  821. if ((((byteTemp[3] + byteTemp[4] + byteTemp[5] + byteTemp[6]) ^ 0xffffffff) & 0xff) === byteTemp[7]) {
  822. // console.log('sum check ok!');
  823. return true
  824. } else {
  825. // console.log('sum check false!');
  826. return false
  827. }
  828. } else {
  829. if (byteTemp[0] === 0xaa && byteTemp[1] === 0xaa && byteTemp[2] === 0x20 && byteTemp[3] === 0x02 &&
  830. byteTemp[5] === 0x83 && byteTemp[6] === 0x18 && byteTemp[31] === 0x04 && byteTemp[33] === 0x05) {
  831. return true
  832. } else {
  833. return false
  834. }
  835. }
  836. }
  837. // 获取到com端口
  838. SerialPort.list().then(ports => {
  839. ports.forEach(function (port) {
  840. console.log(port.path)
  841. // console.log(port.pnpId);
  842. })
  843. })
  844. // 获取到格林尼治时间
  845. function getUtcTime() {
  846. // 取本地时间
  847. let localtime = new Date()
  848. // 取本地毫秒数
  849. let localmesc = localtime.getTime()
  850. // 取本地时区与格林尼治所在时区的偏差毫秒数
  851. let localOffset = localtime.getTimezoneOffset() * 60000
  852. // 反推得到格林尼治时间
  853. let utc = localOffset + localmesc
  854. // 得到指定时区时间
  855. // let calctime = utc + (3600000 * offset);
  856. return utc
  857. };
  858. ipcMain.on('asynchronoushrv-controlpanel', function (event, arg) {
  859. // console.log(arg) // prints "ping"
  860. mainWindow.webContents.send('send-message-control', process.cwd())
  861. // 回应同步消息
  862. })
  863. //------------------------是否需要初始化数据库------------------------------//
  864. function dbInit() {
  865. let syncData = null;
  866. syncData = cmd.runSync('sc query ct')
  867. if (syncData.data == null) {
  868. //cmd.runSync('powershell.exe Start-Process -FilePath "./resources/exe/mysql/mysql-5.7.24-winx64/bin/mysql_init.bat" -WindowStyle Hidden -Verb runAs')
  869. let dbInitScript = 'powershell.exe Start-Process -FilePath "' + serveUrl + '/mysql/mysql-5.7.24-winx64/bin/mysql_init.bat" -WindowStyle Hidden -Verb runAs'
  870. cmd.runSync(dbInitScript)
  871. do {
  872. syncData = cmd.runSync('sc query ct')
  873. // console.log("数据库服务正在初始化")
  874. } while (syncData.data == null)
  875. } else {
  876. // console.log("数据库初始化完成")
  877. }
  878. loadingWindow.webContents.send('main-windows-python-exe-run', {
  879. type: "dbInit",
  880. })
  881. // console.log("数据库初始化完成")
  882. }
  883. // // 调用方法,同步执行,阻塞后续程序的执行;
  884. // sleep(2000)
  885. // console.log("数据库服务初始化完成")
  886. // //要设置启动项目时需要启动jar包,和nginx服务
  887. // //假如数据库已初始化完成
  888. // //则启动jar包
  889. function execute_path(cmd, id, path) {
  890. var exec = require("child_process").exec;
  891. // var iconv = request('iconv-lite')
  892. // var encoding ='cp936'
  893. var binaryEncoding = "utf-8";
  894. let _this = this;
  895. exec(cmd, { cwd: path, windowsHide: true }, function (error, stdout, stderr) {
  896. if (error) {
  897. //_this.judge_false(id);
  898. console.log(error);
  899. } else {
  900. console.log(id + ": success");
  901. }
  902. });
  903. }
  904. function execute(cmd, id) {
  905. var exec = require("child_process").exec;
  906. let _this = this;
  907. exec(cmd, function (error, stdout, stderr) {
  908. if (error) {
  909. console.log(error + "错误");
  910. //return stderr;
  911. } else {
  912. console.log("" + stdout.toString());
  913. //return stdout;
  914. }
  915. });
  916. }
  917. //------------------------需要启动数据库服务-------------------------//
  918. function startDb() {
  919. let syncDataDb = null;
  920. syncDataDb = cmd.runSync('sc query ct')
  921. if (!(syncDataDb.data.includes("RUNNING"))) {
  922. execute(
  923. "powershell -Command \"Start-Process cmd -Verb RunAs -ArgumentList '/c net start ct && command -argument'\"",
  924. 1
  925. );
  926. do {
  927. syncDataDb = cmd.runSync('sc query ct')
  928. } while (!(syncDataDb.data.includes("RUNNING")))
  929. } else {
  930. }
  931. loadingWindow.webContents.send('main-windows-python-exe-run', {
  932. type: "startDb",
  933. })
  934. }
  935. //startDb()
  936. // //数据库启动完成后
  937. //---------------------------需要启动jar包-------------------------//
  938. function startJar() {
  939. let syncDataJar = null;
  940. syncDataJar = cmd.runSync("netstat -ano|findstr 8088");
  941. //判断jar包是否启动
  942. //如果未启动就开始执行启动命令
  943. if (syncDataJar.data == null || syncDataJar.data.trim().toUpperCase().indexOf('LISTENING') <= 0) {
  944. execute_path(
  945. "start jre/bin/javaw -Dfile.encoding=utf-8 -jar confrontation-training-java-0.0.1-SNAPSHOT.jar",
  946. 2,
  947. process.cwd() +
  948. //"\\resources\\exe\\jar"
  949. //"\\exe\\jar"
  950. serveUrlJar + "\\jar"
  951. );
  952. //执行完成以后
  953. //需要开个定时器去监听是否是启动成功了
  954. jar_time = setInterval(() => {
  955. syncDataJar = cmd.runSync("netstat -ano|findstr 8088");
  956. if (!(syncDataJar.data == null || syncDataJar.data.trim().toUpperCase().indexOf('LISTENING') <= 0)) {
  957. loadingWindow.webContents.send('main-windows-python-exe-run', {
  958. type: "startJar",
  959. })
  960. console.log("后台服务已启动");
  961. clearInterval(jar_time)
  962. }
  963. }, 1000)
  964. } else {
  965. loadingWindow.webContents.send('main-windows-python-exe-run', {
  966. type: "startJar",
  967. })
  968. console.log("后台服务已启动");
  969. }
  970. }
  971. //startJar()
  972. //获取本机ip
  973. // function getNetworkIp() {
  974. // var os = require("os");
  975. // let needHost = "";
  976. // try {
  977. // // 获得网络接口列表
  978. // let network = os.networkInterfaces();
  979. // for (let item in network) {
  980. // let iface = network[item];
  981. // for (let i = 0; i < iface.length; i++) {
  982. // let alias = iface[i];
  983. // if (
  984. // alias.family === "IPv4" &&
  985. // alias.address !== "127.0.0.1" &&
  986. // !alias.internal
  987. // ) {
  988. // needHost = alias.address;
  989. // }
  990. // }
  991. // }
  992. // } catch (e) {
  993. // console.log(e);
  994. // }
  995. // return needHost;
  996. // }
  997. //获取本机ip 改变util文件夹中的global的ip
  998. //获取本机ip
  999. function getNetworkIp() {
  1000. var os = require("os");
  1001. let needHost = "";
  1002. try {
  1003. // 获得网络接口列表
  1004. let network = os.networkInterfaces();
  1005. for (let item in network) {
  1006. let iface = network[item];
  1007. for (let i = 0; i < iface.length; i++) {
  1008. let alias = iface[i];
  1009. if (
  1010. alias.family === "IPv4" &&
  1011. alias.address !== "127.0.0.1" &&
  1012. !alias.internal
  1013. ) {
  1014. needHost = alias.address;
  1015. }
  1016. }
  1017. }
  1018. } catch (e) {
  1019. console.log(e);
  1020. }
  1021. return needHost;
  1022. }
  1023. //调用方法,改动web服务global.js
  1024. function webGlobal() {
  1025. let ipv4 = getNetworkIp();
  1026. console.log(ipv4)
  1027. ipv4 = 'localhost'
  1028. let urlPath = process.cwd() +
  1029. serveUrlJar + "\\global.js"
  1030. // let urlPath = __static + "\\global.js"
  1031. let str = "var env = {dev: 'http://" +
  1032. ipv4 +
  1033. ":8088/',prod: 'http://" +
  1034. ipv4 +
  1035. ":8088/'};export default env;";
  1036. console.log(urlPath)
  1037. console.log(str)
  1038. fs.writeFile(
  1039. urlPath,
  1040. str,
  1041. (error) => {
  1042. if (error) {
  1043. // printInfo("PC写入global文件失败,原因是" + error.message, "warn");
  1044. return console.log("写入global文件失败,原因是" + error.message);
  1045. } else {
  1046. // printInfo("PC写入global文件失败,原因是", "info");
  1047. console.log("写入成功");
  1048. }
  1049. }
  1050. );
  1051. }
  1052. webGlobal()
  1053. //启动docker
  1054. function startDocker() {
  1055. execFile("C:/nlp/nlp" + '/start1.bat', function (err, data) {
  1056. console.log(err)
  1057. console.log(data.toString())
  1058. })
  1059. }
  1060. // startDocker()
  1061. //关闭docker
  1062. //启动docker
  1063. function stopDocker() {
  1064. execFile("C:/nlp/nlp" + '/stop.bat', function (err, data) {
  1065. console.log(err)
  1066. console.log(data.toString())
  1067. })
  1068. }
  1069. //stopDocker()
  1070. //异步
  1071. ipcMain.on('main-windows-python-exe', function (event, arg) {
  1072. console.log(arg)
  1073. console.log(arg.type)
  1074. if (arg.type === 'exe') {
  1075. console.log("app.vue传值了")
  1076. //调用.exe
  1077. starServer()
  1078. }
  1079. if (arg.type === "dbInit") {
  1080. dbInit()
  1081. }
  1082. if (arg.type === 'startDb') {
  1083. startDb()
  1084. }
  1085. if (arg.type === 'startJar') {
  1086. startJar()
  1087. startBluetoothServer();
  1088. }
  1089. if (arg.type === 'webGlobal') {
  1090. //
  1091. // webGlobal()
  1092. }
  1093. })