去除udp提示信息

This commit is contained in:
lixiao 2025-03-31 17:09:50 +08:00
parent b22af41722
commit e6ba764364

View File

@ -20,9 +20,6 @@ import FileUtil from '../../common/utils/File'
import { fillZero, string2Bytes } from '../utils/tools' import { fillZero, string2Bytes } from '../utils/tools'
const TAG = '[UdpDemo.UdpClient]' const TAG = '[UdpDemo.UdpClient]'
import hilog from '@ohos.hilog';
import prompt from '@ohos.prompt'
import promptAction from '@ohos.promptAction';
import { getUDP } from './GlobalUdp'; import { getUDP } from './GlobalUdp';
import { dateFormat } from '../utils/tools' import { dateFormat } from '../utils/tools'
import { getSyncData } from '../service/initable'; import { getSyncData } from '../service/initable';
@ -43,15 +40,16 @@ export default class UdpClientByCenter {
private context private context
private folderPath private folderPath
private stashFn: StashFunction private stashFn: StashFunction
private errorStep: number=0 private errorStep: number = 0
private interval private interval
private headLenth: number = 9 private headLenth: number = 9
//消息头长度 //消息头长度
private isWorking: Boolean = false private isWorking: Boolean = false
private plcUdpError = false; private plcUdpError = false;
private initParam private initParam
private onMessage_1Callback:Function = ()=>{} private onMessage_1Callback: Function = () => {
public currentValue:string = '' }
public currentValue: string = ''
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
this.localIp = udplocalIp this.localIp = udplocalIp
@ -68,8 +66,7 @@ export default class UdpClientByCenter {
return this.isWorking 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.localIp = localIp
this.oppositeIp = oppositeIp this.oppositeIp = oppositeIp
this.localIpPort = localIpPort this.localIpPort = localIpPort
@ -90,10 +87,10 @@ export default class UdpClientByCenter {
}).catch(err => { }).catch(err => {
//globalThis.closeHeartSocket=true //globalThis.closeHeartSocket=true
this.isWorking = false this.isWorking = false
// this.writeLog({ // this.writeLog({
// time:dateFormat(new Date()), // time:dateFormat(new Date()),
// PLC:`${TAG} getUDPudp rebind failed:${JSON.stringify(err)}`, // PLC:`${TAG} getUDPudp rebind failed:${JSON.stringify(err)}`,
// }) // })
console.log(`${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') console.log('getUDPsendHeadMsg exit')
} }
sendMsg(msg, sendCallback?) sendMsg(msg, sendCallback?) {
{ if (!this.isWorking) {
if (!this.isWorking ) {
// console.log(`${TAG}getUDPudpCLient sendMsg is closed return `); // console.log(`${TAG}getUDPudpCLient sendMsg is closed return `);
// this.writeLog({ // this.writeLog({
// time:dateFormat(new Date()), // time:dateFormat(new Date()),
// PLC:`${TAG}getUDPudpCLient sendMsg is closed return oppositeIp:${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort:${this.localIpPort}`, // PLC:`${TAG}getUDPudpCLient sendMsg is closed return oppositeIp:${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort:${this.localIpPort}`,
// }) // })
if(sendCallback) { if (sendCallback) {
sendCallback() sendCallback()
} }
return; return;
@ -195,19 +191,8 @@ export default class UdpClientByCenter {
if (sendCallback) { if (sendCallback) {
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 => { }).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 : () => { this.stashFn = callback ? callback : () => {
} }
} }
//中心udp回执 //中心udp回执
onMessage_2(callback, type?) { onMessage_2(callback, type?) {
this.udp.on('message', (value, remoteInfo) => { this.udp.on('message', (value, remoteInfo) => {
@ -268,7 +254,6 @@ export default class UdpClientByCenter {
}) })
// this.interval=setInterval(()=>{ // this.interval=setInterval(()=>{
// //
// },3000) // },3000)
@ -300,16 +285,16 @@ export default class UdpClientByCenter {
//plc //plc
onMessage_1(callback?) { onMessage_1(callback?) {
this.onMessage_1Callback = 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监听事件') 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监听事件 成功') console.info('surenjun', 'getUDP关闭udp message监听事件 成功')
} }
message_1Fn = (value)=>{ message_1Fn = (value) => {
let callback = this.onMessage_1Callback let callback = this.onMessage_1Callback
// 收到的是ArrayBuffer 需要进行转换解析 // 收到的是ArrayBuffer 需要进行转换解析
this.plcUdpError = false this.plcUdpError = false
@ -336,15 +321,15 @@ export default class UdpClientByCenter {
// time:dateFormat(new Date()), // time:dateFormat(new Date()),
// PLC: JSON.stringify(newArr.toString()), // PLC: JSON.stringify(newArr.toString()),
// }) // })
if(strachArr[83]!='4'){ if (strachArr[83] != '4') {
console.log('差分状态异常',strachArr[83],strachArr[92]) console.log('差分状态异常', strachArr[83], strachArr[92])
this.writeLog({ this.writeLog({
time:dateFormat(new Date()), time: dateFormat(new Date()),
PLC: `${TAG}差分状态异常,${strachArr[83]},${strachArr[92]}`, PLC: `${TAG}差分状态异常,${strachArr[83]},${strachArr[92]}`,
}) })
}else{ } else {
globalThis.dialogOpen=false globalThis.dialogOpen = false
this.chafenFlag=0 this.chafenFlag = 0
} }
callback && callback(newArr.toString()) callback && callback(newArr.toString())
this.currentValue = newArr.toString(); this.currentValue = newArr.toString();
@ -354,15 +339,18 @@ export default class UdpClientByCenter {
this.testIfUdpConnetced(callback) this.testIfUdpConnetced(callback)
} }
async writeLog(param){
async writeLog(param) {
// const fileUtil = new FileUtil(globalThis.context) // const fileUtil = new FileUtil(globalThis.context)
// fileUtil.editFile(`${this.folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`) // fileUtil.editFile(`${this.folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`)
} }
async initPath(){
async initPath() {
// const fileUtil = new FileUtil(globalThis.context) // const fileUtil = new FileUtil(globalThis.context)
// const date=dateFormat(new Date).split(' ')[0] // const date=dateFormat(new Date).split(' ')[0]
// this.folderPath = await fileUtil.initFolder(`/PLC/${date}`); // this.folderPath = await fileUtil.initFolder(`/PLC/${date}`);
} }
//获取当前UDP信号 //获取当前UDP信号
getCurrentMessage = () => { getCurrentMessage = () => {
return this.currentValue return this.currentValue
@ -376,7 +364,7 @@ export default class UdpClientByCenter {
const arrRedBuffer = Array2Byte(arrRed).buffer const arrRedBuffer = Array2Byte(arrRed).buffer
const arrGreenBugger = Array2Byte(arrGreen).buffer const arrGreenBugger = Array2Byte(arrGreen).buffer
let num = 0 let num = 0
globalThis.dialogOpen=false globalThis.dialogOpen = false
//监听udp是否断开 //监听udp是否断开
clearInterval(globalThis.messageTimer) clearInterval(globalThis.messageTimer)
@ -389,15 +377,15 @@ export default class UdpClientByCenter {
if (this.plcUdpError) { if (this.plcUdpError) {
num++ num++
this.writeLog({ this.writeLog({
time:dateFormat(new Date()), time: dateFormat(new Date()),
PLC: 'plc udp信号丢失', PLC: 'plc udp信号丢失',
}) })
console.log(TAG, 'plc udp信号丢失') console.log(TAG, 'plc udp信号丢失')
if (num == 3) { if (num == 3) {
getUDP(globalThis.context,true) getUDP(globalThis.context, true)
globalThis.title='plc udp信号丢失' globalThis.title = 'plc udp信号丢失'
globalThis.type='3' globalThis.type = '3'
if(!globalThis.dialogOpen){ if (!globalThis.dialogOpen) {
// AppStorage.SetOrCreate('errorCode', 1); // AppStorage.SetOrCreate('errorCode', 1);
// if(this.errorStep!=1){ // if(this.errorStep!=1){
// console.log('sys.v_valuesys.v_valuesys.v_value11221') // console.log('sys.v_valuesys.v_valuesys.v_value11221')
@ -425,11 +413,6 @@ export default class UdpClientByCenter {
} }
num = 0 num = 0
} }
prompt.showToast({
message: 'plc udp信号丢失',
duration: 2000
});
} }
this.plcUdpError = true; this.plcUdpError = true;