158 lines
5.5 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';
2024-07-09 13:13:59 +08:00
import { getSyncData } from '../service/initable'
import { getChuankouFn } from '../../common/service/indexService'
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
2024-06-04 13:14:48 +08:00
export async function getUDP() {
2024-07-19 09:21:47 +08:00
return new Promise((reslove,reject)=>{
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(() => {
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
globalThis.udpClient.sendMsg('111', null)
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
reslove(`http://${result[0].centerIp}:${result[0].centerPort}`)
// globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
}, 1000)
})
}
else {
// 未绑定
console.log(` getUDP has no udclent and bind `);
const udpClient: UdpClientByCenter = new UdpClientByCenter(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
udpClient.bindUdp()
udpClient.sendMsg('111')
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
// udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
globalThis.udpClient = udpClient
getChuankouFn()
reslove(`http://${result[0].centerIp}:${result[0].centerPort}`)
}
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
}
2024-06-04 13:14:48 +08:00
export async function getUDP2() {
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(() => {
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
}, 1000)
})
2024-07-09 11:11:31 +08:00
}
2024-07-09 13:13:59 +08:00
else {
2024-07-09 11:11:31 +08:00
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
2024-07-09 13:13:59 +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()
await udpClient2.onError_Callback()
await udpClient2.onMessage_2((val) => {
2024-07-09 13:13:59 +08:00
if (val.id == '32') {
globalThis.signNum = val.body[1]
2024-03-12 15:32:48 +08:00
2024-07-09 13:13:59 +08:00
} else if (val.id == '46') {
2024-01-05 11:11:15 +08:00
let tmpList = []
const str = globalThis.lsh
2024-07-09 13:13:59 +08:00
for (let i = 0; i < str.length; i++) {
2024-01-05 11:11:15 +08:00
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
}
2024-07-09 13:13:59 +08:00
const param = {
id: 47,
list: tmpList,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
}
2024-01-05 11:11:15 +08:00
globalThis.udpClient2.send(param)
}
})
2024-07-09 11:11:31 +08:00
globalThis.udpClient2 = udpClient2
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-01-05 11:11:15 +08:00
const config = await getSyncData('IpConfigTable');
2024-07-09 13:13:59 +08:00
if (config && config[0] && config[0].udplocalIp) {
2024-01-05 11:11:15 +08:00
const {udplocalIp,udpOppositeIp,udpOppositeIpPort} = config[0];
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-01-05 11:11:15 +08:00
const config = await getSyncData('IpConfigTable');
2024-05-09 13:42:56 +08:00
let udpIndex = 0;
let currentUdpIndex = 0;
let judgeUdpTimer
clearInterval(judgeUdpTimer)
2024-07-09 13:13:59 +08:00
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-07-09 13:13:59 +08:00
if (config && config[0] && config[0].udplocalIp) {
2024-02-23 15:23:48 +08:00
const {udplocalIp} = config[0];
2024-07-09 13:13:59 +08:00
const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080', globalThis.carInfo?.gpsAddress, globalThis.carInfo?.hintPort)
2024-07-16 17:23:42 +08:00
await udpClientbyCenter.bindUdp()
await udpClientbyCenter.onMessage_1((val) => {
2024-07-09 13:13:59 +08:00
console.log('valval', val)
2024-01-05 11:11:15 +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) {
udpClientbyCenter.sendMsgExt({ id: 45,
list: bytes,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId })
2024-05-09 13:42:56 +08:00
currentUdpIndex = udpIndex
}
2024-01-05 11:11:15 +08:00
}
}
}
}
// globalThis.udpClient.bindUdp()