From 0f950217375c58a840323f24e18fe2f19b0e5f22 Mon Sep 17 00:00:00 2001 From: wangzhongjie Date: Fri, 13 Jun 2025 13:23:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20TCP=20UDP=E4=B8=AD=E5=BF=83=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 16 ++---- entry/src/main/ets/pages/Index/utils.ets | 1 + entry/src/main/ets/utils/SerialNumber.ets | 25 +++++++++ entry/src/main/ets/utils/UdpUtils.ets | 1 + .../ets/utils/business/CenterUdpBusiness.ets | 6 +- .../ets/workers/DifferentialCorrection.ets | 56 +++++++++---------- 6 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 entry/src/main/ets/utils/SerialNumber.ets diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 12cd017..799108a 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -78,13 +78,10 @@ struct Index { async onPageShow(): Promise { console.log("首页 onPageShow") await UseAuth(this.context) - // DifferentialAndSignal.init() - DifferentialAndSignal.onMsg(() => { - console.log("外层接受") - }) this.avPlayer.playAudio(['welcome.wav']) this.baseInfo = AppStorage.get('baseInfo')! this.initParams() + AppStorage.setOrCreate('singlePlay', false) this.num = 0 AppStorage.setOrCreate('lsh', '1111111111111') @@ -181,12 +178,6 @@ struct Index { console.log("test1111") await GetDeviceInfo(this.context) this.carInfo = await GetCarInfo() - // TODO - // CenterUDPBusinessInstance.onMsg((data: CenterCallBackMsgType) => { - // if (data.id == 32) { - // AppStorage.setOrCreate('signNum', data.body[1]) - // } - // },) this.carInfo = AppStorage.get('carInfo')! this.deviceId = this.carInfo.carNo || "" await SetCurrentTime() @@ -199,6 +190,11 @@ struct Index { // TODO // CenterUDPBusinessInstance.startHeartBeat() CreateAlbum(this.fileHelper) + // 启动worker通道 + DifferentialAndSignal.init() + DifferentialAndSignal.onMsg(() => { + console.log("外层接受") + }) } build() { diff --git a/entry/src/main/ets/pages/Index/utils.ets b/entry/src/main/ets/pages/Index/utils.ets index e51d476..5db51f3 100644 --- a/entry/src/main/ets/pages/Index/utils.ets +++ b/entry/src/main/ets/pages/Index/utils.ets @@ -53,6 +53,7 @@ export async function GetCarInfo(): Promise { if (res.obtainCarExamInfoRsp && res.obtainCarExamInfoRsp.body) { const carInfo: ObtainCarExamInfoRspBody = res?.obtainCarExamInfoRsp?.body! carInfo.plateNo = decodeURIComponent(carInfo.plateNo) + console.log("Worker received message car", JSON.stringify(carInfo)) AppStorage.setOrCreate('carInfo', carInfo) return carInfo } diff --git a/entry/src/main/ets/utils/SerialNumber.ets b/entry/src/main/ets/utils/SerialNumber.ets new file mode 100644 index 0000000..730085b --- /dev/null +++ b/entry/src/main/ets/utils/SerialNumber.ets @@ -0,0 +1,25 @@ +// 流水号生成获取 +class SerialNumber { + private serialNumber: string = "000000"; + + // 生成流水号(外部手动调用) + generate() { + let num = Number(this.serialNumber) + 1; + if (num > 999999) { + num = 0; // 超过 999999 时重置为 000000 + } + this.serialNumber = num.toString().padStart(6, "0"); + } + + // 获取当前流水号 + get() { + return this.serialNumber; + } + + // 重置流水号(可选) + reset() { + this.serialNumber = "000000"; + } +} + +export const SerialNumberInstance = new SerialNumber(); \ No newline at end of file diff --git a/entry/src/main/ets/utils/UdpUtils.ets b/entry/src/main/ets/utils/UdpUtils.ets index ed1cad8..71a238f 100644 --- a/entry/src/main/ets/utils/UdpUtils.ets +++ b/entry/src/main/ets/utils/UdpUtils.ets @@ -139,6 +139,7 @@ export default class UdpClient { private bindEvent() { this.udp?.on("message", value => { let result = this.dealMethod?.(value.message) + console.log(UDPTag, "中心返回消息", result) this.messageEvents.forEach(cb => { cb(result) }) diff --git a/entry/src/main/ets/utils/business/CenterUdpBusiness.ets b/entry/src/main/ets/utils/business/CenterUdpBusiness.ets index 9d5ab17..c192260 100644 --- a/entry/src/main/ets/utils/business/CenterUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/CenterUdpBusiness.ets @@ -38,12 +38,10 @@ class CenterUDPBusiness { } startHeartBeat() { + console.log("心跳") // 组装消息,一秒发送一次 this.timer = setInterval(() => { SetSerialNumber() - // const signNum = AppStorage.get('signNum') - // const statue = AppStorage.get('statue') - // const lsh = AppStorage.get('lsh') const arr = [this.signNum || 0, this.statue || 1] let tmpList: number[] = []; tmpList.push(NumberToByteArray(Number(arr[0]), 1 * 8)[0]) @@ -52,7 +50,6 @@ class CenterUDPBusiness { for (let i = 0; i < str.length; i++) { tmpList.push(NumberToByteArray(str.charCodeAt(i), 1 * 8)[0]) } - // const carInfo = AppStorage.get('carInfo')! const data: UDPParamType = { id: 31, list: tmpList, @@ -60,6 +57,7 @@ class CenterUDPBusiness { placeId: this.carInfo.examinationRoomId! } const param = this.setWholeMsg(data) + console.log("查看") this.udp.sendMsg(param); }, 1000); } diff --git a/entry/src/main/ets/workers/DifferentialCorrection.ets b/entry/src/main/ets/workers/DifferentialCorrection.ets index 210dcdf..5316e13 100644 --- a/entry/src/main/ets/workers/DifferentialCorrection.ets +++ b/entry/src/main/ets/workers/DifferentialCorrection.ets @@ -1,7 +1,7 @@ // 处理worker线程的消息tcp拿差分改正数,udp给后置机 import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker'; import { WorkerTag } from '../config'; -import { CenterCallBackMsgType, WorkerBackMessage, WorkerMessage } from '../model'; +import { WorkerMessage } from '../model'; import { CenterUDPBusinessInstance } from '../utils/business/CenterUdpBusiness'; import { DifferentialSignal } from '../utils/business/DifferentialSignal'; import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness'; @@ -21,15 +21,15 @@ workerPort.onmessage = (e: MessageEvents) => { DifferentialSignal.init(result.config); DifferentialSignal.sendData() // 初始化后置机UDP - ObtainUdpBusinessInstance.init(result.config) + // ObtainUdpBusinessInstance.init(result.config) // 初始化中心UDP CenterUDPBusinessInstance.init(result.config, result.carInfo, result.otherMessage) // 中心心跳 CenterUDPBusinessInstance.startHeartBeat() // 如果外部有这个消息进来就开始给中心发送 - if (result.udpParam) { - CenterUDPBusinessInstance.sendData(result.udpParam); - } + // if (result.udpParam) { + // CenterUDPBusinessInstance.sendData(result.udpParam); + // } // 获取TCP差分改正数信号 DifferentialSignal.getData((data: ArrayBuffer) => { console.log(WorkerTag, "Received differential signal data:", data.byteLength, "bytes") @@ -37,29 +37,29 @@ workerPort.onmessage = (e: MessageEvents) => { ObtainUdpBusinessInstance.sendData(data) }) // 监听PLC和GPS信号 - ObtainUdpBusinessInstance.onMsg((data: string) => { - // TODO - // 需要观察 - console.log(WorkerTag, "后置机消息", data) - // 收到后置机消息传出去提供给业务,data应该是个string - workerPort.postMessage( - JSON.stringify({ - type: 'obtainUdpData', - data: data - } as WorkerBackMessage)) - }) - CenterUDPBusinessInstance.onMsg((data: CenterCallBackMsgType) => { - // TODO - // 需要观察 - console.log(WorkerTag, "中心消息", data) - // 收到中心指令发送出去 - workerPort.postMessage( - JSON.stringify({ - type: 'centerUdpData', - data: data - } as WorkerBackMessage) - ) - }) + // ObtainUdpBusinessInstance.onMsg((data: string) => { + // // TODO + // // 需要观察 + // console.log(WorkerTag, "后置机消息", data) + // // 收到后置机消息传出去提供给业务,data应该是个string + // workerPort.postMessage( + // JSON.stringify({ + // type: 'obtainUdpData', + // data: data + // } as WorkerBackMessage)) + // }) + // CenterUDPBusinessInstance.onMsg((data: CenterCallBackMsgType) => { + // // TODO + // // 需要观察 + // console.log(WorkerTag, "中心消息", data) + // // 收到中心指令发送出去 + // workerPort.postMessage( + // JSON.stringify({ + // type: 'centerUdpData', + // data: data + // } as WorkerBackMessage) + // ) + // }) } /**