surenjun 45c6b12067 Merge branch 'main' into feat_surenjun
# Conflicts:
#	entry/src/main/ets/common/utils/UdpClientByCenter.ts
#	entry/src/main/ets/pages/TerminalInfos.ets
#	entry/src/main/ets/pages/UserInfo.ets
2024-08-15 09:05:19 +08:00

324 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//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'
export async function sendMsg(val) {
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
}
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 = {}
globalThis.host = ''
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)
globalThis.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')
globalThis.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}`)
}
}
return
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}`
globalThis.udpClient.onError_Callback(()=>{
console.log('getUDPgetUDPgetUDP')
getUDP(true,context)
if(errorFlag&&globalThis.udpClient&&globalThis.udpClient.onMessage_1){
globalThis.udpClient.onMessage_1=globalThis.udpClient.onMessage_1
}
})
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}`
globalThis.udpClient = udpClient
globalThis.udpClient.onMessage_1(()=>{
console.log('getUDPonMessage_1msgmsgByGloalUdp')
})
globalThis.udpClient.onError_Callback(()=>{
getUDP(true,context)
if(errorFlag&&globalThis.udpClient&&globalThis.udpClient.onMessage_1){
globalThis.udpClient.onMessage_1=globalThis.udpClient.onMessage_1
}
})
// udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
getChuankouFn()
reslove(`http://${result[0].centerIp}:${result[0].centerPort}`)
}
} 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('')
}
})
})
}
export async function getUDP2(context,errorFlag?) {
const fileUtil = new FileUtil(context)
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', 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
}
})
}, 1000)
})
}
else {
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
const udpClient2: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.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) => {
console.log('getUDPOnmessageByGlobalUDP2Message')
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)
}
})
}
globalThis.udpClient2 = udpClient2
}
}
return
console.log(` getUDP2 enter`);
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
if (result.length) {
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[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
globalThis.udpClient2.onError_Callback(()=>{
getUDP2(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[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
await udpClient2.bindUdp()
await udpClient2.onError_Callback(()=>{
getUDP2(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') {
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)
}
})
}
globalThis.udpClient2 = udpClient2
}
} else {
globalThis.udpClient2 = {}
globalThis.udpClient2.setMsgCallBack = () => {
}
}
})
}
export async function setTopLineUdp() {
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) {
const {udplocalIp,udpOppositeIp,udpOppositeIpPort} = config;
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
udpClient.bindUdp()
return {
send(bytes) {
udpClient.sendHeadMsg(bytes)
}
}
}
}
//
export async function setJudgeUdp() {
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');
let udpIndex = 0;
let currentUdpIndex = 0;
let judgeUdpTimer
clearInterval(globalThis.judgeUdpTimer)
globalThis.judgeUdpTimer = setInterval(() => {
udpIndex += 1;
}, 1000);
if (config && config.udplocalIp) {
const {udplocalIp} = config;
const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080', globalThis.carInfo?.gpsAddress, globalThis.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: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
})
currentUdpIndex = udpIndex
}
}
}
}
}
// globalThis.udpClient.bindUdp()