diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 7da2fc8..bd593fb 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -12,6 +12,7 @@ import FileUtils from '../utils/FileUtils'; import { EntryTag } from '../config'; import { dConsole } from '../utils/LogWorker'; import { UseAuth } from '../utils/Common'; +import { DifferentialAndSignal } from '../utils/business/DifferentialAndSignalWorker'; export default class EntryAbility extends UIAbility { async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { @@ -92,6 +93,8 @@ export default class EntryAbility extends UIAbility { onWindowStageDestroy() { // Main window is destroyed, release UI related resources console.log(EntryTag, "onWindowStageDestroy", "窗口销毁完成") + DifferentialAndSignal.close() + } onForeground() { diff --git a/entry/src/main/ets/model/Worker.ets b/entry/src/main/ets/model/Worker.ets index 9d89618..c1944b7 100644 --- a/entry/src/main/ets/model/Worker.ets +++ b/entry/src/main/ets/model/Worker.ets @@ -6,6 +6,8 @@ export interface WorkerMessage { centerUdpParam?: UDPParamType; otherMessage: OtherMessageType; singlePlay?: boolean + // 关闭 + close?: boolean; } export interface OtherMessageType { diff --git a/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets b/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets index b4f9ffd..8f3d6bb 100644 --- a/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets +++ b/entry/src/main/ets/utils/business/DifferentialAndSignalWorker.ets @@ -50,6 +50,18 @@ class differentialAndSignal { this.events = this.events.filter((cb) => cb !== callback); } + // 关闭 + close() { + const config: EnvironmentConfigurationType = AppStorage.get("EnvironmentConfiguration") || { + } + this.workerInstance.postMessage(JSON.stringify({ + config: config, + close: true + })); + // 关闭线程 + this.workerInstance.terminate() + } + // 获取Worker消息 getMessage() { this.workerInstance.onmessage = (e: MessageEvents): void => { diff --git a/entry/src/main/ets/utils/business/ObtainUdpBusiness.ets b/entry/src/main/ets/utils/business/ObtainUdpBusiness.ets index 59d511f..e929d8b 100644 --- a/entry/src/main/ets/utils/business/ObtainUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/ObtainUdpBusiness.ets @@ -88,6 +88,23 @@ class ObtainUdpBusiness { } } + // 关闭 + close() { + switch (this.modelNo) { + case "0": + break + case "1": + break + case "2": + break + case "3": + this.thirdGenerationMachineUdp!.close() + break + default: + break + } + } + private initSecondaryBoard() { } diff --git a/entry/src/main/ets/utils/business/SerialPortService.ets b/entry/src/main/ets/utils/business/SerialPortService.ets index ef34d1b..7e1b86f 100644 --- a/entry/src/main/ets/utils/business/SerialPortService.ets +++ b/entry/src/main/ets/utils/business/SerialPortService.ets @@ -45,6 +45,7 @@ class serialPortService { if (this.fd !== -1) { await InitSerialPortData(this.fd, Number(this.baudRate)) ReceiveSerialPortDataBySelf(this.fd, (res1: number, res2: number, res3: number[]) => { + console.log(SerialPortTag, "串口接收数据", res1, res2, res3) this.events.forEach((callback) => { callback(res3); }); diff --git a/entry/src/main/ets/workers/DifferentialCorrection.ets b/entry/src/main/ets/workers/DifferentialCorrection.ets index 452625e..5b833bb 100644 --- a/entry/src/main/ets/workers/DifferentialCorrection.ets +++ b/entry/src/main/ets/workers/DifferentialCorrection.ets @@ -1,10 +1,11 @@ // 处理worker线程的消息tcp拿差分改正数,udp给后置机 import worker, { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope } from '@ohos.worker'; -import { WorkerTag } from '../config'; +import { SerialPortTag, WorkerTag } from '../config'; import { CenterCallBackMsgType, WorkerBackMessage, WorkerBackMessageType, WorkerMessage } from '../model'; import { CenterUDPBusinessInstance } from '../utils/business/CenterUdpBusiness'; import { DifferentialSignal } from '../utils/business/DifferentialSignal'; import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness'; +import { SerialPortService } from '../utils/business/SerialPortService'; const workerPort: ThreadWorkerGlobalScope = worker.workerPort; @@ -23,6 +24,10 @@ workerPort.onmessage = (e: MessageEvents) => { // CenterUDPBusinessInstance.sendData(result.centerUdpParam); // } getDataFn() + // 必传环境配置 + if (result.close) { + closedFn(result) + } } // 初始化函数 @@ -39,6 +44,10 @@ function initFn(result: WorkerMessage) { CenterUDPBusinessInstance.startHeartBeat() // 初始化考试过程UDP // JudgeUdpBusinessInstance.init(result.config, result.carInfo, result?.singlePlay || false, result.otherMessage.lsh) + // 初始化档位信号串口 + if (result.config.carType !== "2") { + SerialPortService.init() + } } function getDataFn() { @@ -53,7 +62,11 @@ function getDataFn() { // TODO // 需要观察 console.log(WorkerTag, "后置机消息", data) + // 这里面还需要处理一下档位信号 // 收到后置机消息传出去提供给业务,data应该是个string + SerialPortService.onMsg((data: number[]) => { + console.log(SerialPortTag, "需要处理档位信号:", data); + }) workerPort.postMessage( JSON.stringify({ type: WorkerBackMessageType.ObtainUdpData, @@ -75,6 +88,16 @@ function getDataFn() { }) } +// 关闭函数 +function closedFn(result: WorkerMessage) { + ObtainUdpBusinessInstance.close() + CenterUDPBusinessInstance.close() + // 关闭串口 + if (result.config.carType !== "2") { + SerialPortService.closed() + } +} + workerPort.onmessageerror = (e: MessageEvents) => { console.log(WorkerTag, `Worker received message error: ${e.data}`);