Browse Source

罗技控制数据使用线程读取

Caner 2 years ago
parent
commit
11af57de65
3 changed files with 28 additions and 20 deletions
  1. 11 0
      electron/logiControl.js
  2. 16 17
      electron/main.js
  3. 1 3
      src/views/index/index.vue

+ 11 - 0
electron/logiControl.js

@@ -0,0 +1,11 @@
+const HID = require('node-hid');
+try {
+    const devices = HID.devices();
+    const logitech = devices.filter(el => el.manufacturer == 'Logitech');
+    const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
+    data.on('data', (db) => {
+        process.send(db)
+    })
+} catch (error) {
+    process.send({ type: 'err'})
+}

+ 16 - 17
electron/main.js

@@ -1,6 +1,6 @@
 const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, screen, Tray } = require('electron');
 const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, screen, Tray } = require('electron');
 const { join } = require('path');
 const { join } = require('path');
-const HID = require('node-hid');
+const { fork } = require('child_process');
 
 
 class MainSerivce {
 class MainSerivce {
   constructor() {
   constructor() {
@@ -100,7 +100,7 @@ class MainSerivce {
       if (evt === 'close-loading') {
       if (evt === 'close-loading') {
         if (this.loadingWin) this.loadingWin.close()
         if (this.loadingWin) this.loadingWin.close()
         this.mainWin.show()
         this.mainWin.show()
-        // this.connectLogi()
+        if(this.mainWin.isVisible()) this.connectLogi()
       } else if (evt === 'minWin') {
       } else if (evt === 'minWin') {
         this.mainWin.minimize()
         this.mainWin.minimize()
       } else if (evt === 'closeWin') {
       } else if (evt === 'closeWin') {
@@ -116,21 +116,20 @@ class MainSerivce {
   }
   }
 
 
   connectLogi() {
   connectLogi() {
-    try {
-      const devices = HID.devices();
-      const logitech = devices.filter(el => el.manufacturer == 'Logitech');
-      const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
-      data.on('data', (db) => {
-        if (this.mainWin) this.mainWin?.webContents.send('contrlData', db)
-      })
-    } catch (error) {
-      dialog.showMessageBox(this.mainWin, { message: '连接方向盘失败', type: 'error', title: '连接错误', detail: '请检查方向盘是否连接' }).then(({ response }) => {
-        if (!response) {
-          this.mainWin.close()
-          app.quit()
-        }
-      })
-    }
+    const signal = fork(join(__dirname, './logiControl.js'))
+    signal.on('message',msg=>{
+      if(msg.type === 'err'){
+        dialog.showMessageBox(this.mainWin, { message: '连接方向盘失败', type: 'error', title: '连接错误', detail: '请检查方向盘是否连接' }).then(({ response }) => {
+          if (!response) {
+            this.mainWin.close()
+            app.quit()
+          }
+        })
+      }else{
+        console.log(msg.data);
+        if (this.mainWin && !this.mainWin.isDestroyed()) this.mainWin?.webContents.send('contrlData', msg.data)
+      }
+    })
   }
   }
 };
 };
 new MainSerivce()
 new MainSerivce()

+ 1 - 3
src/views/index/index.vue

@@ -240,12 +240,10 @@ function intSoketRtc(host: string) {
 // })
 // })
 
 
 window.$electron?.on('contrlData', (db: Uint8Array) => {
 window.$electron?.on('contrlData', (db: Uint8Array) => {
-  console.log(1, db)
-
   // 拨片 2 是右拨片,1是左拨片,0是取消
   // 拨片 2 是右拨片,1是左拨片,0是取消
   const bp = db[6] === 2 ? '右拨片' : db[6] === 1 ? '左拨片' : ''
   const bp = db[6] === 2 ? '右拨片' : db[6] === 1 ? '左拨片' : ''
   const fx = parseInt(db[44].toString(), 10) // 转10进制
   const fx = parseInt(db[44].toString(), 10) // 转10进制
-  const fxp = fx < 123 ? `左轮${fx}` : fx > 132 ? `右轮${fx}` : ''
+  const fxp = fx < 125 ? `左轮${fx}` : fx > 130 ? `右轮${fx}` : ''
   const ym = parseInt(db[46].toString(), 10) // 油门
   const ym = parseInt(db[46].toString(), 10) // 油门
   console.log(66, bp, fxp, 255 - ym)
   console.log(66, bp, fxp, 255 - ym)
 })
 })