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
|
}
|