|
|
@ -2,7 +2,7 @@ import { ipcMain } from 'electron' |
|
|
|
import dgram from 'dgram' |
|
|
|
import net from 'net' |
|
|
|
import { IPC_EVENT } from '../renderer/src/common/ipcEvents.js' |
|
|
|
import fs from 'fs' |
|
|
|
import log from 'electron-log' |
|
|
|
const TIMEOUT = 10000 // 10秒超时
|
|
|
|
const END_SEQUENCE = '\n\n' // 消息结束标志
|
|
|
|
// 全局保存所有TCP连接和相关信息
|
|
|
@ -10,6 +10,12 @@ const tcpClients = new Map() |
|
|
|
// 保存待处理的请求,用于关联响应
|
|
|
|
const pendingRequests = new Map() |
|
|
|
|
|
|
|
// 记录IPC命令的通用函数(事件类型自动拼接)
|
|
|
|
const logIPCCommand = (ip, command) => { |
|
|
|
const eventType = `${command.command?.toUpperCase()}_${command.type?.toUpperCase()}` |
|
|
|
log.info(`IPC Command from renderer - ${eventType} to ${ip}:`, command) |
|
|
|
} |
|
|
|
|
|
|
|
export function registerIpRouter() { |
|
|
|
ipcMain.on(IPC_EVENT.DEVICE_SEARCH, searchDevice) |
|
|
|
ipcMain.on(IPC_EVENT.DEVICE_CONNECT, connectDevice) |
|
|
@ -49,10 +55,10 @@ const searchDevice = (event) => { |
|
|
|
udpClient.setBroadcast(true) |
|
|
|
udpClient.send(message, 0, message.length, PORT, BROADCAST_ADDR, (err) => { |
|
|
|
if (err) { |
|
|
|
console.error('UDP send failed', err) |
|
|
|
log.error('UDP send failed', err) |
|
|
|
udpClient.close() |
|
|
|
} else { |
|
|
|
console.log('UDP send successful') |
|
|
|
log.info('UDP send successful') |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
@ -64,18 +70,18 @@ const searchDevice = (event) => { |
|
|
|
data: msg.toString() |
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
console.error('parse UDP message failed:', e) |
|
|
|
log.error('UDP message parse error', e) |
|
|
|
} |
|
|
|
if (timer) clearTimeout(timer) |
|
|
|
timer = setTimeout(() => { |
|
|
|
udpClient.close() |
|
|
|
console.log('UDP socket closed after timeout') |
|
|
|
log.info('UDP socket closed after timeout') |
|
|
|
event.reply && event.reply(IPC_EVENT.DEVICE_SEARCH_REPLY, Array.from(resultMap.values())) |
|
|
|
}, 1000) |
|
|
|
}) |
|
|
|
|
|
|
|
udpClient.on('error', (err) => { |
|
|
|
console.error('UDP error:', err) |
|
|
|
log.error('UDP error:', err) |
|
|
|
udpClient.close() |
|
|
|
event.reply && event.reply(IPC_EVENT.DEVICE_SEARCH_REPLY, Array.from(resultMap.values())) |
|
|
|
}) |
|
|
@ -121,13 +127,12 @@ const connectDevice = (event, { ip, port }) => { |
|
|
|
try { |
|
|
|
msg = JSON.parse(line) |
|
|
|
} catch (e) { |
|
|
|
console.error('TCP data parse error:', e) |
|
|
|
fs.appendFileSync('error_log.txt', line + '\n') |
|
|
|
log.error('TCP data parse error:', e) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
if (!msg || !msg.command) { |
|
|
|
console.warn('invalid msg format:', msg) |
|
|
|
log.warn('invalid msg format:', msg) |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
@ -137,6 +142,7 @@ const connectDevice = (event, { ip, port }) => { |
|
|
|
}) |
|
|
|
|
|
|
|
client.on('error', (err) => { |
|
|
|
log.error('TCP connection error:', err) |
|
|
|
event.reply(IPC_EVENT.DEVICE_CONNECT_REPLY, { success: false, error: err.message }) |
|
|
|
client.destroy() |
|
|
|
tcpClients.delete(ip) |
|
|
@ -145,6 +151,7 @@ const connectDevice = (event, { ip, port }) => { |
|
|
|
}) |
|
|
|
|
|
|
|
client.on('close', () => { |
|
|
|
log.info(`TCP connection to ${ip} closed`) |
|
|
|
tcpClients.delete(ip) |
|
|
|
clearPendingRequestsByIp(ip) |
|
|
|
}) |
|
|
@ -168,7 +175,17 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
|
const connectionInfo = tcpClients.get(ip) |
|
|
|
if (!connectionInfo) return |
|
|
|
|
|
|
|
switch (`${msg.command}:${msg.type}`) { |
|
|
|
// 记录TCP响应数据
|
|
|
|
const commandType = `${msg.command}:${msg.type}` |
|
|
|
if ( |
|
|
|
commandType !== IPC_EVENT.RESULT_REPLY && |
|
|
|
commandType !== IPC_EVENT.IMAGE_REPLY && |
|
|
|
commandType !== IPC_EVENT.HEARTBEAT_REPLY |
|
|
|
) { |
|
|
|
log.info(`TCP Response from ${ip}:`, commandType, msg.values || msg) |
|
|
|
} |
|
|
|
|
|
|
|
switch (commandType) { |
|
|
|
case IPC_EVENT.RESULT_REPLY: |
|
|
|
connectionInfo.eventSender.send(IPC_EVENT.RESULT_REPLY, { ip, ...msg }) |
|
|
|
break |
|
|
@ -407,6 +424,7 @@ const sensorLoad = async (event, { ip }) => { |
|
|
|
// 发送传感器加载命令
|
|
|
|
const command = { command: 'sensors', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -451,6 +469,7 @@ const sensorSet = async (event, { ip, sensors }) => { |
|
|
|
// 发送传感器设置命令
|
|
|
|
const command = { command: 'sensors', type: 'set', values: sensors } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -495,6 +514,7 @@ const imageSendTimeGet = async (event, { ip }) => { |
|
|
|
// 发送图像发送间隔获取命令
|
|
|
|
const command = { command: 'imageSendTime', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -539,6 +559,7 @@ const imageSendTimeSet = async (event, { ip, value }) => { |
|
|
|
// 发送图像发送间隔设置命令
|
|
|
|
const command = { command: 'imageSendTime', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -579,6 +600,7 @@ const zeroCountGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'zeroCount', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -619,6 +641,7 @@ const zeroCountSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'zeroCount', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -659,6 +682,7 @@ const resultCountGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'resultCount', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand('RESULT_COUNT_GET', ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -699,6 +723,7 @@ const resultCountSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'resultCount', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -739,7 +764,7 @@ const dataFpsGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'dataFps', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -779,6 +804,7 @@ const dataFpsSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'dataFps', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -818,7 +844,7 @@ const videoFpsGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'videoFps', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -858,7 +884,7 @@ const videoFpsSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'videoFps', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -898,7 +924,7 @@ const thresholdGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'threshold', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -938,7 +964,7 @@ const thresholdSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'threshold', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -978,7 +1004,7 @@ const invalidDataCountGet = async (event, { ip }) => { |
|
|
|
|
|
|
|
const command = { command: 'invalidDataCount', type: 'get', values: '' } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -1018,7 +1044,7 @@ const invalidDataCountSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'invalidDataCount', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
logIPCCommand(ip, command) |
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
@ -1057,6 +1083,7 @@ const imageSendEnabledSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'imageSendEnabled', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
@ -1095,6 +1122,8 @@ const isClearZeroSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
const command = { command: 'isClearZero', type: 'set', values: value } |
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
logIPCCommand(ip, command) |
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
if (err) { |
|
|
|
pendingRequests.delete(requestKey) |
|
|
|