fix: 完善worker关闭性能优化,完善读取串口代码

This commit is contained in:
wangzhongjie 2025-06-30 09:55:39 +08:00
parent fd2f75d1fe
commit 078ec1afd4
6 changed files with 59 additions and 1 deletions

View File

@ -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() {

View File

@ -6,6 +6,8 @@ export interface WorkerMessage {
centerUdpParam?: UDPParamType;
otherMessage: OtherMessageType;
singlePlay?: boolean
// 关闭
close?: boolean;
}
export interface OtherMessageType {

View File

@ -50,6 +50,18 @@ class differentialAndSignal {
this.events = this.events.filter((cb) => cb !== callback);
}
// 关闭
close() {
const config: EnvironmentConfigurationType = AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") || {
}
this.workerInstance.postMessage(JSON.stringify({
config: config,
close: true
}));
// 关闭线程
this.workerInstance.terminate()
}
// 获取Worker消息
getMessage() {
this.workerInstance.onmessage = (e: MessageEvents): void => {

View File

@ -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() {
}

View File

@ -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);
});

View File

@ -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}`);