From 970221509c9a903e050b214304104e7f8757490b Mon Sep 17 00:00:00 2001 From: lixiao <932184220@qq.com> Date: Wed, 15 Oct 2025 17:52:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=8F=E7=BA=A2=E7=90=83udp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/utils/business/JudgeUdpBusiness.ets | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets index 510828d..b601406 100644 --- a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets @@ -30,7 +30,7 @@ class JudgeUdpBusiness { private carInfo: CarInfoType = {} private singlePlay: boolean = false private lsh: string = "" - private serialIndex: number = 0 + private serialIndex: number = 1 constructor() { if (!JudgeUdpBusiness.instance) { @@ -39,7 +39,7 @@ class JudgeUdpBusiness { return JudgeUdpBusiness.instance } - async getMessageHeartbeat(msg: string): Promise { + async getMessageHeartbeat(msg: string, isEnd: boolean = false): Promise { let gpsDigit = JudgeConfig.fourInOneScreen.gpsDigit const asclshArr = StringToASCII(FillZero( this.singlePlay @@ -77,7 +77,7 @@ class JudgeUdpBusiness { //科目类型(0:未考试 1:科目二 2:科目三) + 考试开始时间 NumberToByteArray(`${0}${'00:00:000'}`, 4 * 8), // 消息序号 - NumberToByteArray(this.serialIndex, 2 * 8), + NumberToByteArray(isEnd ? 0 : this.serialIndex, 2 * 8), translateSignals, NumberToByteArray(tempSd * 100, 2 * 8), NumberToByteArray(plcData.sensor.fdjzs / 100, 8), @@ -128,7 +128,11 @@ class JudgeUdpBusiness { result = result.concat(itemArr) }) // AppStorage.setOrCreate("serialIndex", 0) - this.serialIndex = 0 + if (isEnd) { + this.serialIndex = 1 + } else { + this.serialIndex += 1 + } return [...new Uint8Array(result)] } From b6c153443a6e32bdfd84b399129762d055ecc3aa Mon Sep 17 00:00:00 2001 From: lixiao <932184220@qq.com> Date: Thu, 16 Oct 2025 17:47:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=B0=8F=E7=BA=A2=E7=90=83udp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/oh-package-lock.json5 | 2 +- entry/src/main/ets/model/Worker.ets | 18 ++-- .../ets/pages/Judge/BaseJudgeBussines.ets | 8 +- .../main/ets/pages/Judge/JudgeBusiness.ets | 9 +- entry/src/main/ets/utils/UdpUtils.ets | 2 + .../business/DifferentialAndSignalWorker.ets | 27 +++--- .../ets/utils/business/JudgeUdpBusiness.ets | 97 ++++++++++--------- .../src/main/ets/utils/business/UdpEvent.ets | 5 +- .../ets/workers/DifferentialCorrection.ets | 51 ++++++---- oh-package-lock.json5 | 18 ++-- 10 files changed, 133 insertions(+), 104 deletions(-) diff --git a/entry/oh-package-lock.json5 b/entry/oh-package-lock.json5 index 3f4c904..d5892e3 100644 --- a/entry/oh-package-lock.json5 +++ b/entry/oh-package-lock.json5 @@ -1,5 +1,5 @@ { - "lockfileVersion": 2, + "lockfileVersion": 1, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "specifiers": {}, "packages": {} diff --git a/entry/src/main/ets/model/Worker.ets b/entry/src/main/ets/model/Worker.ets index 6f39e65..5f75662 100644 --- a/entry/src/main/ets/model/Worker.ets +++ b/entry/src/main/ets/model/Worker.ets @@ -1,15 +1,21 @@ import { CarInfoType, CenterCallBackMsgType, EnvironmentConfigurationType, UDPParamType } from '.'; -export interface WorkerMessage { - config: EnvironmentConfigurationType; - carInfo: CarInfoType; +interface WorkerMessageData { + config?: EnvironmentConfigurationType; + carInfo?: CarInfoType; centerUdpParam?: UDPParamType; - otherMessage: OtherMessageType; + otherMessage?: OtherMessageType; singlePlay?: boolean - // 关闭 - close?: boolean; + judgeUdp?: string + judgeUdpEnd?: string } +export interface WorkerMessage { + type: "init" | "data" | "close" + data: WorkerMessageData +} + + export interface OtherMessageType { signNum: number; statue: string; diff --git a/entry/src/main/ets/pages/Judge/BaseJudgeBussines.ets b/entry/src/main/ets/pages/Judge/BaseJudgeBussines.ets index a5de163..8a2cd5b 100644 --- a/entry/src/main/ets/pages/Judge/BaseJudgeBussines.ets +++ b/entry/src/main/ets/pages/Judge/BaseJudgeBussines.ets @@ -34,6 +34,8 @@ import { endRecordVideo, saveStartRecordVideo } from '../../utils/Video' import router from '@ohos.router' import { GetSyncData, UpdateTableByArray } from '../../utils/table/Operation' import { CurrentProjectConversion, DeductionProjectConversion, DetectingDifferences, GetCarStatus } from './utils' +import { DifferentialSignal } from '../../utils/business/DifferentialSignal' +import { DifferentialAndSignal } from '../../utils/business/DifferentialAndSignalWorker' export interface GetSysSetResult { @@ -69,6 +71,8 @@ export const SetJudgeItem = async (itemno: string, type: 1 | 2) => { } export class BaseJudge { + private gpsAndPlcData: string = "" + async goJudgeVoice(sound: JudgeSound, avPlayer: VoiceAnnounce) { dConsole.info(JudgeTag, 'surenjun code=>', JSON.stringify(sound.code)) //判断是不是模拟灯光语音 @@ -375,8 +379,7 @@ export class BaseJudge { that.avPlayer?.playAudio(['voice/exam_waiting.mp3'], judgeUI.singlePlay, async () => { try { if (!judgeUI.singlePlay) { - // const bytes = await this.getMessageHeartbeat(true); - // globalThis.judgeUdp.send(bytes) + DifferentialAndSignal.sendMsg({ type: "data", data: { judgeUdpEnd: this.gpsAndPlcData } }) } } catch (e) { console.info(JudgeTag, JSON.stringify(e)) @@ -631,6 +634,7 @@ export class BaseJudge { }; async judging(strData: string, callBack: Function, judgeUI: JudgePage, that: JudgeBusiness) { + this.gpsAndPlcData = strData let examData: JudgeCallBackData = JSON.parse(strData); const carzt = examData.carzt const xmks = examData.xmks diff --git a/entry/src/main/ets/pages/Judge/JudgeBusiness.ets b/entry/src/main/ets/pages/Judge/JudgeBusiness.ets index eaa4bf3..f979865 100644 --- a/entry/src/main/ets/pages/Judge/JudgeBusiness.ets +++ b/entry/src/main/ets/pages/Judge/JudgeBusiness.ets @@ -22,7 +22,6 @@ import { DifferentialAndSignal } from '../../utils/business/DifferentialAndSigna import { PlcStrToJson, PlcStrToWXJson } from './utils' import { BaseJudgeImpl, SetJudgeItem } from './BaseJudgeBussines' import { JudgePage } from '../Judge'; -import { JudgeUdpBusinessInstance } from '../../utils/business/JudgeUdpBusiness' export default class JudgeBusiness { public mndgStr: string | undefined @@ -146,7 +145,6 @@ export default class JudgeBusiness { const gpsPart = msg.split("#END$GPS,")[1]; const gpsStatus = gpsPart.split(",")[0]; if (gpsStatus === "4") { - // dConsole.log(JudgeTag, "差分状态正常", gpsStatus) this.judgeUI.isDwztRight = true } else { dConsole.log(JudgeTag, "差分状态异常", gpsStatus) @@ -173,8 +171,11 @@ export default class JudgeBusiness { this.judgeUI.dw = (Math.floor(this.plcData.sensor.dw as number) || 0) + '' if (this.udpCount % 5 === 0 && !this.isExamEnd) { - const bytes = await JudgeUdpBusinessInstance.getMessageHeartbeat(msg); - JudgeUdpBusinessInstance.sendData(bytes) + DifferentialAndSignal.sendMsg({ + type: "data", data: { + judgeUdp: msg + } + }) } if (!this.isExamEnd) { diff --git a/entry/src/main/ets/utils/UdpUtils.ets b/entry/src/main/ets/utils/UdpUtils.ets index 8b1a70d..0269cd2 100644 --- a/entry/src/main/ets/utils/UdpUtils.ets +++ b/entry/src/main/ets/utils/UdpUtils.ets @@ -2,6 +2,7 @@ import socket from '@ohos.net.socket' import { UDPTag } from '../config' import { FillZero, StringToASCII } from './Common' import { BusinessError } from '@ohos.base' +import { dConsole } from './LogWorker' interface MsgExt { id: number, @@ -101,6 +102,7 @@ export default class UdpClient { // 发送消息 sendMsgExt(param: MsgExt) { + dConsole.info(UDPTag, "小红球数据", param) const msgData = this.setWholeMsg(param) this.sendMsg(msgData) } diff --git a/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets b/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets index d60780b..86f6502 100644 --- a/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets +++ b/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets @@ -7,12 +7,8 @@ class differentialAndSignal { private workerInstance = new worker.ThreadWorker("entry/ets/workers/DifferentialCorrection.ets") private events: Array = [] - constructor() { - } - // 初始化Worker init() { - this.workerInstance = new worker.ThreadWorker("entry/ets/workers/DifferentialCorrection.ets"); let config: EnvironmentConfigurationType = AppStorage.get("EnvironmentConfiguration") || { tcplocalIp: "", @@ -26,19 +22,26 @@ class differentialAndSignal { const lsh = AppStorage.get('lsh') const singlePlay = AppStorage.get("singlePlay") const data: WorkerMessage = { - config: config, - carInfo: carInfo, - otherMessage: { - signNum: signNum || 0, - statue: statue || "", - lsh: lsh || "" - }, - singlePlay: singlePlay || false + type: "init", + data: { + config: config, + carInfo: carInfo, + otherMessage: { + signNum: signNum || 0, + statue: statue || "", + lsh: lsh || "" + }, + singlePlay: singlePlay || false + } } this.workerInstance.postMessage(JSON.stringify(data)) this.getMessage() } + sendMsg(msg: WorkerMessage) { + this.workerInstance.postMessage(JSON.stringify(msg)) + } + // 添加监听 onMsg(callback: Function) { if (this.events.findIndex(cb => cb === callback) !== -1) return; // 防止重复添加监听器 diff --git a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets index b601406..65da01f 100644 --- a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets @@ -18,20 +18,24 @@ import systemTime from '@ohos.systemDateTime'; import { SerialNumberInstance } from '../SerialNumber'; import { GetDwStatusType, GetTranslateSignals } from '../../pages/Judge/utils'; import { testKm2Items, testKm3Items } from '../../mock'; +import { dConsole } from '../LogWorker'; // import { examCalcGpsDistance } from '../../pages/Judge/JudgeUtils'; class JudgeUdpBusiness { private static instance: JudgeUdpBusiness - public udpIndex = 0; + // public udpIndex = 1; private udp: UdpClient = new UdpClient() - private currentUdpIndex = 0; + // private currentUdpIndex = 0; private carInfo: CarInfoType = {} private singlePlay: boolean = false private lsh: string = "" private serialIndex: number = 1 + // private carNo: string = "" + // private examinationRoomId: string = "" + constructor() { if (!JudgeUdpBusiness.instance) { JudgeUdpBusiness.instance = this @@ -48,7 +52,6 @@ class JudgeUdpBusiness { 13)); const ascksyhArr = StringToASCII(this.carInfo.examSubject === '2' ? '0000000000000' : '1111111111111') const ascsbxhArr = StringToASCII('00000000') - // const serialIndex = AppStorage.get("serialIndex")! const plcData = await this.getPlcData(msg); let param: number[] = Object.entries(plcData.sensor) @@ -142,15 +145,13 @@ class JudgeUdpBusiness { } sendData(bytes: number[]) { - if (this.udpIndex > this.currentUdpIndex) { - this.udp.sendMsgExt({ - id: 45, - list: bytes, - carNo: this.carInfo.carNo!, - placeId: this.carInfo.examinationRoomId! - }) - this.currentUdpIndex = this.udpIndex - } + dConsole.info("lixiao", this.carInfo.carNo, this.carInfo.examinationRoomId) + this.udp.sendMsgExt({ + id: 45, + list: bytes, + carNo: this.carInfo.carNo!, + placeId: this.carInfo!.examinationRoomId! + }) } //申请远程扣分查询 @@ -178,45 +179,47 @@ class JudgeUdpBusiness { // 初始化 async init(config: EnvironmentConfigurationType, carInfo: CarInfoType, singlePlay: boolean, lsh: string): Promise { this.lsh = lsh - this.carInfo = carInfo + // this.carNo = carInfo.carNo! + // this.examinationRoomId = carInfo.examinationRoomId! this.singlePlay = singlePlay + this.carInfo = carInfo return new Promise((resolve, reject) => { try { this.udp.create(config.udplocalIp!, '8080', carInfo.udpAddress!, carInfo.hintPort!).then(resolve).catch(reject) - ObtainUdpBusinessInstance.onMsg(async (msg: string) => { - let prevJd = 0, preWd = 0 - - const stashArr = msg.split(',') - if (stashArr[0] != '#DN_GD') { - return - } - const udpIndex = AppStorage.get("udpIndex")!; - const isJudge = AppStorage.get("isJudge")!; - if (udpIndex % 5 === 0 && !isJudge) { - const bytes = await this.getMessageHeartbeat(msg); - const msgArr: string[] = msg.split(','); - const jd = this.convertGpsCoord2(Number(msgArr[96])); - const wd = this.convertGpsCoord2(Number(msgArr[95]) || 0); - this.sendData(bytes) - if (prevJd && Number(msgArr[83]) === 4) { - //TODO 解决轨迹黑屏 - // await examCalcGpsDistance({ - // jd1: prevJd, - // wd1: preWd, - // jd2: jd, - // wd2: wd, - // h: Number(msgArr[90]) || 1, - // }) - - } - prevJd = jd; - preWd = wd; - } - // AppStorage.setOrCreate("udpIndex", udpIndex + 1) - }) - setInterval(() => { - this.udpIndex += 1 - }, 1000) + // ObtainUdpBusinessInstance.onMsg(async (msg: string) => { + // let prevJd = 0, preWd = 0 + // + // const stashArr = msg.split(',') + // if (stashArr[0] != '#DN_GD') { + // return + // } + // const udpIndex = AppStorage.get("udpIndex")!; + // const isJudge = AppStorage.get("isJudge")!; + // if (udpIndex % 5 === 0 && !isJudge) { + // const bytes = await this.getMessageHeartbeat(msg); + // const msgArr: string[] = msg.split(','); + // const jd = this.convertGpsCoord2(Number(msgArr[96])); + // const wd = this.convertGpsCoord2(Number(msgArr[95]) || 0); + // this.sendData(bytes) + // if (prevJd && Number(msgArr[83]) === 4) { + // //TODO 解决轨迹黑屏 + // // await examCalcGpsDistance({ + // // jd1: prevJd, + // // wd1: preWd, + // // jd2: jd, + // // wd2: wd, + // // h: Number(msgArr[90]) || 1, + // // }) + // + // } + // prevJd = jd; + // preWd = wd; + // } + // // AppStorage.setOrCreate("udpIndex", udpIndex + 1) + // }) + // setInterval(() => { + // this.udpIndex += 1 + // }, 1000) } catch (e) { reject(e) } diff --git a/entry/src/main/ets/utils/business/UdpEvent.ets b/entry/src/main/ets/utils/business/UdpEvent.ets index fc9cd6d..78a55ae 100644 --- a/entry/src/main/ets/utils/business/UdpEvent.ets +++ b/entry/src/main/ets/utils/business/UdpEvent.ets @@ -1,5 +1,5 @@ import emitter from '@ohos.events.emitter'; -import { JudgeUdpBusinessInstance } from './JudgeUdpBusiness'; +// import { JudgeUdpBusinessInstance } from './JudgeUdpBusiness'; enum EventId { //远程扣分处理 @@ -80,7 +80,8 @@ class JudgeEmitter { //监听扣分指令 public sendOnKf(directives: string) { this.directives = directives - JudgeUdpBusinessInstance.askKf(Number(directives)) + // TODO 远程扣分 + // JudgeUdpBusinessInstance.askKf(Number(directives)) } // 获取扣分项目编号 diff --git a/entry/src/main/ets/workers/DifferentialCorrection.ets b/entry/src/main/ets/workers/DifferentialCorrection.ets index 032025f..f9a772b 100644 --- a/entry/src/main/ets/workers/DifferentialCorrection.ets +++ b/entry/src/main/ets/workers/DifferentialCorrection.ets @@ -13,46 +13,57 @@ import { DifferentialSignal } from '../utils/business/DifferentialSignal'; import { JudgeUdpBusinessInstance } from '../utils/business/JudgeUdpBusiness'; import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness'; import { SerialPortService } from '../utils/business/SerialPortService'; +import { dConsole } from '../utils/LogWorker'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; -workerPort.onmessage = (e: MessageEvents) => { +workerPort.onmessage = async (e: MessageEvents) => { console.log(WorkerTag, `Worker received message: ${e.data}`); const result: WorkerMessage = JSON.parse(e.data); - initFn(result) - // TODO 内部已经实现,外部未实现 - // 如果外部有这个值就去更新中心这个值 - if (result.otherMessage) { - CenterUDPBusinessInstance.changeKeyValue(result.otherMessage) - } - // 如果外部有这个消息进来就开始给中心发送,暂时看不需要 - // if (result.centerUdpParam) { - // CenterUDPBusinessInstance.sendData(result.centerUdpParam); - // } - getDataFn(result.config) - // 必传环境配置 - if (result.close) { + if (result.type === "init") { + initFn(result) + getDataFn(result.data.config!) + } else if (result.type === "data") { + if (result.data.otherMessage) { + CenterUDPBusinessInstance.changeKeyValue(result.data.otherMessage!) + } + if (result.data.judgeUdp) { + let data = await JudgeUdpBusinessInstance.getMessageHeartbeat(result.data.judgeUdp!) + JudgeUdpBusinessInstance.sendData(data) + } + if (result.data.judgeUdpEnd) { + let data = await JudgeUdpBusinessInstance.getMessageHeartbeat(result.data.judgeUdp!, true) + JudgeUdpBusinessInstance.sendData(data) + } + // 如果外部有这个消息进来就开始给中心发送,暂时看不需要 + // if (result.centerUdpParam) { + // CenterUDPBusinessInstance.sendData(result.centerUdpParam); + // } + } else if (result.type === "close") { closedFn(result) + } + // TODO 内部已经实现,外部未实现 } // 初始化函数 function initFn(result: WorkerMessage) { // 初始化差分校正TCP - DifferentialSignal.init(result.config); + DifferentialSignal.init(result.data.config!); // 定时发送TCP消息 DifferentialSignal.sendData() // 初始化后置机UDP - ObtainUdpBusinessInstance.init(result.config) + ObtainUdpBusinessInstance.init(result.data.config!) // 初始化中心UDP - CenterUDPBusinessInstance.init(result.config, result.carInfo, result.otherMessage) + CenterUDPBusinessInstance.init(result.data.config!, result.data.carInfo!, result.data.otherMessage!) // 中心心跳 CenterUDPBusinessInstance.startHeartBeat() // 初始化考试过程UDP - JudgeUdpBusinessInstance.init(result.config, result.carInfo, result?.singlePlay || false, result.otherMessage.lsh) + dConsole.info("lixiao 初始化", result.data.carInfo) + JudgeUdpBusinessInstance.init(result.data.config!, result.data.carInfo!, result?.data.singlePlay || false, result.data.otherMessage!.lsh) // 初始化档位信号串口 - if (result.config.carType !== "2") { + if (result.data.config?.carType !== "2") { SerialPortService.init() } } @@ -107,7 +118,7 @@ function closedFn(result: WorkerMessage) { ObtainUdpBusinessInstance.close() CenterUDPBusinessInstance.close() // 关闭串口 - if (result.config.carType !== "2") { + if (result.data.config?.carType !== "2") { SerialPortService.closed() } } diff --git a/oh-package-lock.json5 b/oh-package-lock.json5 index b959024..f76874c 100644 --- a/oh-package-lock.json5 +++ b/oh-package-lock.json5 @@ -1,20 +1,18 @@ { - "lockfileVersion": 2, + "lockfileVersion": 1, "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "specifiers": { - "@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19", - "@ohos/crypto-js@2.0.3": "@ohos/crypto-js@2.0.3" + "@ohos/crypto-js@2.0.3": "@ohos/crypto-js@2.0.3", + "@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19" }, "packages": { - "@ohos/hypium@1.0.19": { - "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har", - "integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==", - "registryType": "ohpm" - }, "@ohos/crypto-js@2.0.3": { "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/crypto-js/-/crypto-js-2.0.3.har", - "integrity": "sha512-LuHaR1kD5PxnOXnuR1fWvPwGtbed9Q/QGzk6JOh8y5Wdzvi8brPesODZiaWf9scOVRHsbTPOtZw91vWB35p1vQ==", - "registryType": "ohpm" + "integrity": "sha512-LuHaR1kD5PxnOXnuR1fWvPwGtbed9Q/QGzk6JOh8y5Wdzvi8brPesODZiaWf9scOVRHsbTPOtZw91vWB35p1vQ==" + }, + "@ohos/hypium@1.0.19": { + "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har", + "integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==" } } } \ No newline at end of file