From e6ba764364f006de32eb47cd47c23bfeae570f99 Mon Sep 17 00:00:00 2001 From: lixiao <932184220@qq.com> Date: Mon, 31 Mar 2025 17:09:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4udp=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/common/utils/UdpClientByCenter.ts | 87 ++++++++----------- 1 file changed, 35 insertions(+), 52 deletions(-) diff --git a/entry/src/main/ets/common/utils/UdpClientByCenter.ts b/entry/src/main/ets/common/utils/UdpClientByCenter.ts index d1891209..8635352d 100644 --- a/entry/src/main/ets/common/utils/UdpClientByCenter.ts +++ b/entry/src/main/ets/common/utils/UdpClientByCenter.ts @@ -20,9 +20,6 @@ import FileUtil from '../../common/utils/File' import { fillZero, string2Bytes } from '../utils/tools' const TAG = '[UdpDemo.UdpClient]' -import hilog from '@ohos.hilog'; -import prompt from '@ohos.prompt' -import promptAction from '@ohos.promptAction'; import { getUDP } from './GlobalUdp'; import { dateFormat } from '../utils/tools' import { getSyncData } from '../service/initable'; @@ -43,15 +40,16 @@ export default class UdpClientByCenter { private context private folderPath private stashFn: StashFunction - private errorStep: number=0 + private errorStep: number = 0 private interval private headLenth: number = 9 //消息头长度 private isWorking: Boolean = false private plcUdpError = false; private initParam - private onMessage_1Callback:Function = ()=>{} - public currentValue:string = '' + private onMessage_1Callback: Function = () => { + } + public currentValue: string = '' constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { this.localIp = udplocalIp @@ -68,8 +66,7 @@ export default class UdpClientByCenter { return this.isWorking } - rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) - { + rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) { this.localIp = localIp this.oppositeIp = oppositeIp this.localIpPort = localIpPort @@ -90,10 +87,10 @@ export default class UdpClientByCenter { }).catch(err => { //globalThis.closeHeartSocket=true this.isWorking = false - // this.writeLog({ - // time:dateFormat(new Date()), - // PLC:`${TAG} getUDPudp rebind failed:${JSON.stringify(err)}`, - // }) + // this.writeLog({ + // time:dateFormat(new Date()), + // PLC:`${TAG} getUDPudp rebind failed:${JSON.stringify(err)}`, + // }) console.log(`${TAG} getUDPudp rebind failed:${JSON.stringify(err)}`); }); } @@ -171,15 +168,14 @@ export default class UdpClientByCenter { console.log('getUDPsendHeadMsg exit') } - sendMsg(msg, sendCallback?) - { - if (!this.isWorking ) { + sendMsg(msg, sendCallback?) { + if (!this.isWorking) { // console.log(`${TAG}getUDPudpCLient sendMsg is closed return `); // this.writeLog({ // time:dateFormat(new Date()), // PLC:`${TAG}getUDPudpCLient sendMsg is closed return oppositeIp:${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort:${this.localIpPort}`, // }) - if(sendCallback) { + if (sendCallback) { sendCallback() } return; @@ -195,19 +191,8 @@ export default class UdpClientByCenter { if (sendCallback) { sendCallback() } - // this.writeLog({ - // time:dateFormat(new Date()), - // PLC:`${TAG}getUDPudpCLient udp send success: oppositeIp:${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort:${this.localIpPort}`, - // }) }).catch(err => { - // this.writeLog({ - // time:dateFormat(new Date()), - // PLC:`${TAG}getUDPudpCLient udp send fail:oppositeIp${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort,${this.localIpPort}${JSON.stringify(err)}`, - // }) - promptAction.showToast({ - message:`${TAG}getUDPudpCLient udp send fail:oppositeIp${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort,${this.localIpPort}`, - duration:4000 - }) + }); } @@ -243,6 +228,7 @@ export default class UdpClientByCenter { this.stashFn = callback ? callback : () => { } } + //中心udp回执 onMessage_2(callback, type?) { this.udp.on('message', (value, remoteInfo) => { @@ -268,7 +254,6 @@ export default class UdpClientByCenter { }) - // this.interval=setInterval(()=>{ // // },3000) @@ -300,16 +285,16 @@ export default class UdpClientByCenter { //plc onMessage_1(callback?) { this.onMessage_1Callback = callback; - this.udp&&this.udp.on('message', this.message_1Fn); + this.udp && this.udp.on('message', this.message_1Fn); } - closeMessage_1(){ + closeMessage_1() { console.info('surenjun', 'getUDP关闭udp message监听事件') - this.udp.off('message',this.message_1Fn); + this.udp.off('message', this.message_1Fn); console.info('surenjun', 'getUDP关闭udp message监听事件 成功') } - message_1Fn = (value)=>{ + message_1Fn = (value) => { let callback = this.onMessage_1Callback // 收到的是ArrayBuffer 需要进行转换解析 this.plcUdpError = false @@ -336,15 +321,15 @@ export default class UdpClientByCenter { // time:dateFormat(new Date()), // PLC: JSON.stringify(newArr.toString()), // }) - if(strachArr[83]!='4'){ - console.log('差分状态异常',strachArr[83],strachArr[92]) + if (strachArr[83] != '4') { + console.log('差分状态异常', strachArr[83], strachArr[92]) this.writeLog({ - time:dateFormat(new Date()), + time: dateFormat(new Date()), PLC: `${TAG}差分状态异常,${strachArr[83]},${strachArr[92]}`, }) - }else{ - globalThis.dialogOpen=false - this.chafenFlag=0 + } else { + globalThis.dialogOpen = false + this.chafenFlag = 0 } callback && callback(newArr.toString()) this.currentValue = newArr.toString(); @@ -354,15 +339,18 @@ export default class UdpClientByCenter { this.testIfUdpConnetced(callback) } - async writeLog(param){ + + async writeLog(param) { // const fileUtil = new FileUtil(globalThis.context) // fileUtil.editFile(`${this.folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`) } - async initPath(){ + + async initPath() { // const fileUtil = new FileUtil(globalThis.context) // const date=dateFormat(new Date).split(' ')[0] // this.folderPath = await fileUtil.initFolder(`/PLC/${date}`); } + //获取当前UDP信号 getCurrentMessage = () => { return this.currentValue @@ -376,7 +364,7 @@ export default class UdpClientByCenter { const arrRedBuffer = Array2Byte(arrRed).buffer const arrGreenBugger = Array2Byte(arrGreen).buffer let num = 0 - globalThis.dialogOpen=false + globalThis.dialogOpen = false //监听udp是否断开 clearInterval(globalThis.messageTimer) @@ -389,15 +377,15 @@ export default class UdpClientByCenter { if (this.plcUdpError) { num++ this.writeLog({ - time:dateFormat(new Date()), + time: dateFormat(new Date()), PLC: 'plc udp信号丢失', }) console.log(TAG, 'plc udp信号丢失') if (num == 3) { - getUDP(globalThis.context,true) - globalThis.title='plc udp信号丢失' - globalThis.type='3' - if(!globalThis.dialogOpen){ + getUDP(globalThis.context, true) + globalThis.title = 'plc udp信号丢失' + globalThis.type = '3' + if (!globalThis.dialogOpen) { // AppStorage.SetOrCreate('errorCode', 1); // if(this.errorStep!=1){ // console.log('sys.v_valuesys.v_valuesys.v_value11221') @@ -425,11 +413,6 @@ export default class UdpClientByCenter { } num = 0 } - - prompt.showToast({ - message: 'plc udp信号丢失', - duration: 2000 - }); } this.plcUdpError = true;