fix: 优化评判UDP

This commit is contained in:
wangzhongjie 2025-06-17 09:06:18 +08:00
parent 46244ac980
commit 339dc43493
6 changed files with 69 additions and 81 deletions

View File

@ -5,6 +5,7 @@ export interface WorkerMessage {
carInfo: CarInfoType; carInfo: CarInfoType;
centerUdpParam?: UDPParamType; centerUdpParam?: UDPParamType;
otherMessage: OtherMessageType; otherMessage: OtherMessageType;
singlePlay?: boolean
} }
export interface OtherMessageType { export interface OtherMessageType {

View File

@ -11,7 +11,6 @@ import { BusinessError } from '@ohos.base';
import { delPic } from '../utils/Video'; import { delPic } from '../utils/Video';
import { FileHelper } from '../utils/FileHelp'; import { FileHelper } from '../utils/FileHelp';
import { DrivingDataStorage } from '../utils/business/DrivingDataStorage'; import { DrivingDataStorage } from '../utils/business/DrivingDataStorage';
import { JudgeUdpBusinessInstance } from '../utils/business/JudgeUdpBusiness';
import { JudgeEmitterInstance } from '../utils/business/UdpEvent'; import { JudgeEmitterInstance } from '../utils/business/UdpEvent';
import HeaderComponent from './compontents/Header'; import HeaderComponent from './compontents/Header';
import CardComponent from './Index/Card'; import CardComponent from './Index/Card';
@ -184,7 +183,7 @@ struct Index {
this.timeInfo = AppStorage.get<TimeSynchronizationRspBody>('timeInfo')! this.timeInfo = AppStorage.get<TimeSynchronizationRspBody>('timeInfo')!
DrivingDataStorage.init(this.context) DrivingDataStorage.init(this.context)
DrivingDataStorage.initializeTheDrivingDataFolder() DrivingDataStorage.initializeTheDrivingDataFolder()
JudgeUdpBusinessInstance.init() // JudgeUdpBusinessInstance.init()
// TODO 摄像头遮挡 // TODO 摄像头遮挡
// takePhotoFn(this.context) // takePhotoFn(this.context)
// TODO // TODO

View File

@ -74,8 +74,8 @@ export function ArrayToByteArray(array: number[]): Uint8Array {
* @param len 字节数 * @param len 字节数
* @returns 返回字节数组 * @returns 返回字节数组
*/ */
export function NumberToByteArray(number: number, len: number): number[] { export function NumberToByteArray(number: number | string, len: number): number[] {
let str = Math.floor(number).toString(2); let str = Math.floor(Number(number)).toString(2);
if (str.length > len) { if (str.length > len) {
console.log('数据长度不对~~'); console.log('数据长度不对~~');
return []; return [];

View File

@ -24,6 +24,7 @@ class differentialAndSignal {
const signNum = AppStorage.get<number>('signNum') const signNum = AppStorage.get<number>('signNum')
const statue = AppStorage.get<string>('statue') const statue = AppStorage.get<string>('statue')
const lsh = AppStorage.get<string>('lsh') const lsh = AppStorage.get<string>('lsh')
const singlePlay = AppStorage.get<boolean>("singlePlay")
const data: WorkerMessage = { const data: WorkerMessage = {
config: config, config: config,
carInfo: carInfo, carInfo: carInfo,
@ -31,7 +32,8 @@ class differentialAndSignal {
signNum: signNum || 0, signNum: signNum || 0,
statue: statue || "", statue: statue || "",
lsh: lsh || "" lsh: lsh || ""
} },
singlePlay: singlePlay || false
} }
this.workerInstance.postMessage(JSON.stringify(data)) this.workerInstance.postMessage(JSON.stringify(data))
this.getMessage() this.getMessage()

View File

@ -2,7 +2,7 @@ import { CarInfoType, EnvironmentConfigurationType, Gps, Sensor, UDPParamType }
import { testKm2Items, testKm3Items } from '../../pages/judgeSDK/dataTest'; import { testKm2Items, testKm3Items } from '../../pages/judgeSDK/dataTest';
import { fillZero, } from '../../pages/judgeSDK/utils/Common'; import { fillZero, } from '../../pages/judgeSDK/utils/Common';
import { JudgeConfig } from '../../config'; import { JudgeConfig } from '../../config';
import { FillZero, StringToASCII } from '../Common'; import { FillZero, NumberToByteArray, StringToASCII } from '../Common';
import UdpClient from '../UdpUtils'; import UdpClient from '../UdpUtils';
import { CenterUDPBusinessInstance } from './CenterUdpBusiness'; import { CenterUDPBusinessInstance } from './CenterUdpBusiness';
import { ObtainUdpBusinessInstance } from './ObtainUdpBusiness'; import { ObtainUdpBusinessInstance } from './ObtainUdpBusiness';
@ -25,33 +25,16 @@ interface ProjectItemType {
status: string status: string
} }
function string2Bytes(num: number | string, len: number) {
let str = (Math.floor(Number(num))).toString(2);
if (str.length > len) {
console.log('数据长度不对~~');
return []
}
let byteString = FillZero(str, len);
let arrBytes: number[] = [];
for (let i = byteString.length; i > 0; ) {
let j = i - 8;
if (j < 0) {
j = 0
}
let s = byteString.slice(j, i);
let v = parseInt(s, 2);
arrBytes.push(v);
i = i - 8
}
return arrBytes;
}
class JudgeUdpBusiness { class JudgeUdpBusiness {
private static instance: JudgeUdpBusiness private static instance: JudgeUdpBusiness
public udpIndex = 0; public udpIndex = 0;
private udp: UdpClient = new UdpClient() private udp: UdpClient = new UdpClient()
private currentUdpIndex = 0; private currentUdpIndex = 0;
private carInfo: CarInfoType = {}
private singlePlay: boolean = false
private lsh: string = ""
private serialIndex: number = 0
constructor() { constructor() {
if (!JudgeUdpBusiness.instance) { if (!JudgeUdpBusiness.instance) {
@ -61,16 +44,16 @@ class JudgeUdpBusiness {
} }
async getMessageHeartbeat(msg: string): Promise<number[]> { async getMessageHeartbeat(msg: string): Promise<number[]> {
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
let gpsDigit = JudgeConfig.fourInOneScreen.gpsDigit let gpsDigit = JudgeConfig.fourInOneScreen.gpsDigit
const asclshArr = StringToASCII(FillZero( const asclshArr = StringToASCII(FillZero(
AppStorage.get<boolean>("singlePlay") this.singlePlay
? '1111111111111' ? '1111111111111'
: AppStorage.get<string>("lsh")!, : this.lsh,
13)); 13));
const ascksyhArr = StringToASCII(carInfo.examSubject === '2' ? '0000000000000' : '1111111111111') const ascksyhArr = StringToASCII(this.carInfo.examSubject === '2' ? '0000000000000' : '1111111111111')
const ascsbxhArr = StringToASCII('00000000') const ascsbxhArr = StringToASCII('00000000')
const serialIndex = AppStorage.get<number>("serialIndex")! // const serialIndex = AppStorage.get<number>("serialIndex")!
const plcData = await this.getPlcData(msg); const plcData = await this.getPlcData(msg);
let param: number[] = Object.entries(plcData.sensor) let param: number[] = Object.entries(plcData.sensor)
.filter((item: [string, number]) => { .filter((item: [string, number]) => {
@ -85,70 +68,71 @@ class JudgeUdpBusiness {
const translateProject = this.getTranslateProject(); const translateProject = this.getTranslateProject();
const translateJd = Number(this.convertGpsCoord2(plcData.gps.wd).toFixed(gpsDigit)) * Math.pow(10, gpsDigit); const translateJd = Number(this.convertGpsCoord2(plcData.gps.wd).toFixed(gpsDigit)) * Math.pow(10, gpsDigit);
const translateWd = Number(this.convertGpsCoord2(plcData.gps.jd).toFixed(gpsDigit)) * Math.pow(10, gpsDigit) const translateWd = Number(this.convertGpsCoord2(plcData.gps.jd).toFixed(gpsDigit)) * Math.pow(10, gpsDigit)
const translateProjects = translateProject.map(numStr => string2Bytes(parseInt(numStr, 2), 8)[0]) const translateProjects = translateProject.map(numStr => NumberToByteArray(parseInt(numStr, 2), 8)[0])
let tempSd = Number((plcData.gps.sd * 1.852).toFixed(0)) let tempSd = Number((plcData.gps.sd * 1.852).toFixed(0))
if (tempSd < 1) { if (tempSd < 1) {
tempSd = 0 tempSd = 0
} }
const arr: number[][] = [ const arr: number[][] = [
//考生号 //考生号
asclshArr.map(lsh => string2Bytes(lsh, 8)[0]), asclshArr.map(lsh => NumberToByteArray(lsh, 8)[0]),
//考试员号 //考试员号
ascksyhArr.map(ksyh => string2Bytes(ksyh, 8)[0]), ascksyhArr.map(ksyh => NumberToByteArray(ksyh, 8)[0]),
//科目类型(0:未考试 1:科目二 2:科目三) + 考试开始时间 //科目类型(0:未考试 1:科目二 2:科目三) + 考试开始时间
string2Bytes(`${0}${'00:00:000'}`, 4 * 8), NumberToByteArray(`${0}${'00:00:000'}`, 4 * 8),
// 消息序号 // 消息序号
string2Bytes(serialIndex, 2 * 8), NumberToByteArray(this.serialIndex, 2 * 8),
translateSignals, translateSignals,
string2Bytes(tempSd * 100, 2 * 8), NumberToByteArray(tempSd * 100, 2 * 8),
string2Bytes(plcData.sensor.fdjzs / 100, 8), NumberToByteArray(plcData.sensor.fdjzs / 100, 8),
string2Bytes(translateJd, 4 * 8), NumberToByteArray(translateJd, 4 * 8),
string2Bytes(translateWd, 4 * 8), string2Bytes(1, 8), NumberToByteArray(translateWd, 4 * 8), NumberToByteArray(1, 8),
//GPS东向距离 //GPS东向距离
string2Bytes(0, 4 * 8), NumberToByteArray(0, 4 * 8),
//GPS北向距离 //GPS北向距离
string2Bytes(0, 4 * 8), NumberToByteArray(0, 4 * 8),
//航向角 //航向角
string2Bytes(plcData.gps.hxj * 100, 2 * 8), NumberToByteArray(plcData.gps.hxj * 100, 2 * 8),
//俯仰角 //俯仰角
string2Bytes(plcData.gps.fyj * 100, 2 * 8), NumberToByteArray(plcData.gps.fyj * 100, 2 * 8),
// 高程(海拔) // 高程(海拔)
string2Bytes(plcData.gps.hbg * 100, 4 * 8), NumberToByteArray(plcData.gps.hbg * 100, 4 * 8),
//dddd //dddd
translateProjects, translateProjects,
//当前项目编号 //当前项目编号
string2Bytes(0, 8), NumberToByteArray(0, 8),
//场地设备编号 //场地设备编号
ascsbxhArr.map(sbxh => string2Bytes(sbxh, 8)[0]), ascsbxhArr.map(sbxh => NumberToByteArray(sbxh, 8)[0]),
//本次考试行驶距离 //本次考试行驶距离
string2Bytes(0, 2 * 8), NumberToByteArray(0, 2 * 8),
//扣分值 //扣分值
string2Bytes(0, 2 * 8), NumberToByteArray(0, 2 * 8),
//扣分数 //扣分数
string2Bytes(0, 2 * 8), NumberToByteArray(0, 2 * 8),
//扣分项数量 //扣分项数量
string2Bytes(0, 8), NumberToByteArray(0, 8),
//n个扣分序号 //n个扣分序号
// [].map(kf => string2Bytes(kf.markcatalog, 8)), // [].map(kf => string2Bytes(kf.markcatalog, 8)),
[], [],
//牵引车第二gps精度、纬度 //牵引车第二gps精度、纬度
string2Bytes(0, 4 * 8), string2Bytes(0, 4 * 8), NumberToByteArray(0, 4 * 8), NumberToByteArray(0, 4 * 8),
//牵引车第二航向角 //牵引车第二航向角
string2Bytes(0, 2 * 8), NumberToByteArray(0, 2 * 8),
//摩托压线 Byte[20], //摩托压线 Byte[20],
string2Bytes(0, 20 * 8), NumberToByteArray(0, 20 * 8),
//考试用时 //考试用时
string2Bytes(FillZero(0, 4), 4 * 8), NumberToByteArray(FillZero(0, 4), 4 * 8),
//项目用时 //项目用时
string2Bytes(FillZero(0, 2), 2 * 8), NumberToByteArray(FillZero(0, 2), 2 * 8),
//设备信号状态 //设备信号状态
string2Bytes(0, 4 * 8), NumberToByteArray(0, 4 * 8),
] ]
let result: number[] = []; let result: number[] = [];
arr.forEach(itemArr => { arr.forEach(itemArr => {
result = result.concat(itemArr) result = result.concat(itemArr)
}) })
AppStorage.setOrCreate("serialIndex", 0) // AppStorage.setOrCreate("serialIndex", 0)
this.serialIndex = 0
return [...new Uint8Array(result)] return [...new Uint8Array(result)]
} }
@ -158,13 +142,12 @@ class JudgeUdpBusiness {
} }
sendData(bytes: number[]) { sendData(bytes: number[]) {
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
if (this.udpIndex > this.currentUdpIndex) { if (this.udpIndex > this.currentUdpIndex) {
this.udp.sendMsgExt({ this.udp.sendMsgExt({
id: 45, id: 45,
list: bytes, list: bytes,
carNo: carInfo.carNo!, carNo: this.carInfo.carNo!,
placeId: carInfo.examinationRoomId! placeId: this.carInfo.examinationRoomId!
}) })
this.currentUdpIndex = this.udpIndex this.currentUdpIndex = this.udpIndex
} }
@ -172,35 +155,33 @@ class JudgeUdpBusiness {
//申请远程扣分查询 //申请远程扣分查询
askKf(directives: number) { askKf(directives: number) {
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
CenterUDPBusinessInstance.sendData({ CenterUDPBusinessInstance.sendData({
id: 35, id: 35,
list: [directives], list: [directives],
carNo: carInfo.carNo!, carNo: this.carInfo.carNo!,
placeId: carInfo.examinationRoomId!, placeId: this.carInfo.examinationRoomId!,
}) })
console.info('surenjun', `考车查询扣分项目内容,请求指令为:${directives}`) console.info('surenjun', `考车查询扣分项目内容,请求指令为:${directives}`)
} }
//确定远程扣分 //确定远程扣分
confirmKf(directives: number, code: number) { confirmKf(directives: number, code: number) {
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
CenterUDPBusinessInstance.sendData({ CenterUDPBusinessInstance.sendData({
id: 37, id: 37,
list: [directives, code], list: [directives, code],
carNo: carInfo.carNo!, carNo: this.carInfo.carNo!,
placeId: carInfo.examinationRoomId! placeId: this.carInfo.examinationRoomId!
}) })
console.info('surenjun', `考车发送确定扣分指令,指令为:${directives}`) console.info('surenjun', `考车发送确定扣分指令,指令为:${directives}`)
} }
async init(): Promise<void> { async init(config: EnvironmentConfigurationType, carInfo: CarInfoType, singlePlay: boolean, lsh: string): Promise<void> {
this.lsh = lsh
this.carInfo = carInfo
this.singlePlay = singlePlay
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
let result: EnvironmentConfigurationType = this.udp.create(config.udplocalIp!, '8080', carInfo.udpAddress!, carInfo.hintPort!).then(resolve).catch(reject)
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")!
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
this.udp.create(result.udplocalIp!, '8080', carInfo.udpAddress!, carInfo.hintPort!).then(resolve).catch(reject)
ObtainUdpBusinessInstance.onMsg(async (msg: string) => { ObtainUdpBusinessInstance.onMsg(async (msg: string) => {
let prevJd = 0, preWd = 0 let prevJd = 0, preWd = 0
@ -228,7 +209,7 @@ class JudgeUdpBusiness {
prevJd = jd; prevJd = jd;
preWd = wd; preWd = wd;
} }
AppStorage.set("udpIndex", udpIndex + 1) // AppStorage.setOrCreate("udpIndex", udpIndex + 1)
}) })
setInterval(() => { setInterval(() => {
this.udpIndex += 1 this.udpIndex += 1
@ -259,9 +240,9 @@ class JudgeUdpBusiness {
private setMsgHead(params: UDPParamType) { private setMsgHead(params: UDPParamType) {
const lshNo = SerialNumberInstance.get() const lshNo = SerialNumberInstance.get()
let a = string2Bytes(Number(`${params.id}${fillZero(params.placeId, 3)}`), 2 * 8); let a = NumberToByteArray(Number(`${params.id}${fillZero(params.placeId, 3)}`), 2 * 8);
let b = string2Bytes(Number(`${fillZero(params.carNo, 4)}${lshNo}`), 4 * 8); let b = NumberToByteArray(Number(`${fillZero(params.carNo, 4)}${lshNo}`), 4 * 8);
let c = string2Bytes(params.list.length, 2 * 8); let c = NumberToByteArray(params.list.length, 2 * 8);
let result: number[] = [] let result: number[] = []
a?.forEach(item => result.push(item)) a?.forEach(item => result.push(item))
b?.forEach(item => result.push(item)) b?.forEach(item => result.push(item))
@ -291,14 +272,14 @@ class JudgeUdpBusiness {
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
arr.push(tempItems.slice(i * 8, (i + 1) * 8).join('')); arr.push(tempItems.slice(i * 8, (i + 1) * 8).join(''));
} }
return arr.map(numStr => parseInt(numStr, 2)).map(item => string2Bytes(item, 8)[0]) return arr.map(numStr => parseInt(numStr, 2)).map(item => NumberToByteArray(item, 8)[0])
} }
// 中心所有项目转换 // 中心所有项目转换
private getTranslateProject(): string[] { private getTranslateProject(): string[] {
const examSubject = AppStorage.get<string>("examSubject"); this.carInfo.examSubject
const tempItems: ProjectItemType[] = const tempItems: ProjectItemType[] =
(examSubject === '2' ? testKm2Items : testKm3Items).map((code: ProjectDataType) => { (this.carInfo.examSubject === '2' ? testKm2Items : testKm3Items).map((code: ProjectDataType) => {
let data: ProjectItemType = { let data: ProjectItemType = {
code, code,
status: '0' status: '0'
@ -310,7 +291,7 @@ class JudgeUdpBusiness {
const temp = tempItems.slice(i * 4, (i + 1) * 4); const temp = tempItems.slice(i * 4, (i + 1) * 4);
let tempArr = temp.map(item => item.status) let tempArr = temp.map(item => item.status)
if (i === 4) { if (i === 4) {
tempArr = examSubject === '2' tempArr = this.carInfo.examSubject === '2'
//bit36-bit39保留 //bit36-bit39保留
? tempArr.concat(['00', '00']) ? tempArr.concat(['00', '00'])
//bit30-bit39保留 //bit30-bit39保留
@ -416,4 +397,5 @@ class JudgeUdpBusiness {
} }
} }
export const JudgeUdpBusinessInstance = new JudgeUdpBusiness() export const JudgeUdpBusinessInstance = new JudgeUdpBusiness()

View File

@ -4,6 +4,7 @@ import { WorkerTag } from '../config';
import { CenterCallBackMsgType, WorkerBackMessage, WorkerMessage } from '../model'; import { CenterCallBackMsgType, WorkerBackMessage, WorkerMessage } from '../model';
import { CenterUDPBusinessInstance } from '../utils/business/CenterUdpBusiness'; import { CenterUDPBusinessInstance } from '../utils/business/CenterUdpBusiness';
import { DifferentialSignal } from '../utils/business/DifferentialSignal'; import { DifferentialSignal } from '../utils/business/DifferentialSignal';
import { JudgeUdpBusinessInstance } from '../utils/business/JudgeUdpBusiness';
import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness'; import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort; const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
@ -37,6 +38,9 @@ function initFn(result: WorkerMessage) {
CenterUDPBusinessInstance.init(result.config, result.carInfo, result.otherMessage) CenterUDPBusinessInstance.init(result.config, result.carInfo, result.otherMessage)
// 中心心跳 // 中心心跳
CenterUDPBusinessInstance.startHeartBeat() CenterUDPBusinessInstance.startHeartBeat()
// 初始化考试过程UDP
JudgeUdpBusinessInstance.init(result.config, result.carInfo, result.singlePlay, result.otherMessage.lsh)
} }
function getDataFn() { function getDataFn() {