|
@ -32,8 +32,10 @@ export function registerIpRouter() { |
|
|
ipcMain.handle(IPC_EVENT.VIDEO_FPS_SET, videoFpsSet) |
|
|
ipcMain.handle(IPC_EVENT.VIDEO_FPS_SET, videoFpsSet) |
|
|
ipcMain.handle(IPC_EVENT.THRESHOLD_SET, thresholdSet) |
|
|
ipcMain.handle(IPC_EVENT.THRESHOLD_SET, thresholdSet) |
|
|
ipcMain.handle(IPC_EVENT.INVALID_DATA_COUNT_SET, invalidDataCountSet) |
|
|
ipcMain.handle(IPC_EVENT.INVALID_DATA_COUNT_SET, invalidDataCountSet) |
|
|
|
|
|
ipcMain.handle(IPC_EVENT.IMAGE_SEND_ENABLED, imageSendEnabledSet) |
|
|
|
|
|
ipcMain.handle(IPC_EVENT.IS_CLEAR_ZERO, isClearZeroSet) |
|
|
} |
|
|
} |
|
|
|
|
|
// 搜索设备
|
|
|
const searchDevice = (event) => { |
|
|
const searchDevice = (event) => { |
|
|
const message = Buffer.from(JSON.stringify({ command: 'name', type: 'get' })) |
|
|
const message = Buffer.from(JSON.stringify({ command: 'name', type: 'get' })) |
|
|
const PORT = 2230 |
|
|
const PORT = 2230 |
|
@ -79,6 +81,7 @@ const searchDevice = (event) => { |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 连接设备
|
|
|
const connectDevice = (event, { ip, port }) => { |
|
|
const connectDevice = (event, { ip, port }) => { |
|
|
if (!ip || !port) { |
|
|
if (!ip || !port) { |
|
|
event.reply(IPC_EVENT.DEVICE_CONNECT_REPLY, { success: false, error: '参数缺失' }) |
|
|
event.reply(IPC_EVENT.DEVICE_CONNECT_REPLY, { success: false, error: '参数缺失' }) |
|
@ -117,7 +120,6 @@ const connectDevice = (event, { ip, port }) => { |
|
|
let msg |
|
|
let msg |
|
|
try { |
|
|
try { |
|
|
msg = JSON.parse(line) |
|
|
msg = JSON.parse(line) |
|
|
console.log('Received command:', `${msg.command}:${msg.type}`) |
|
|
|
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
console.error('TCP data parse error:', e) |
|
|
console.error('TCP data parse error:', e) |
|
|
fs.appendFileSync('error_log.txt', line + '\n') |
|
|
fs.appendFileSync('error_log.txt', line + '\n') |
|
@ -125,7 +127,7 @@ const connectDevice = (event, { ip, port }) => { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (!msg || !msg.command) { |
|
|
if (!msg || !msg.command) { |
|
|
console.log('invalid msg format:', msg) |
|
|
console.warn('invalid msg format:', msg) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -148,6 +150,7 @@ const connectDevice = (event, { ip, port }) => { |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 断开连接
|
|
|
const disconnectDevice = (event, { ip }) => { |
|
|
const disconnectDevice = (event, { ip }) => { |
|
|
const connectionInfo = tcpClients.get(ip) |
|
|
const connectionInfo = tcpClients.get(ip) |
|
|
if (connectionInfo) { |
|
|
if (connectionInfo) { |
|
@ -200,7 +203,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
if (imageTimeRequest) { |
|
|
if (imageTimeRequest) { |
|
|
// 响应图像发送间隔获取请求
|
|
|
// 响应图像发送间隔获取请求
|
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
imageTimeRequest.resolve({ success: true, data: msg }) |
|
|
imageTimeRequest.resolve({ success: true, data: msg }) |
|
|
|
|
|
|
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IMAGE_SEND_TIME_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IMAGE_SEND_TIME_GET}`) |
|
@ -213,7 +215,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
if (imageTimeRequest) { |
|
|
if (imageTimeRequest) { |
|
|
// 响应图像发送间隔设置请求
|
|
|
// 响应图像发送间隔设置请求
|
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
imageTimeRequest.resolve({ success: true, data: msg }) |
|
|
imageTimeRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IMAGE_SEND_TIME_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IMAGE_SEND_TIME_SET}`) |
|
|
} |
|
|
} |
|
@ -225,7 +226,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.ZERO_COUNT_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.ZERO_COUNT_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.ZERO_COUNT_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.ZERO_COUNT_GET}`) |
|
|
} |
|
|
} |
|
@ -236,7 +236,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.RESULT_COUNT_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.RESULT_COUNT_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.RESULT_COUNT_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.RESULT_COUNT_GET}`) |
|
|
} |
|
|
} |
|
@ -247,7 +246,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.DATA_FPS_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.DATA_FPS_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.DATA_FPS_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.DATA_FPS_GET}`) |
|
|
} |
|
|
} |
|
@ -258,7 +256,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.VIDEO_FPS_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.VIDEO_FPS_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.VIDEO_FPS_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.VIDEO_FPS_GET}`) |
|
|
} |
|
|
} |
|
@ -269,7 +266,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.THRESHOLD_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.THRESHOLD_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.THRESHOLD_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.THRESHOLD_GET}`) |
|
|
} |
|
|
} |
|
@ -280,7 +276,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_GET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_GET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_GET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_GET}`) |
|
|
} |
|
|
} |
|
@ -292,7 +287,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.ZERO_COUNT_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.ZERO_COUNT_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.ZERO_COUNT_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.ZERO_COUNT_SET}`) |
|
|
} |
|
|
} |
|
@ -303,7 +297,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.RESULT_COUNT_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.RESULT_COUNT_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.RESULT_COUNT_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.RESULT_COUNT_SET}`) |
|
|
} |
|
|
} |
|
@ -314,7 +307,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.DATA_FPS_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.DATA_FPS_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.DATA_FPS_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.DATA_FPS_SET}`) |
|
|
} |
|
|
} |
|
@ -325,7 +317,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.VIDEO_FPS_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.VIDEO_FPS_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.VIDEO_FPS_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.VIDEO_FPS_SET}`) |
|
|
} |
|
|
} |
|
@ -336,7 +327,6 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.THRESHOLD_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.THRESHOLD_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.THRESHOLD_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.THRESHOLD_SET}`) |
|
|
} |
|
|
} |
|
@ -347,12 +337,33 @@ const handleTcpResponse = async (ip, msg) => { |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_SET}`) |
|
|
const paramRequest = pendingRequests.get(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_SET}`) |
|
|
if (paramRequest) { |
|
|
if (paramRequest) { |
|
|
await delay() |
|
|
await delay() |
|
|
console.log(msg) |
|
|
|
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
paramRequest.resolve({ success: true, data: msg }) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_SET}`) |
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.INVALID_DATA_COUNT_SET}`) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
break |
|
|
break |
|
|
|
|
|
case IPC_EVENT.IMAGE_SEND_ENABLED: |
|
|
|
|
|
{ |
|
|
|
|
|
const imageSendRequest = pendingRequests.get(`${ip}${IPC_EVENT.IMAGE_SEND_ENABLED}`) |
|
|
|
|
|
if (imageSendRequest) { |
|
|
|
|
|
// 响应图像发送设置请求
|
|
|
|
|
|
await delay() |
|
|
|
|
|
imageSendRequest.resolve({ success: true, data: msg }) |
|
|
|
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IMAGE_SEND_ENABLED}`) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
break |
|
|
|
|
|
case IPC_EVENT.IS_CLEAR_ZERO: |
|
|
|
|
|
{ |
|
|
|
|
|
const clearZeroRequest = pendingRequests.get(`${ip}${IPC_EVENT.IS_CLEAR_ZERO}`) |
|
|
|
|
|
if (clearZeroRequest) { |
|
|
|
|
|
// 响应清零使能设置请求
|
|
|
|
|
|
await delay() |
|
|
|
|
|
clearZeroRequest.resolve({ success: true, data: msg }) |
|
|
|
|
|
pendingRequests.delete(`${ip}${IPC_EVENT.IS_CLEAR_ZERO}`) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
break |
|
|
case IPC_EVENT.HEARTBEAT_REPLY: |
|
|
case IPC_EVENT.HEARTBEAT_REPLY: |
|
|
// 心跳处理
|
|
|
// 心跳处理
|
|
|
break |
|
|
break |
|
@ -768,7 +779,6 @@ const dataFpsSet = async (event, { ip, value }) => { |
|
|
|
|
|
|
|
|
const command = { command: 'dataFps', type: 'set', values: value } |
|
|
const command = { command: 'dataFps', type: 'set', values: value } |
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
console.log('Sending command:', message) |
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
connectionInfo.client.write(message, (err) => { |
|
|
if (err) { |
|
|
if (err) { |
|
|
pendingRequests.delete(requestKey) |
|
|
pendingRequests.delete(requestKey) |
|
@ -1018,6 +1028,82 @@ const invalidDataCountSet = async (event, { ip, value }) => { |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
// 图像发送使能设置
|
|
|
|
|
|
const imageSendEnabledSet = async (event, { ip, value }) => { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
const connectionInfo = tcpClients.get(ip) |
|
|
|
|
|
if (!connectionInfo) { |
|
|
|
|
|
resolve({ success: false, error: '设备未连接' }) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const requestKey = `${ip}${IPC_EVENT.IMAGE_SEND_ENABLED}` |
|
|
|
|
|
const timeout = setTimeout(() => { |
|
|
|
|
|
pendingRequests.delete(requestKey) |
|
|
|
|
|
resolve({ success: false, error: '请求超时' }) |
|
|
|
|
|
}, TIMEOUT) |
|
|
|
|
|
|
|
|
|
|
|
pendingRequests.set(requestKey, { |
|
|
|
|
|
resolve: (result) => { |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
resolve(result) |
|
|
|
|
|
}, |
|
|
|
|
|
reject: (error) => { |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
reject(error) |
|
|
|
|
|
}, |
|
|
|
|
|
timestamp: Date.now() |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const command = { command: 'imageSendEnabled', type: 'set', values: value } |
|
|
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
|
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
pendingRequests.delete(requestKey) |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
resolve({ success: false, error: err.message }) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
const isClearZeroSet = async (event, { ip, value }) => { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
const connectionInfo = tcpClients.get(ip) |
|
|
|
|
|
if (!connectionInfo) { |
|
|
|
|
|
resolve({ success: false, error: '设备未连接' }) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const requestKey = `${ip}${IPC_EVENT.IS_CLEAR_ZERO}` |
|
|
|
|
|
const timeout = setTimeout(() => { |
|
|
|
|
|
pendingRequests.delete(requestKey) |
|
|
|
|
|
resolve({ success: false, error: '请求超时' }) |
|
|
|
|
|
}, TIMEOUT) |
|
|
|
|
|
|
|
|
|
|
|
pendingRequests.set(requestKey, { |
|
|
|
|
|
resolve: (result) => { |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
resolve(result) |
|
|
|
|
|
}, |
|
|
|
|
|
reject: (error) => { |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
reject(error) |
|
|
|
|
|
}, |
|
|
|
|
|
timestamp: Date.now() |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const command = { command: 'isClearZero', type: 'set', values: value } |
|
|
|
|
|
const message = JSON.stringify(command) + END_SEQUENCE |
|
|
|
|
|
connectionInfo.client.write(message, (err) => { |
|
|
|
|
|
if (err) { |
|
|
|
|
|
pendingRequests.delete(requestKey) |
|
|
|
|
|
clearTimeout(timeout) |
|
|
|
|
|
resolve({ success: false, error: err.message }) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 清理指定IP的所有待处理请求
|
|
|
// 清理指定IP的所有待处理请求
|
|
|
const clearPendingRequestsByIp = (ip) => { |
|
|
const clearPendingRequestsByIp = (ip) => { |
|
|