2024-01-05 11:11:15 +08:00
|
|
|
import TcpClient from './TcpClient';
|
2024-07-30 10:35:41 +08:00
|
|
|
import { getSyncData } from '../service/initable';
|
2024-05-16 09:53:10 +08:00
|
|
|
import hilog from '@ohos.hilog';
|
2024-08-10 14:00:18 +08:00
|
|
|
import FileUtil from '../../common/utils/File'
|
|
|
|
|
import { GlobalConfig } from '../../config/index'
|
2025-06-25 10:19:26 +08:00
|
|
|
const TAG = 'socketTag[TcpDemo.TcpClient]'
|
2024-12-24 16:43:59 +08:00
|
|
|
export async function getTCP(flag=false) {
|
2025-01-02 17:53:34 +08:00
|
|
|
globalThis.getCloseTcp=true
|
2024-08-10 14:00:18 +08:00
|
|
|
const fileUtil = new FileUtil(globalThis.context)
|
|
|
|
|
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
|
2025-03-17 13:46:02 +08:00
|
|
|
|
2024-08-10 14:00:18 +08:00
|
|
|
if (data === '' || data === undefined) {
|
|
|
|
|
globalThis.TcpClient = {}
|
|
|
|
|
globalThis.TcpClient.onMessage = () => {
|
|
|
|
|
}
|
|
|
|
|
}else{
|
2025-03-25 10:31:44 +08:00
|
|
|
const result = JSON.parse(data)
|
2025-05-07 15:22:03 +08:00
|
|
|
const netOpen = result.netOpen
|
|
|
|
|
//是否开启网络差分
|
|
|
|
|
if(netOpen != 1){
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-12-24 16:43:59 +08:00
|
|
|
if (globalThis.TcpClient && globalThis.TcpClient.closeTcp&&!flag) {
|
2024-08-10 14:00:18 +08:00
|
|
|
globalThis.TcpClient.closeTcp(async () => {
|
|
|
|
|
// const tcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)
|
|
|
|
|
// globalThis.TcpClient = tcpClient
|
|
|
|
|
setTimeout(async () => {
|
2024-12-20 13:28:03 +08:00
|
|
|
const tcpClient: TcpClient = new TcpClient(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
|
|
|
|
globalThis.TcpClient = tcpClient
|
|
|
|
|
await globalThis.TcpClient.bindTcp()
|
|
|
|
|
// await globalThis.TcpClient.rebindTcp(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
2024-08-10 14:00:18 +08:00
|
|
|
await globalThis.TcpClient.connectTcp()
|
2025-01-02 17:53:34 +08:00
|
|
|
console.log('ttttttt0')
|
|
|
|
|
// await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
|
|
|
|
console.log('ttttttt00')
|
|
|
|
|
|
|
|
|
|
clearInterval(globalThis.intervalSendmsg)
|
2025-06-25 10:19:26 +08:00
|
|
|
globalThis.intervalSendmsg=setInterval(async()=>{
|
2025-01-02 17:53:34 +08:00
|
|
|
console.log('getCloseTcp',globalThis.carInfo.carNo)
|
|
|
|
|
if(!globalThis.getCloseTcp){
|
2025-06-25 10:19:26 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
|
|
|
|
globalThis.tcpStep+=1
|
|
|
|
|
if(globalThis.tcpStep>30){
|
|
|
|
|
console.log(TAG,'tcp重连开始')
|
|
|
|
|
globalThis.tcpStep=0
|
|
|
|
|
getTCP()
|
|
|
|
|
}
|
2025-01-02 17:53:34 +08:00
|
|
|
}
|
|
|
|
|
},1000/3)
|
2024-08-10 14:00:18 +08:00
|
|
|
globalThis.TcpClient.onError((val) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
getTCP()
|
|
|
|
|
}, 1000)
|
|
|
|
|
})
|
|
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (val) {
|
2025-05-07 15:22:03 +08:00
|
|
|
globalThis.retClass?.sendDiffCorrections(val)
|
2024-08-10 14:00:18 +08:00
|
|
|
}
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}, 2000)
|
|
|
|
|
})
|
2024-07-03 16:19:55 +08:00
|
|
|
|
2024-08-10 14:00:18 +08:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const tcpClient: TcpClient = new TcpClient(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
|
|
|
|
globalThis.TcpClient = tcpClient
|
|
|
|
|
await globalThis.TcpClient.bindTcp()
|
|
|
|
|
await globalThis.TcpClient.connectTcp()
|
2025-01-02 17:53:34 +08:00
|
|
|
console.log('ttttttt1')
|
2024-11-05 09:47:58 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
2025-01-02 17:53:34 +08:00
|
|
|
console.log('ttttttt12')
|
|
|
|
|
|
2024-12-20 13:28:03 +08:00
|
|
|
clearInterval(globalThis.intervalSendmsg)
|
2025-06-25 10:19:26 +08:00
|
|
|
globalThis.intervalSendmsg=setInterval(async()=>{
|
2025-01-02 17:53:34 +08:00
|
|
|
// console.log('getCloseTcp',globalThis.getCloseTcp)
|
2024-12-20 13:28:03 +08:00
|
|
|
if(!globalThis.getCloseTcp){
|
2025-06-25 10:19:26 +08:00
|
|
|
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
|
|
|
|
globalThis.tcpStep+=1
|
|
|
|
|
if(globalThis.tcpStep>30){
|
|
|
|
|
console.log(TAG,'tcp重连开始')
|
|
|
|
|
globalThis.tcpStep=0
|
|
|
|
|
getTCP()
|
|
|
|
|
}
|
2024-12-20 13:28:03 +08:00
|
|
|
}
|
|
|
|
|
},1000/3)
|
2024-08-10 14:00:18 +08:00
|
|
|
globalThis.TcpClient.onError((val) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
getTCP()
|
|
|
|
|
}, 1000)
|
|
|
|
|
})
|
|
|
|
|
await globalThis.TcpClient.onMessage((val) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (val && globalThis.udpClient?.sendMsg) {
|
2025-05-07 15:22:03 +08:00
|
|
|
globalThis.retClass?.sendDiffCorrections(val)
|
2024-08-10 14:00:18 +08:00
|
|
|
}
|
|
|
|
|
}, 1000)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// globalThis.udpClient.bindUdp()
|