2025-02-17 10:30:31 +08:00

261 lines
9.1 KiB
TypeScript

//import UdpClient from './UdpClient';
import UdpClientByCenter from './UdpClientByCenter';
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';
export async function sendMsg(val) {
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
}
// obj
export async function getUDP(context, errorFlag?) {
return new Promise(async (reslove, reject) => {
const fileUtil = new FileUtil(context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
console.log('ttttt', data)
if (data === '' || data === undefined) {
console.log(` getUDP has no IPConfigTable `);
globalThis.udpClient = {}
reslove('')
} else {
const result = JSON.parse(data)
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.udplocalIp, result.udplocalIpPort, result.udpOppositeIp,
result.udpOppositeIpPort)
globalThis.udpClient.sendMsg('111', null)
AppStorage.setOrCreate('host',`http://${result.centerIp}:${result.centerPort}`)
globalThis.udpClient.onError_Callback(() => {
console.log('getUDPgetUDPgetUDP')
getUDP(context, true)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
}
})
reslove(`http://${result.centerIp}:${result.centerPort}`)
// globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
}, 1000)
})
} else {
// 未绑定
console.log(` getUDP has no udclent and bind `);
console.log('ttttttdata', JSON.stringify(result))
const udpClient: UdpClientByCenter =
new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp,
result.udpOppositeIpPort)
udpClient.bindUdp()
udpClient.sendMsg('111')
AppStorage.setOrCreate('host',`http://${result.centerIp}:${result.centerPort}`)
globalThis.udpClient = udpClient
globalThis.udpClient.onMessage_1(() => {
console.log('getUDPonMessage_1msgmsgByGloalUdp')
})
globalThis.udpClient.onError_Callback(() => {
getUDP(context, true)
if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) {
globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1
}
})
getChuankouFn()
reslove(`http://${result.centerIp}:${result.centerPort}`)
}
}
})
}
// 中心
export async function getUDP2(context, errorFlag?) {
const fileUtil = new FileUtil(context)
const carInfo=AppStorage.get('carInfo')
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
if (data === '' || data === undefined) {
globalThis.udpClient2 = {}
globalThis.udpClient2.setMsgCallBack = () => {
}
} else {
const result = JSON.parse(data)
console.log(` getUDP2 has IPConfigTable `);
if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
console.log(` getUDP2 has udclent ,close and rebind `);
globalThis.udpClient2.closeUdp(async () => {
setTimeout(() => {
globalThis.udpClient2.rebindUdp(result.udplocalIp, '8800', carInfo?.udpAddress,
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
}
})
}, 1000)
})
} else {
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
const udpClient2: UdpClientByCenter =
new UdpClientByCenter(result.udplocalIp, '8800', carInfo?.udpAddress,
carInfo?.messagePort)
console.info('surenjun udp2=> ', carInfo?.messagePort)
await udpClient2.bindUdp()
await udpClient2.onError_Callback(() => {
getUDP2(context, true);
if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) {
globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2
}
})
if (!errorFlag) {
await udpClient2.onMessage_2((val) => {
if (val.id == '32') {
AppStorage.setOrCreate('signNum',val.body[1])
if (val.body[0] == 5) {
console.info('surenjun', `接收到平台扣分请求,请求指令为:${val.body[1]}`)
globalThis.udpEvent.sendOnKf(val.body[1])
}
if (val.body[0] == 11) {
console.info('surenjun', 'GlobalUdp收到beginExam事件')
globalThis.udpEvent.sendBeginExam(val.body[1])
}
if (val.body[0] == 12) {
console.info('surenjun', 'GlobalUdp收到endExam事件')
globalThis.udpEvent.sendEndExam(val.body[1])
}
}
if (val.id == '36') {
//获取远程扣分内容
console.info('surenjun', '考车收到远程扣分项目内容,扣分代码:' + val.body[0])
globalThis.udpEvent.sendKfContent(val.body[0])
} else if (val.id == '46') {
let tmpList = []
const str = AppStorage.get('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: carInfo.carNo,
placeId: carInfo.examinationRoomId
}
globalThis.udpClient2.send(param)
}
})
}
globalThis.udpClient2 = udpClient2
}
}
}
// 灯光
export async function setTopLineUdp() {
const context=AppStorage.get('context')
const fileUtil = new FileUtil(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) {
const { udplocalIp, udpOppositeIp, udpOppositeIpPort } = config;
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
udpClient.bindUdp()
return {
send(bytes) {
udpClient.sendHeadMsg(bytes)
}
}
}
}
//
let judgeUdpTimer
// 评判
export async function setJudgeUdp() {
const context=AppStorage.get('context')
const fileUtil = new FileUtil(context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
const config = JSON.parse(data)
const carInfo=AppStorage.get('carInfo')
//
// const config = await getSyncData('IpConfigTable');
let udpIndex = 0;
let currentUdpIndex = 0;
clearInterval(judgeUdpTimer)
judgeUdpTimer = setInterval(() => {
udpIndex += 1;
}, 1000);
if (config && config.udplocalIp) {
const { udplocalIp } = config;
const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(
udplocalIp, '8080',
carInfo?.udpAddress,
carInfo?.hintPort
)
console.info('surenjun hintPort=> ', carInfo?.hintPort)
await udpClientbyCenter.onError_Callback(() => {
})
await udpClientbyCenter.bindUdp()
await udpClientbyCenter.onMessage_2((val) => {
})
//远程监听开始考试、结束考试、扣分指令
// globalThis.judgeUdpClient = udpClientbyCenter;
return {
send(bytes) {
if (udpIndex > currentUdpIndex) {
udpClientbyCenter.sendMsgExt({
id: 45,
list: bytes,
carNo: carInfo.carNo,
placeId: carInfo.examinationRoomId
})
currentUdpIndex = udpIndex
}
},
//申请远程扣分查询
askKf(directives) {
globalThis.udpClient2.sendMsgExt({
id: 35,
list: [directives],
carNo: carInfo.carNo,
placeId: carInfo.examinationRoomId,
})
console.info('surenjun', `考车查询扣分项目内容,请求指令为:${directives}`)
},
//确定远程扣分
confirmKf(directives, code) {
globalThis.udpClient2.sendMsgExt({
id: 37,
list: Array2Byte([string2Bytes(directives, 8), string2Bytes(code, 8)]),
carNo: carInfo.carNo,
placeId: carInfo.examinationRoomId
})
console.info('surenjun', `考车发送确定扣分指令,指令为:${directives}`)
},
}
}
}
// globalThis.udpClient.bindUdp()