34
chenyc
2024-12-03 41ce1252805212ce336f292a69a8dbdcbf981fc0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import os from 'node:os'
import logger from '../../utils/logger';
import deviceClientSocket from './deviceClientSocket'
import toolSocket from './toolSocket'
 
 
const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url))
/**本地sockte连接池  */
const devcieClientSocketList=[]
/** 测试socket对象 */
let socketOnjj=null
 
 
 
// The built directory structure
//
// ├─┬ dist-electron
// │ ├─┬ main
// │ │ └── index.js    > Electron-Main
// │ └─┬ preload
// │   └── index.mjs   > Preload-Scripts
// ├─┬ dist
// │ └── index.html    > Electron-Renderer
//
process.env.APP_ROOT = path.join(__dirname, '../..')
 
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
export const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
 
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
  ? path.join(process.env.APP_ROOT, 'public')
  : RENDERER_DIST
 
// Disable GPU Acceleration for Windows 7
if (os.release().startsWith('6.1')) app.disableHardwareAcceleration()
 
// Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
 
if (!app.requestSingleInstanceLock()) {
  app.quit()
  process.exit(0)
}
 
let win: BrowserWindow | null = null
const preload = path.join(__dirname, '../preload/index.mjs')
const indexHtml = path.join(RENDERER_DIST, 'index.html')
 
async function createWindow() {
  win = new BrowserWindow({
    title: '主窗口',
    icon: path.join(process.env.VITE_PUBLIC, 'favicon.ico'),
    webPreferences: {
      preload,
      // 警告:启用节点集成和禁用上下文隔离在生产中不安全
      //node集成为true,
      //考虑使用上下文桥.exposeInMainWorld
      //"/阅读更多关于https://www.electronjs.org/docs/latest/tutorial/context-isolation
      //上下文隔离:false,
      // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
      // nodeIntegration: true,
 
      // Consider using contextBridge.exposeInMainWorld
      // Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
      // contextIsolation: false,
    },
  })
 
  if (VITE_DEV_SERVER_URL) { // #298
    win.loadURL(VITE_DEV_SERVER_URL)
    // Open devTool if the app is not packaged
    //测试主动推送消息给电子邮件发送者
    win.webContents.openDevTools()
  } else {
    win.loadFile(indexHtml)
  }
 
  // Test actively push message to the Electron-Renderer
  //测试主动推送消息给电子邮件发送者
  win.webContents.on('did-finish-load', () => {
    win?.webContents.send('main-process-message', new Date().toLocaleString())
  })
 
  // Make all links open with the browser, not with the application
  ////用浏览器打开所有链接,而不是用应用程序打开
  win.webContents.setWindowOpenHandler(({ url }) => {
    console.log(url)
    if (url.startsWith('https:')) shell.openExternal(url)
    return { action: 'deny' }
  })
  // win.webContents.on('will-navigate', (event, url) => { }) #344
  logger.info('窗口已经打开')
}
 
app.whenReady().then(createWindow)
 
 
/**
 *  参数有 ip  端口 三元组信息  row.productKey,row.deviceName,row.deviceSecret
 */
