globalThis 修改

This commit is contained in:
lvyuankang 2025-02-14 13:12:46 +08:00
parent 4dda9cae91
commit eba191b4e0
6 changed files with 28 additions and 82 deletions

View File

@ -3,7 +3,7 @@ import { getSyncData } from '../service/initable';
import hilog from '@ohos.hilog';
import FileUtil from '../../common/utils/File';
import { GlobalConfig } from '../../config/index';
let intervalSendmsg
export async function getTCP(flag = false) {
globalThis.getCloseTcp = true
const context= AppStorage.get('context')
@ -30,11 +30,10 @@ export async function getTCP(flag = false) {
// await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt00')
clearInterval(globalThis.intervalSendmsg)
globalThis.intervalSendmsg = setInterval(() => {
console.log('getCloseTcp', globalThis.carInfo.carNo)
clearInterval(intervalSendmsg)
intervalSendmsg = setInterval(() => {
if (!globalThis.getCloseTcp) {
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
globalThis.TcpClient.sendMsg(AppStorage.get('carInfo').carNo) //1002
}
}, 1000 / 3)
globalThis.TcpClient.onError((val) => {
@ -65,8 +64,8 @@ export async function getTCP(flag = false) {
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt12')
clearInterval(globalThis.intervalSendmsg)
globalThis.intervalSendmsg = setInterval(() => {
clearInterval(intervalSendmsg)
intervalSendmsg = setInterval(() => {
// console.log('getCloseTcp',globalThis.getCloseTcp)
if (!globalThis.getCloseTcp) {
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
@ -87,71 +86,7 @@ export async function getTCP(flag = false) {
})
}
}
return
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
console.log('result222', JSON.stringify(result))
if (result.length) {
if (globalThis.TcpClient && globalThis.TcpClient.closeTcp) {
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)
await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg('1002') //1002
globalThis.TcpClient.onError((val) => {
setTimeout(() => {
getTCP()
}, 1000)
})
await globalThis.TcpClient.onMessage((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)
})
}, 2000)
})
} else {
const tcpClient: TcpClient =
new TcpClient(result[0].tcplocalIp, result[0].tcplocalIpPort, result[0].tcpOppositeIp,
result[0].tcpOppositePort)
globalThis.TcpClient = tcpClient
await globalThis.TcpClient.bindTcp()
await globalThis.TcpClient.connectTcp()
await globalThis.TcpClient.sendMsg('1002') //1002
globalThis.TcpClient.onError((val) => {
setTimeout(() => {
getTCP()
}, 1000)
})
await globalThis.TcpClient.onMessage((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)
})
}
// globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort)
} else {
globalThis.TcpClient = {}
globalThis.TcpClient.onMessage = () => {
}
}
})
}

View File

@ -180,6 +180,7 @@ export async function setTopLineUdp() {
}
//
let judgeUdpTimer
export async function setJudgeUdp() {
const context=AppStorage.get('context')
const fileUtil = new FileUtil(context)
@ -190,9 +191,8 @@ export async function setJudgeUdp() {
// const config = await getSyncData('IpConfigTable');
let udpIndex = 0;
let currentUdpIndex = 0;
let judgeUdpTimer
clearInterval(globalThis.judgeUdpTimer)
globalThis.judgeUdpTimer = setInterval(() => {
clearInterval(judgeUdpTimer)
judgeUdpTimer = setInterval(() => {
udpIndex += 1;
}, 1000);

View File

@ -44,7 +44,7 @@ export default class UdpClientByCenter {
private folderPath
private stashFn: StashFunction
private errorStep: number=0
private interval
private messageTimer:number=-1
private headLenth: number = 9
//消息头长度
private isWorking: Boolean = false
@ -375,8 +375,8 @@ export default class UdpClientByCenter {
let num = 0
//监听udp是否断开
clearInterval(globalThis.messageTimer)
globalThis.messageTimer = setInterval(() => {
clearInterval(this.messageTimer)
this.messageTimer = setInterval(() => {
const lightLineUdp = globalThis.lightLineUdp
const isJudge = AppStorage.get('isJudge')
setTimeout(async () => {

View File

@ -0,0 +1,11 @@
export default class SingleInterval{
private interval:number=-1
constructor(time:number,callback) {
this.interval = setInterval(()=>{
callback()
},time||6000)
}
clear(){
clearInterval(this.interval)
}
}

View File

@ -20,6 +20,7 @@ export default struct SignDisplayCom {
@State interval: number = 0
@State @Watch('outClick') outFlag: boolean = false;
@State url: string = ''
@State signalTimer: number = -1
constructor() {
super()
@ -333,8 +334,8 @@ export default struct SignDisplayCom {
}
})
} else {
clearInterval(globalThis.signalTimer)
globalThis.signalTimer = setInterval(() => {
clearInterval(this.signalTimer)
this.signalTimer = setInterval(() => {
const msgStr = AppStorage.get('msgStr')
if (msgStr) {
getSignal(msgStr)
@ -357,8 +358,8 @@ export default struct SignDisplayCom {
getSignal(msg)
})
} else {
clearInterval(globalThis.signalTimer)
globalThis.signalTimer = setInterval(() => {
clearInterval(this.signalTimer)
this.signalTimer = setInterval(() => {
//TODO 临时方案
const msgStr = AppStorage.get('msgStr')
getSignal(msgStr)

View File

@ -81,7 +81,6 @@ export default struct RealTime {
.onDestroy(() => {
apiJudgeSdk.examJudgeMapSetDrawing(false); //停止绘制地图轨迹false:表示结束绘制
this.draw = false;
clearInterval(globalThis.realTimer)
})
} else {
Column() {