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 hilog from '@ohos.hilog';
import FileUtil from '../../common/utils/File'; import FileUtil from '../../common/utils/File';
import { GlobalConfig } from '../../config/index'; import { GlobalConfig } from '../../config/index';
let intervalSendmsg
export async function getTCP(flag = false) { export async function getTCP(flag = false) {
globalThis.getCloseTcp = true globalThis.getCloseTcp = true
const context= AppStorage.get('context') const context= AppStorage.get('context')
@ -30,11 +30,10 @@ export async function getTCP(flag = false) {
// await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002 // await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt00') console.log('ttttttt00')
clearInterval(globalThis.intervalSendmsg) clearInterval(intervalSendmsg)
globalThis.intervalSendmsg = setInterval(() => { intervalSendmsg = setInterval(() => {
console.log('getCloseTcp', globalThis.carInfo.carNo)
if (!globalThis.getCloseTcp) { if (!globalThis.getCloseTcp) {
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002 globalThis.TcpClient.sendMsg(AppStorage.get('carInfo').carNo) //1002
} }
}, 1000 / 3) }, 1000 / 3)
globalThis.TcpClient.onError((val) => { globalThis.TcpClient.onError((val) => {
@ -65,8 +64,8 @@ export async function getTCP(flag = false) {
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002 await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
console.log('ttttttt12') console.log('ttttttt12')
clearInterval(globalThis.intervalSendmsg) clearInterval(intervalSendmsg)
globalThis.intervalSendmsg = setInterval(() => { intervalSendmsg = setInterval(() => {
// console.log('getCloseTcp',globalThis.getCloseTcp) // console.log('getCloseTcp',globalThis.getCloseTcp)
if (!globalThis.getCloseTcp) { if (!globalThis.getCloseTcp) {
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002 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() { export async function setJudgeUdp() {
const context=AppStorage.get('context') const context=AppStorage.get('context')
const fileUtil = new FileUtil(context) const fileUtil = new FileUtil(context)
@ -190,9 +191,8 @@ export async function setJudgeUdp() {
// const config = await getSyncData('IpConfigTable'); // const config = await getSyncData('IpConfigTable');
let udpIndex = 0; let udpIndex = 0;
let currentUdpIndex = 0; let currentUdpIndex = 0;
let judgeUdpTimer clearInterval(judgeUdpTimer)
clearInterval(globalThis.judgeUdpTimer) judgeUdpTimer = setInterval(() => {
globalThis.judgeUdpTimer = setInterval(() => {
udpIndex += 1; udpIndex += 1;
}, 1000); }, 1000);

View File

@ -44,7 +44,7 @@ export default class UdpClientByCenter {
private folderPath private folderPath
private stashFn: StashFunction private stashFn: StashFunction
private errorStep: number=0 private errorStep: number=0
private interval private messageTimer:number=-1
private headLenth: number = 9 private headLenth: number = 9
//消息头长度 //消息头长度
private isWorking: Boolean = false private isWorking: Boolean = false
@ -375,8 +375,8 @@ export default class UdpClientByCenter {
let num = 0 let num = 0
//监听udp是否断开 //监听udp是否断开
clearInterval(globalThis.messageTimer) clearInterval(this.messageTimer)
globalThis.messageTimer = setInterval(() => { this.messageTimer = setInterval(() => {
const lightLineUdp = globalThis.lightLineUdp const lightLineUdp = globalThis.lightLineUdp
const isJudge = AppStorage.get('isJudge') const isJudge = AppStorage.get('isJudge')
setTimeout(async () => { 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 interval: number = 0
@State @Watch('outClick') outFlag: boolean = false; @State @Watch('outClick') outFlag: boolean = false;
@State url: string = '' @State url: string = ''
@State signalTimer: number = -1
constructor() { constructor() {
super() super()
@ -333,8 +334,8 @@ export default struct SignDisplayCom {
} }
}) })
} else { } else {
clearInterval(globalThis.signalTimer) clearInterval(this.signalTimer)
globalThis.signalTimer = setInterval(() => { this.signalTimer = setInterval(() => {
const msgStr = AppStorage.get('msgStr') const msgStr = AppStorage.get('msgStr')
if (msgStr) { if (msgStr) {
getSignal(msgStr) getSignal(msgStr)
@ -357,8 +358,8 @@ export default struct SignDisplayCom {
getSignal(msg) getSignal(msg)
}) })
} else { } else {
clearInterval(globalThis.signalTimer) clearInterval(this.signalTimer)
globalThis.signalTimer = setInterval(() => { this.signalTimer = setInterval(() => {
//TODO 临时方案 //TODO 临时方案
const msgStr = AppStorage.get('msgStr') const msgStr = AppStorage.get('msgStr')
getSignal(msgStr) getSignal(msgStr)

View File

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