From 0b2b653b8e265098004bd601a469b9e238080795 Mon Sep 17 00:00:00 2001 From: lvyuankang <1344032923@qq.com> Date: Tue, 5 Nov 2024 09:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6dev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/TcpClient.ts | 61 ++++++++++++++++---- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/entry/src/main/ets/common/utils/TcpClient.ts b/entry/src/main/ets/common/utils/TcpClient.ts index eea1e52d..44fde4cb 100644 --- a/entry/src/main/ets/common/utils/TcpClient.ts +++ b/entry/src/main/ets/common/utils/TcpClient.ts @@ -13,6 +13,7 @@ export default class TcpClient { private oppositeIp: string = '' private oppositeIpPort: string = '' private num: number = 0//重连次数 + private folderPath private tcp: any = null @@ -23,6 +24,7 @@ export default class TcpClient { this.oppositeIpPort = tcpOppositePort console.log(TAG, 'new Tcp', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) this.tcp = socket.constructTCPSocketInstance(); + this.initPath() } onError(callback?) { @@ -72,6 +74,10 @@ export default class TcpClient { } bindTcp() { + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} tcpbind localIp${this.localIp} ${localIpPort}`, + }) console.log(TAG, 'tcpbind', this.localIp, 'localIp', this.localIpPort) return new Promise((resolve, reject) => { let promise = this.tcp.bind({ @@ -82,6 +88,10 @@ export default class TcpClient { // 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)}`, + }) console.log('testTag tcp bind faile'); globalThis.getCloseTcp = true hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err)); @@ -109,6 +119,10 @@ export default class TcpClient { } connectTcp() { + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} tcpConnect oppositeIp ${ this.oppositeIp} ${this.oppositeIpPort}`, + }) console.log(TAG, 'tcpConnect', this.oppositeIp, 'localIp', this.oppositeIpPort) return new Promise((resolve, reject) => { let promise = this.tcp.connect({ @@ -122,6 +136,10 @@ export default class TcpClient { keepAlive: true, }, err => { if (err) { + 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`, @@ -141,6 +159,10 @@ export default class TcpClient { globalThis.getCloseTcp = false resolve(true) }).catch(err => { + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} TCPconnect error2 ${JSON.stringify(err)}`, + }) globalThis.getCloseTcp = true this.num++ if(this.num>3){ @@ -167,6 +189,10 @@ export default class TcpClient { promise.then(() => { reslove(true) }).catch(err => { + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} TCPsend error ${JSON.stringify(err)}`, + }) reslove(false) }); }) @@ -180,7 +206,10 @@ export default class TcpClient { // }) console.log(TAG, 'Tcponmessage', value) globalThis.tcpUdpError = false - + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} Tcponmessage ,${value}`, + }) // console.log('messageLengt',,value.message.length)) console.log('testTagtcpmsg') // console.log("on message, message:" + value.message+ ", remoteInfo:" ) @@ -204,18 +233,23 @@ export default class TcpClient { duration: 2000 }); } + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} tcp信号丢失`, + }) globalThis.tcpUdpError = true; },3000) // callback(value.message) }); } - async writeLog(param){ - // return - // const fileUtil = new FileUtil(globalThis.context) - // const date=dateFormat(new Date).split(' ')[0] - // const folderPath = await fileUtil.initFolder(`/PLC/${date}`); - // fileUtil.editFile(`${folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`) - + async writeLog(param){ + const fileUtil = new FileUtil(globalThis.context) + fileUtil.editFile(`${this.folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`) + } + async initPath(){ + const fileUtil = new FileUtil(globalThis.context) + const date=dateFormat(new Date).split(' ')[0] + this.folderPath = await fileUtil.initFolder(`/PLC/${date}`); } offTcp(callback) { console.log(TAG, 'tcpofff') @@ -225,17 +259,24 @@ export default class TcpClient { } closeTcp(callback) { + return new Promise((reslove, reject) => { console.log(TAG, 'tcpClose') let promise = this.tcp.close(); promise.then(() => { globalThis.getCloseTcp = true - + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} tcp closeSuccess`, + }) console.log(TAG, 'tcpCloseSuccess') callback() reslove(true) }).catch(err => { - console.log(TAG, 'tcpClosefailed') + this.writeLog({ + time:dateFormat(new Date()), + message: `${TAG} tcpclose error ${JSON.stringify(err)}`, + }) reslove(false) }); })