Compare commits

...

2 Commits

3 changed files with 41 additions and 72 deletions

View File

@ -4,6 +4,7 @@ import hilog from '@ohos.hilog';
import FileUtil from '../../common/utils/File'
import { GlobalConfig } from '../../config/index'
export async function getTCP(flag=false) {
globalThis.getCloseTcp=true
const fileUtil = new FileUtil(globalThis.context)
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
if (data === '' || data === undefined) {
@ -22,6 +23,17 @@ export async function getTCP(flag=false) {
await globalThis.TcpClient.bindTcp()
// await globalThis.TcpClient.rebindTcp(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
await globalThis.TcpClient.connectTcp()
console.log('ttttttt0')
// await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt00')
clearInterval(globalThis.intervalSendmsg)
globalThis.intervalSendmsg=setInterval(()=>{
console.log('getCloseTcp',globalThis.carInfo.carNo)
if(!globalThis.getCloseTcp){
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
}
},1000/3)
globalThis.TcpClient.onError((val) => {
setTimeout(() => {
getTCP()
@ -46,9 +58,13 @@ export async function getTCP(flag=false) {
globalThis.TcpClient = tcpClient
await globalThis.TcpClient.bindTcp()
await globalThis.TcpClient.connectTcp()
console.log('ttttttt1')
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt12')
clearInterval(globalThis.intervalSendmsg)
globalThis.intervalSendmsg=setInterval(()=>{
// console.log('getCloseTcp',globalThis.getCloseTcp)
if(!globalThis.getCloseTcp){
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
}

View File

@ -33,13 +33,12 @@ export default class TcpClient {
// time:dateFormat(new Date()),
// PLC:`${TAG} tcpOnerror ${JSON.stringify(err)}`,
// })
console.log(TAG, 'getCloseTtcpOnerror0000', JSON.stringify(err))
globalThis.getCloseTcp = true
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} tcponerror localIp${JSON.stringify(err)}`,
})
console.log(TAG, 'tcpOnerror', JSON.stringify(err))
console.log(TAG, 'getCloseTtcpOnerror', JSON.stringify(err))
setTimeout(async () => {
getTCP()
}, 2000)
@ -49,46 +48,7 @@ export default class TcpClient {
});
}
rebindTcp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
return new Promise((resolve, reject) => {
this.localIp = localIp
this.oppositeIp = oppositeIp
this.localIpPort = localIpPort
this.oppositeIpPort = oppositeIpPort
console.log(TAG, 'tcpreBind', this.localIp, this.localIpPort)
let promise = this.tcp.bind({
address: this.localIp, port: parseInt(this.localIpPort), family: 1
}, err => {
if (err) {
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} tcprebind tcpreBinderror${JSON.stringify(err)}`,
})
setTimeout(async () => {
getTCP()
}, 2000)
// globalThis.getCloseTcp = true
hilog.info(0x0000, 'testTag', "tcpreBinderror:" + JSON.stringify(err));
resolve(true)
}
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} tcprebind success`,
})
console.log('testTag,rebindtestTag tcp bind success');
// globalThis.getCloseTcp = false
resolve(false)
})
})
// let promise = this.udp.bind({
// // address: '192.168.7.170', port: 20122, family: 1
// // address: '192.168.7.170', port: 31013, family: 1
// address: this.localIp, port: parseInt(this.localIpPort), family: 1
// });
}
bindTcp() {
this.writeLog({
@ -101,10 +61,6 @@ export default class TcpClient {
address: this.localIp, port: parseInt(this.localIpPort), family: 1
}, err => {
if (err) {
// this.writeLog({
// time:dateFormat(new Date()),
// PLC:`${TAG} testTag tcp bind faile ${JSON.stringify(err)}`,
// })
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} tcpbind error ${JSON.stringify(err)}`,
@ -112,12 +68,14 @@ export default class TcpClient {
setTimeout(async () => {
getTCP()
}, 2000)
console.log('testTag tcp bind faile');
console.log('getCloseTBinderror');
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
resolve(true)
return
}
console.log('testTag tcp bind success');
console.log('getCloseTBindsuccess');
// console.log('testTag tcp bind success');
// this.writeLog({
// time:dateFormat(new Date()),
// PLC:`${TAG} testTag tcp bind success`,
@ -125,14 +83,6 @@ export default class TcpClient {
resolve(false)
})
})
console.log('localIp', this.localIp)
console.log('localIpPort', this.localIpPort)
// let promise = this.udp.bind({
// // address: '192.168.7.170', port: 20122, family: 1
// // address: '192.168.7.170', port: 31013, family: 1
// address: this.localIp, port: parseInt(this.localIpPort), family: 1
// });
}
@ -154,28 +104,21 @@ export default class TcpClient {
keepAlive: true,
}, err => {
if (err) {
console.log('getCloseTconnectsuccess','error',globalThis.getCloseTcp)
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} TCPconnect error ${JSON.stringify(err)}`,
})
// this.writeLog({
// time:dateFormat(new Date()),
// PLC:`${TAG} tcp connect rebind success`,
// })
setTimeout(() => {
this.writeLog({
time:dateFormat(new Date()),
message: `${TAG} tcp重连`,
})
getTCP()
resolve(false)
}, 9000)
return;
}
});
console.log('socketTag','success')
console.log('getCloseTconnectsuccess');
console.log('getCloseTcpsocketTagconnect','success',globalThis.getCloseTcp)
globalThis.getCloseTcp = false
resolve(true)
}).catch(err => {
@ -207,6 +150,14 @@ export default class TcpClient {
promise.then(() => {
reslove(true)
}).catch(err => {
if(!globalThis.getCloseTcp){
setTimeout(async () => {
getTCP(true)
}, 3000)
return
}
globalThis.getCloseTcp=true
console.log(`${TAG} TCPsend error ${JSON.stringify(err)}`)
// this.writeLog({
// time:dateFormat(new Date()),
@ -270,8 +221,6 @@ export default class TcpClient {
}
offTcp(callback) {
console.log(TAG, 'tcpofff')
this.tcp.off('testTagofmessg', callback);
}
@ -285,6 +234,8 @@ export default class TcpClient {
time:dateFormat(new Date()),
message: `${TAG} tcp closeSuccess`,
})
console.log('getCloseTcpsocketTagclose','success',globalThis.getCloseTcp)
console.log(TAG, 'tcpCloseSuccess')
callback()
reslove(true)
@ -296,6 +247,8 @@ export default class TcpClient {
time:dateFormat(new Date()),
message: `${TAG} tcpclose error ${JSON.stringify(err)}`,
})
console.log('getCloseTcpsocketTagclose','error',globalThis.getCloseTcp)
reslove(false)
});
})

View File

@ -466,7 +466,7 @@ struct Index {
globalThis.singlePlay = false
}
this.isSingle = globalThis.singlePlay
this.loading = false
// this.loading = false
this.createAlbum()
this.num = 0
// const TcpClient: TcpClient =new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)