382 lines
14 KiB
TypeScript
Raw Normal View History

2024-07-09 11:11:31 +08:00
//import UdpClient from './UdpClient';
2024-01-05 11:11:15 +08:00
import UdpClientByCenter from './UdpClientByCenter';
2025-02-11 09:09:34 +08:00
import { getSyncData } from '../service/initable';
import { getChuankouFn } from '../../common/service/indexService';
import FileUtil from '../../common/utils/File';
import { GlobalConfig } from '../../config/index';
import { Array2Byte, string2Bytes } from './tools';
2024-07-09 13:13:59 +08:00
export async function sendMsg(val) {
2024-05-16 09:53:10 +08:00
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
2024-01-05 11:11:15 +08:00
}
2024-07-09 13:13:59 +08:00
2025-02-11 09:09:34 +08:00
export async function getUDP(context, errorFlag?) {
return new Promise(async (reslove, reject) => {
2024-08-10 14:00:18 +08:00
const fileUtil = new FileUtil(context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
2025-02-11 09:09:34 +08:00
console.log('ttttt', data)
2024-08-10 14:00:18 +08:00
if (data === '' || data === undefined) {
console.log(` getUDP has no IPConfigTable `);
globalThis.udpClient = {}
globalThis.host = ''
reslove('')
} else {
2025-02-11 09:09:34 +08:00
const result = JSON.parse(data)
2024-08-10 14:00:18 +08:00
console.log(` getUDP has IPConfigTable `);
if (globalThis.udpClient && globalThis.udpClient.closeUdp) {
console.log(` getUDP has udclent close and rebind `);
globalThis.udpClient.closeUdp(() => {
setTimeout(() => {
2025-02-11 09:09:34 +08:00
globalThis.udpClient.rebindUdp(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp,
result.udpOppositeIpPort)
2024-08-10 14:00:18 +08:00
globalThis.udpClient.sendMsg('111', null)
globalThis.host = `http://${result.centerIp}:${result.centerPort}`
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onError_Callback(() => {
2024-08-10 14:00:18 +08:00
console.log('getUDPgetUDPgetUDP')
2025-02-11 09:09:34 +08:00
getUDP(context, true)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
2024-08-10 14:00:18 +08:00
}
})
reslove(`http://${result.centerIp}:${result.centerPort}`)
// globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
}, 1000)
})
2025-02-11 09:09:34 +08:00
} else {
2024-08-10 14:00:18 +08:00
// 未绑定
console.log(` getUDP has no udclent and bind `);
2025-02-11 09:09:34 +08:00
console.log('ttttttdata', JSON.stringify(result))
const udpClient: UdpClientByCenter =
new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp,
result.udpOppositeIpPort)
2024-08-10 14:00:18 +08:00
udpClient.bindUdp()
udpClient.sendMsg('111')
globalThis.host = `http://${result.centerIp}:${result.centerPort}`
globalThis.udpClient = udpClient
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onMessage_1(() => {
2024-08-10 14:00:18 +08:00
console.log('getUDPonMessage_1msgmsgByGloalUdp')
})
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onError_Callback(() => {
getUDP(context, true)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
2024-08-10 14:00:18 +08:00
}
})
getChuankouFn()
2024-08-14 14:46:33 +08:00
reslove(`http://${result.centerIp}:${result.centerPort}`)
2024-08-10 14:00:18 +08:00
}
}
return
2024-07-19 09:21:47 +08:00
getSyncData('IpConfigTable').then((result: Array<any>) => {
if (result.length) {
console.log(` getUDP has IPConfigTable `);
if (globalThis.udpClient && globalThis.udpClient.closeUdp) {
console.log(` getUDP has udclent close and rebind `);
globalThis.udpClient.closeUdp(() => {
setTimeout(() => {
2025-02-11 09:09:34 +08:00
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp,
result[0].udpOppositeIpPort)
2024-07-19 09:21:47 +08:00
globalThis.udpClient.sendMsg('111', null)
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onError_Callback(() => {
2024-08-07 08:57:40 +08:00
console.log('getUDPgetUDPgetUDP')
2025-02-11 09:09:34 +08:00
getUDP(true, context)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
2024-08-07 08:57:40 +08:00
}
})
2024-07-19 09:21:47 +08:00
reslove(`http://${result[0].centerIp}:${result[0].centerPort}`)
2024-08-07 08:57:40 +08:00
2024-07-19 09:21:47 +08:00
// globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
}, 1000)
})
2024-08-07 08:57:40 +08:00
2025-02-11 09:09:34 +08:00
} else {
2024-07-19 09:21:47 +08:00
// 未绑定
console.log(` getUDP has no udclent and bind `);
2025-02-11 09:09:34 +08:00
const udpClient: UdpClientByCenter =
new UdpClientByCenter(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp,
result[0].udpOppositeIpPort)
2024-07-19 09:21:47 +08:00
udpClient.bindUdp()
udpClient.sendMsg('111')
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
globalThis.udpClient = udpClient
2024-08-07 08:57:40 +08:00
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onMessage_1(() => {
2024-08-08 18:09:06 +08:00
console.log('getUDPonMessage_1msgmsgByGloalUdp')
2024-08-07 08:57:40 +08:00
})
2025-02-11 09:09:34 +08:00
globalThis.udpClient.onError_Callback(() => {
getUDP(true, context)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
2024-08-07 08:57:40 +08:00
}
})
// udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
2024-07-19 09:21:47 +08:00
getChuankouFn()
reslove(`http://${result[0].centerIp}:${result[0].centerPort}`)
2024-08-07 08:57:40 +08:00
2024-07-19 09:21:47 +08:00
}
2024-01-05 11:11:15 +08:00
2024-07-19 09:21:47 +08:00
} else {
console.log(` getUDP has no IPConfigTable `);
// if(globalThis.udpClient)
// {
// console.log(` getUDP2 has IPConfigTable ,has udpClient close it`);
// globalThis.udpClient.closeUdp(async ()=>{ })
// }
globalThis.udpClient = {}
// globalThis.udpClient.onMessage_1 = () => {}
globalThis.host = ''
reslove('')
}
})
2024-01-05 11:11:15 +08:00
})
2024-07-19 09:21:47 +08:00
2024-01-05 11:11:15 +08:00
}
2025-02-11 09:09:34 +08:00
export async function getUDP2(context, errorFlag?) {
2024-08-10 14:00:18 +08:00
const fileUtil = new FileUtil(context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
if (data === '' || data === undefined) {
globalThis.udpClient2 = {}
globalThis.udpClient2.setMsgCallBack = () => {
}
2025-02-11 09:09:34 +08:00
} else {
const result = JSON.parse(data)
2024-08-10 14:00:18 +08:00
console.log(` getUDP2 has IPConfigTable `);
if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
console.log(` getUDP2 has udclent ,close and rebind `);
globalThis.udpClient2.closeUdp(async () => {
setTimeout(() => {
2025-02-11 09:09:34 +08:00
globalThis.udpClient2.rebindUdp(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress,
globalThis.carInfo?.messagePort)
globalThis.udpClient2.onError_Callback(() => {
getUDP2(context, true);
if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) {
globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2
2024-08-10 14:00:18 +08:00
}
})
}, 1000)
})
2025-02-11 09:09:34 +08:00
} else {
2024-08-10 14:00:18 +08:00
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
2025-02-11 09:09:34 +08:00
const udpClient2: UdpClientByCenter =
new UdpClientByCenter(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress,
globalThis.carInfo?.messagePort)
2024-11-18 15:16:42 +08:00
console.info('surenjun udp2=> ', globalThis.carInfo?.messagePort)
2024-08-10 14:00:18 +08:00
await udpClient2.bindUdp()
2025-02-11 09:09:34 +08:00
await udpClient2.onError_Callback(() => {
getUDP2(context, true);
if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) {
globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2
2024-08-10 14:00:18 +08:00
}
})
2025-02-11 09:09:34 +08:00
if (!errorFlag) {
2024-08-10 14:00:18 +08:00
await udpClient2.onMessage_2((val) => {
if (val.id == '32') {
globalThis.signNum = val.body[1]
2025-02-11 09:09:34 +08:00
if (val.body[0] == 5) {
2024-11-18 15:16:42 +08:00
console.info('surenjun', `接收到平台扣分请求,请求指令为:${val.body[1]}`)
globalThis.udpEvent.sendOnKf(val.body[1])
2024-09-03 19:28:07 +08:00
}
2025-02-11 09:09:34 +08:00
if (val.body[0] == 11) {
console.info('surenjun', 'GlobalUdp收到beginExam事件')
2024-11-18 15:16:42 +08:00
globalThis.udpEvent.sendBeginExam(val.body[1])
2024-09-03 19:28:07 +08:00
}
2025-02-11 09:09:34 +08:00
if (val.body[0] == 12) {
console.info('surenjun', 'GlobalUdp收到endExam事件')
2024-11-18 15:16:42 +08:00
globalThis.udpEvent.sendEndExam(val.body[1])
2024-09-03 19:28:07 +08:00
}
2025-02-11 09:09:34 +08:00
}
if (val.id == '36') {
2024-11-18 15:16:42 +08:00
//获取远程扣分内容
2025-02-11 09:09:34 +08:00
console.info('surenjun', '考车收到远程扣分项目内容,扣分代码:' + val.body[0])
2024-11-18 15:16:42 +08:00
globalThis.udpEvent.sendKfContent(val.body[0])
2024-08-10 14:00:18 +08:00
} else if (val.id == '46') {
let tmpList = []
const str = globalThis.lsh
for (let i = 0; i < str.length; i++) {
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
}
const param = {
id: 47,
list: tmpList,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
}
globalThis.udpClient2.send(param)
}
})
}
globalThis.udpClient2 = udpClient2
}
}
return
2024-07-09 11:11:31 +08:00
console.log(` getUDP2 enter`);
2024-07-09 13:13:59 +08:00
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
if (result.length) {
2024-07-09 11:11:31 +08:00
console.log(` getUDP2 has IPConfigTable `);
2024-07-09 13:13:59 +08:00
if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
console.log(` getUDP2 has udclent ,close and rebind `);
globalThis.udpClient2.closeUdp(async () => {
setTimeout(() => {
2025-02-11 09:09:34 +08:00
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress,
globalThis.carInfo?.messagePort)
globalThis.udpClient2.onError_Callback(() => {
2024-08-07 08:57:40 +08:00
getUDP2(true);
2025-02-11 09:09:34 +08:00
if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) {
globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2
2024-08-07 08:57:40 +08:00
}
})
2024-07-09 13:13:59 +08:00
}, 1000)
})
2025-02-11 09:09:34 +08:00
} else {
2024-07-09 11:11:31 +08:00
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
2025-02-11 09:09:34 +08:00
const udpClient2: UdpClientByCenter =
new UdpClientByCenter(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress,
globalThis.carInfo?.messagePort)
2024-07-16 17:23:42 +08:00
await udpClient2.bindUdp()
2025-02-11 09:09:34 +08:00
await udpClient2.onError_Callback(() => {
2024-08-07 08:57:40 +08:00
getUDP2(true);
2025-02-11 09:09:34 +08:00
if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) {
globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2
2024-01-05 11:11:15 +08:00
}
})
2025-02-11 09:09:34 +08:00
if (!errorFlag) {
2024-08-07 08:57:40 +08:00
await udpClient2.onMessage_2((val) => {
if (val.id == '32') {
globalThis.signNum = val.body[1]
} else if (val.id == '46') {
let tmpList = []
const str = globalThis.lsh
for (let i = 0; i < str.length; i++) {
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
}
const param = {
id: 47,
list: tmpList,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
}
globalThis.udpClient2.send(param)
}
})
}
2024-07-09 11:11:31 +08:00
globalThis.udpClient2 = udpClient2
2024-08-07 08:57:40 +08:00
2024-01-05 11:11:15 +08:00
}
2024-07-09 13:13:59 +08:00
} else {
globalThis.udpClient2 = {}
globalThis.udpClient2.setMsgCallBack = () => {
2024-07-09 11:11:31 +08:00
}
2024-01-05 11:11:15 +08:00
}
})
}
2024-02-20 10:22:33 +08:00
2024-07-09 13:13:59 +08:00
export async function setTopLineUdp() {
2024-08-10 14:00:18 +08:00
const fileUtil = new FileUtil(globalThis.context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
const config = JSON.parse(data)
// const config = await getSyncData('IpConfigTable');
if (config && config.udplocalIp) {
2025-02-11 09:09:34 +08:00
const { udplocalIp, udpOppositeIp, udpOppositeIpPort } = config;
2024-07-09 13:13:59 +08:00
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
2024-01-05 11:11:15 +08:00
udpClient.bindUdp()
return {
2024-07-09 13:13:59 +08:00
send(bytes) {
2024-01-05 11:11:15 +08:00
udpClient.sendHeadMsg(bytes)
}
}
}
}
2024-05-09 13:42:56 +08:00
//
2024-07-09 13:13:59 +08:00
export async function setJudgeUdp() {
2024-08-10 14:00:18 +08:00
const fileUtil = new FileUtil(globalThis.context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
const config = JSON.parse(data)
2024-05-09 13:42:56 +08:00
2024-08-10 14:00:18 +08:00
//
// const config = await getSyncData('IpConfigTable');
2024-05-09 13:42:56 +08:00
let udpIndex = 0;
let currentUdpIndex = 0;
let judgeUdpTimer
2024-08-15 09:01:09 +08:00
clearInterval(globalThis.judgeUdpTimer)
globalThis.judgeUdpTimer = setInterval(() => {
2024-05-09 13:42:56 +08:00
udpIndex += 1;
2024-07-09 13:13:59 +08:00
}, 1000);
2024-05-09 13:42:56 +08:00
2024-08-10 14:00:18 +08:00
if (config && config.udplocalIp) {
2025-02-11 09:09:34 +08:00
const { udplocalIp } = config;
2024-09-03 19:28:07 +08:00
const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(
udplocalIp, '8080',
globalThis.carInfo?.udpAddress,
globalThis.carInfo?.hintPort
)
2025-02-11 09:09:34 +08:00
console.info('surenjun hintPort=> ', globalThis.carInfo?.hintPort)
2024-09-02 19:23:56 +08:00
2025-02-11 09:09:34 +08:00
await udpClientbyCenter.onError_Callback(() => {
})
2024-07-16 17:23:42 +08:00
await udpClientbyCenter.bindUdp()
2025-02-11 09:09:34 +08:00
await udpClientbyCenter.onMessage_2((val) => {
})
2024-09-03 19:28:07 +08:00
//远程监听开始考试、结束考试、扣分指令
2024-07-09 13:13:59 +08:00
// globalThis.judgeUdpClient = udpClientbyCenter;
2024-01-05 11:11:15 +08:00
return {
2024-07-09 13:13:59 +08:00
send(bytes) {
if (udpIndex > currentUdpIndex) {
2024-08-15 09:01:09 +08:00
udpClientbyCenter.sendMsgExt({
id: 45,
2024-07-09 13:13:59 +08:00
list: bytes,
carNo: globalThis.carInfo.carNo,
2024-08-15 09:01:09 +08:00
placeId: globalThis.carInfo.examinationRoomId
})
2024-05-09 13:42:56 +08:00
currentUdpIndex = udpIndex
}
2024-09-03 19:28:07 +08:00
},
2024-11-18 15:16:42 +08:00
//申请远程扣分查询
2025-02-11 09:09:34 +08:00
askKf(directives) {
2024-11-18 15:16:42 +08:00
globalThis.udpClient2.sendMsgExt({
id: 35,
list: [directives],
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId,
})
console.info('surenjun', `考车查询扣分项目内容,请求指令为:${directives}`)
},
2024-09-03 19:28:07 +08:00
2024-11-18 15:16:42 +08:00
//确定远程扣分
2025-02-11 09:09:34 +08:00
confirmKf(directives, code) {
2024-11-18 15:16:42 +08:00
globalThis.udpClient2.sendMsgExt({
id: 37,
2025-02-11 09:09:34 +08:00
list: Array2Byte([string2Bytes(directives, 8), string2Bytes(code, 8)]),
2024-11-18 15:16:42 +08:00
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
})
console.info('surenjun', `考车发送确定扣分指令,指令为:${directives}`)
2024-09-03 19:28:07 +08:00
},
2024-01-05 11:11:15 +08:00
}
}
}
2025-02-11 09:09:34 +08:00
2024-01-05 11:11:15 +08:00
// globalThis.udpClient.bindUdp()