ipcMain.on('openClient',(event,arg)=>{
    console.log(arg[0],arg[1],arg[2],arg[3],arg[4])
    console.log(devcieClientSocketList.length,'chang du ----------')
    const deviceClientObj=devcieClientSocketList.find(e=>{return e.host===arg[0]})
    if(deviceClientObj){
      console.log('you jiu  zhi jie chong lian')
      try{
        // 有重连机制就放弃操作
        if(!deviceClientObj.shouldReconnect){
          deviceClientObj.connect()
        }
      }catch(error){
        logger.error(`${arg[0]}打开重连中出错了`+error)
      }
     
    }else{
      try{
        const deviceClientObj= new deviceClientSocket(arg[0],arg[1],arg[2],arg[3],arg[4])
        console.log('mei you jiu new yi  ge xin dui xiang')
        devcieClientSocketList.push(deviceClientObj)
      }catch(error){
        console.error(error)
        logger.error(`${arg[0]}初始化链接中出错了`+error)
      }
    }
      
})
// 发送数据
ipcMain.on('openClientMsg',(event,arg)=>{
  console.log(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5])
    const deviceClientObj=devcieClientSocketList.find(e=>{return e.host===arg[0]})
    if(deviceClientObj){
      // 有就发送数据
      console.log('you jiu fa song  shu ju')
      try{
        deviceClientObj.fasongData(arg[5])
      }catch(error){
        fanhuiEErrer(error)
        logger.error(`${arg[0]}发送数据错误了`+error)
      }
      
     
    }else{
      console.log('mei you jian li lian jie, bu neng fa song')
    }
})
// 发送有校验的数据血压
ipcMain.on('fasongYJMsg',(event,arg)=>{
  console.log(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5])
    const deviceClientObj=devcieClientSocketList.find(e=>{return e.host===arg[0]})
    if(deviceClientObj){
      // 有就发送数据
      console.log('you jiu fa song  shu ju')
      try{
        deviceClientObj.fasongDataJiaoyan(arg[5])
      }catch(error){
        fanhuiEErrer(error)
        logger.error(`${arg[0]}发送数据错误了`+error)
      }
      
     
    }
})
ipcMain.on('closeClient', (event,arg) => {
    console.log('zhu xiao lian jie')
    const deviceClientObj=devcieClientSocketList.find(e=>{return e.host===arg[0]&&e.prot===arg[2]})
    const index =devcieClientSocketList.findIndex(e=>{return e.host===arg[0]&&e.prot===arg[2]})
    if(deviceClientObj){
      console.log('xiao  hui  lian jie')
      try{
        deviceClientObj.close()
        console.log('0000222')
        devcieClientSocketList.splice(index,1)
        console.log(devcieClientSocketList.length,'-----')
      }catch(error){
        console.log('------gggg')
        logger.error(`${arg[0]}关闭连接`+error)
      }
      
    }else{
      console.log('mei you yao czhu xiao d lain jie')
    }
  
})
    
ipcMain.on('reload', (event,arg) => {
 win.reload()
})
 
 
app.on('window-all-closed', () => {
  win = null
  if (process.platform !== 'darwin') app.quit()
})
 
app.on('second-instance', () => {
  if (win) {
    // Focus on the main window if the user tried to open another
    //如果用户试图打开另一个窗口,请将焦点放在主窗口上
    if (win.isMinimized()) win.restore()
    win.focus()
  }
})
 
app.on('activate', () => {
  const allWindows = BrowserWindow.getAllWindows()
  if (allWindows.length) {
    allWindows[0].focus()
  } else {
    createWindow()
  }
})
 
// New window example arg: new windows url
ipcMain.handle('open-win', (_, arg) => {
  const childWindow = new BrowserWindow({
    webPreferences: {
      preload,
      nodeIntegration: true,
      contextIsolation: false,
    },
  })
 
  if (VITE_DEV_SERVER_URL) {
    childWindow.loadURL(`${VITE_DEV_SERVER_URL}#${arg}`)
  } else {
    childWindow.loadFile(indexHtml, { hash: arg })
  }
})
 
/**
 * 
 * @param data 返回连接日志
 */
const fanhuiCommLog=(data:any)=>{
  win?.webContents.send('result-connLog', data)
}
 
/**把传过来的数据返回给 */
// data[0]IP,data[1] 数据bf
const fanhuiData=(data:any)=>{
  win?.webContents.send('result-data', data)
}
 
/**把传过来的数据返回给 */
// data[0]IP,data[1] 数据bf
const fanhuiState=(data:any)=>{
  win?.webContents.send('result-state', data)
}
/**
 * 
 * @param data 传递异常信息
 */
const fanhuiEErrer=(data:any)=>{
  win.webContents.send('result-errer',data)
}
/**把这个方法调用输出到socket连接池用 */
export  {
  fanhuiData,
  fanhuiState,
  fanhuiEErrer,
  fanhuiCommLog
}