2024-01-05 11:11:15 +08:00
|
|
|
import TcpClient from './TcpClient';
|
2024-05-30 15:52:03 +08:00
|
|
|
import { getSyncData } from '../service/initable'
|
2024-05-16 09:53:10 +08:00
|
|
|
import hilog from '@ohos.hilog';
|
2024-01-05 11:11:15 +08:00
|
|
|
|
2024-05-30 15:52:03 +08:00
|
|
|
export async function getTCP(flag?) {
|
|
|
|
|
|
|
|
|
|
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
|
|
|
|
|
console.log('result222', JSON.stringify(result))
|
|
|
|
|
if (result.length) {
|
|
|
|
|
if (globalThis.TcpClient && globalThis.TcpClient.closeTcp) {
|
|
|
|
|
globalThis.TcpClient.closeTcp(async () => {
|
|
|
|
|
if(!flag){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort)
|
2024-05-27 17:25:20 +08:00
|
|
|
await globalThis.TcpClient.connectTcp()
|
2024-05-30 15:52:03 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
2024-05-27 17:25:20 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val));
|
|
|
|
|
|
|
|
|
|
// console.log('valvalval',val)
|
2024-05-30 15:52:03 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) {
|
2024-05-27 17:25:20 +08:00
|
|
|
// const msg=val.substring(5,val.length-1)
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
2024-05-30 15:52:03 +08:00
|
|
|
}, 1000)
|
2024-05-27 17:25:20 +08:00
|
|
|
|
|
|
|
|
})
|
2024-05-30 15:52:03 +08:00
|
|
|
globalThis.TcpClient.onError(async () => {
|
|
|
|
|
await globalThis.TcpClient.rebindTcp(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort)
|
|
|
|
|
await globalThis.TcpClient.connectTcp()
|
|
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
|
|
|
|
hilog.info(0x0000, 'testTag', "valvalval1" + JSON.stringify(val));
|
|
|
|
|
|
|
|
|
|
// console.log('valvalval',val)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) {
|
|
|
|
|
// const msg=val.substring(5,val.length-1)
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
},1000)
|
2024-01-05 11:11:15 +08:00
|
|
|
})
|
2024-05-16 09:53:10 +08:00
|
|
|
|
2024-05-29 10:13:37 +08:00
|
|
|
}
|
2024-05-30 15:52:03 +08:00
|
|
|
else {
|
2024-01-05 11:11:15 +08:00
|
|
|
// prompt.showToast({
|
|
|
|
|
// message:'初始化localIp'+ result[0].localIp+ result[0].localIpPort+'oppositeIp'+result[0].oppositeIp+result[0].oppositeIpPort,
|
|
|
|
|
// duration: 1000
|
|
|
|
|
// });
|
2024-05-30 15:52:03 +08:00
|
|
|
const tcpClient: TcpClient = new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort)
|
2024-01-05 11:11:15 +08:00
|
|
|
globalThis.TcpClient = tcpClient
|
|
|
|
|
await globalThis.TcpClient.bindTcp()
|
|
|
|
|
await globalThis.TcpClient.connectTcp()
|
2024-05-30 15:52:03 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
2024-05-27 17:25:20 +08:00
|
|
|
|
2024-05-30 15:52:03 +08:00
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
2024-05-16 09:53:10 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val));
|
2024-05-30 15:52:03 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) {
|
2024-01-05 11:11:15 +08:00
|
|
|
// const msg=val.substring(5,val.length-1)
|
2024-05-16 09:53:10 +08:00
|
|
|
globalThis.udpClient?.sendMsg(val)
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
2024-05-30 15:52:03 +08:00
|
|
|
}, 1000)
|
2024-01-05 11:11:15 +08:00
|
|
|
|
|
|
|
|
})
|
2024-05-30 15:52:03 +08:00
|
|
|
globalThis.TcpClient.onError(async () => {
|
|
|
|
|
const tcpClient: TcpClient = new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp, result[0].tcpOppositePort)
|
2024-05-27 17:25:20 +08:00
|
|
|
globalThis.TcpClient = tcpClient
|
|
|
|
|
await globalThis.TcpClient.bindTcp()
|
|
|
|
|
await globalThis.TcpClient.connectTcp()
|
2024-05-30 15:52:03 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
2024-05-27 17:25:20 +08:00
|
|
|
|
2024-05-30 15:52:03 +08:00
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
2024-05-27 17:25:20 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val));
|
2024-05-30 15:52:03 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
globalThis.TcpClient.sendMsg(globalThis.carInfo?.carNo || '402') //1002
|
|
|
|
|
if (val && globalThis.udpClient && globalThis.udpClient.sendMsg) {
|
2024-05-27 17:25:20 +08:00
|
|
|
// const msg=val.substring(5,val.length-1)
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
2024-05-30 15:52:03 +08:00
|
|
|
}, 1000)
|
2024-05-27 17:25:20 +08:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
// globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort)
|
|
|
|
|
|
2024-05-30 15:52:03 +08:00
|
|
|
} else {
|
|
|
|
|
globalThis.TcpClient = {}
|
|
|
|
|
globalThis.TcpClient.onMessage = () => {
|
|
|
|
|
}
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// globalThis.udpClient.bindUdp()
|