2024-01-05 11:11:15 +08:00
|
|
|
import TcpClient from './TcpClient';
|
2024-07-03 16:19:55 +08:00
|
|
|
import { getSyncData} from '../service/initable'
|
2024-05-16 09:53:10 +08:00
|
|
|
import hilog from '@ohos.hilog';
|
2024-07-03 16:19:55 +08:00
|
|
|
export async function getTCP() {
|
|
|
|
|
|
|
|
|
|
getSyncData('IpConfigTable').then(async (result:Array<any>)=>{
|
|
|
|
|
console.log('result222',JSON.stringify(result))
|
|
|
|
|
if(result.length){
|
2024-07-05 09:08:17 +08:00
|
|
|
if(globalThis.TcpClient&&globalThis.getCloseTcp){
|
2024-07-03 16:19:55 +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 ()=>{
|
|
|
|
|
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-07-03 16:19:55 +08:00
|
|
|
await globalThis.TcpClient.sendMsg('1002')//1002
|
2024-07-19 13:27:51 +08:00
|
|
|
globalThis.TcpClient.onError((val)=>{
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
globalThis.TcpClient.sendMsg('1002')//1002
|
|
|
|
|
if(val){
|
|
|
|
|
// const msg=val.substring(5,val.length-1)
|
|
|
|
|
console.log('socketTag[PLC.UdpClient] status:',globalThis.udpClient.getStatus())
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
|
|
|
|
},1000)
|
|
|
|
|
})
|
2024-07-03 16:19:55 +08:00
|
|
|
await globalThis.TcpClient.onMessage((val)=>{
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
globalThis.TcpClient.sendMsg('1002')//1002
|
2024-07-09 11:11:31 +08:00
|
|
|
if(val){
|
2024-05-27 17:25:20 +08:00
|
|
|
// const msg=val.substring(5,val.length-1)
|
2024-07-09 11:11:31 +08:00
|
|
|
console.log('socketTag[PLC.UdpClient] status:',globalThis.udpClient.getStatus())
|
2024-05-27 17:25:20 +08:00
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
2024-07-03 16:19:55 +08:00
|
|
|
},1000)
|
2024-05-30 15:52:03 +08:00
|
|
|
|
|
|
|
|
})
|
2024-07-03 16:19:55 +08:00
|
|
|
},2000)
|
2024-01-05 11:11:15 +08:00
|
|
|
})
|
2024-05-16 09:53:10 +08:00
|
|
|
|
2024-07-03 16:19:55 +08:00
|
|
|
}else{
|
|
|
|
|
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-07-03 16:19:55 +08:00
|
|
|
await globalThis.TcpClient.sendMsg('1002')//1002
|
2024-07-19 13:27:51 +08:00
|
|
|
globalThis.TcpClient.onError((val)=>{
|
|
|
|
|
hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val));
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
globalThis.TcpClient.sendMsg('1002')//1002
|
|
|
|
|
if(val&& globalThis.udpClient?.sendMsg){
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
|
|
|
|
}
|
|
|
|
|
},1000)
|
|
|
|
|
})
|
2024-07-03 16:19:55 +08:00
|
|
|
await globalThis.TcpClient.onMessage((val)=>{
|
2024-05-16 09:53:10 +08:00
|
|
|
hilog.info(0x0000, 'testTag', "valvalval2" + JSON.stringify(val));
|
2024-07-03 16:19:55 +08:00
|
|
|
setTimeout(()=>{
|
|
|
|
|
globalThis.TcpClient.sendMsg('1002')//1002
|
2024-07-09 11:11:31 +08:00
|
|
|
if(val&& globalThis.udpClient?.sendMsg){
|
|
|
|
|
globalThis.udpClient?.sendMsg(val)
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
2024-07-03 16:19:55 +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-07-03 16:19:55 +08:00
|
|
|
}else{
|
|
|
|
|
globalThis.TcpClient={}
|
|
|
|
|
globalThis.TcpClient.onMessage=()=>{}
|
2024-01-05 11:11:15 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// globalThis.udpClient.bindUdp()
|