From 163793b5b1af1ec3e830763a4a6cc408418428c6 Mon Sep 17 00:00:00 2001 From: wangzhongjie Date: Wed, 18 Jun 2025 14:42:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/pages/judgeSDK/utils/Common.ets | 44 +------------------ .../ets/pages/judgeSDK/utils/judgeCommon.ets | 16 +++---- entry/src/main/ets/utils/Common.ets | 2 +- .../ets/utils/business/CenterUdpBusiness.ets | 13 +++--- .../ets/utils/business/JudgeUdpBusiness.ets | 6 +-- 5 files changed, 19 insertions(+), 62 deletions(-) diff --git a/entry/src/main/ets/pages/judgeSDK/utils/Common.ets b/entry/src/main/ets/pages/judgeSDK/utils/Common.ets index 0df51a7..cb11255 100644 --- a/entry/src/main/ets/pages/judgeSDK/utils/Common.ets +++ b/entry/src/main/ets/pages/judgeSDK/utils/Common.ets @@ -16,7 +16,7 @@ export function chunkArr(arr: T[], size: number): T[][] { } //对象深拷贝 -export function deepClone(target: T): T { +export function deepClone(target: T): T { // 如果是对象,且不是原始值null if (typeof target === 'object' && target !== null) { // 创建容器 @@ -31,17 +31,6 @@ export function deepClone(target: T): T { return target; } -export function fillZero(str: string | number, len: number): string { - if (!str) { - return "" - } - str = str.toString(); - if (str.length >= len || len <= 0) { - return str; - } - const zeroStr = '0'.repeat(len - str.length); - return zeroStr + str; -} //经纬度转换 export function convertGpsCoord2(num: number): number { @@ -50,34 +39,3 @@ export function convertGpsCoord2(num: number): number { const fen = tempNum % 100 + num - tempNum; return du + fen / 60 } - -export function Array2Byte(array: number[]): Uint8Array { - const buf = new ArrayBuffer(array.length); - const view = new Uint8Array(buf); - for (let i = 0; i < array.length; i++) { - view[i] = array[i] & 0xFF; - } - return view; -} - -export function string2Bytes(number: number, len: number): number[] | undefined { - let str = Math.floor(number).toString(2); - if (str.length > len) { - console.log('数据长度不对~~'); - return; - } - const byteString = fillZero(str, len); - - const arrBytes: number[] = []; - for (let i = byteString.length; i > 0; ) { - let j = i - 8; - if (j < 0) { - j = 0; - } - const s = byteString.slice(j, i); - const v = parseInt(s, 2); - arrBytes.push(v); - i -= 8; - } - return arrBytes; -} \ No newline at end of file diff --git a/entry/src/main/ets/pages/judgeSDK/utils/judgeCommon.ets b/entry/src/main/ets/pages/judgeSDK/utils/judgeCommon.ets index 9acac9e..d923456 100644 --- a/entry/src/main/ets/pages/judgeSDK/utils/judgeCommon.ets +++ b/entry/src/main/ets/pages/judgeSDK/utils/judgeCommon.ets @@ -1,10 +1,10 @@ import { testRealExam } from '../dataTest/index'; -import { NumberToByteArray } from '../../../utils/Common'; +import { ArrayToByteArray, NumberToByteArray } from '../../../utils/Common'; import systemTime from '@ohos.systemDateTime'; -import { Array2Byte } from './Common'; + import { CarInfoType } from '../../../model'; -import { DefaultJudgeConfigObj, Gps, JudgeLane, Plc, Radar, Sensor, Vision } from '../../../model/Judge'; +import { DefaultJudgeConfigObj, Gps, JudgeLane, Plc, Radar, Vision } from '../../../model/Judge'; interface Extend {} @@ -102,7 +102,7 @@ export function getKmProjectVoice( xmxh: string ) { const carInfo = AppStorage.get('carInfo')!; - const examSubject = carInfo.examSubject||"2"; + const examSubject = carInfo.examSubject || "2"; const param506Str: number[] = (Reflect.get(judgeConfig, '506')?.split(',')) || [] const param512Str: number[] = (Reflect.get(judgeConfig, '512')?.split(',')) || [] const param544Str: number[] = (Reflect.get(judgeConfig, '544')?.split(',')) || [] @@ -595,7 +595,7 @@ export const plcStrToWXJson = async (plc: string) => { const p = plc.split(',').map((val, key) => { if (key !== 27 && key !== 92) { return Number(val) - }else{ + } else { return 0 } }); @@ -699,21 +699,21 @@ export const getTimeStr = async () => { //蓝灯 export function sendBlue() { const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00]; - const arrBlueBuffer = Array2Byte(arrBlue).buffer + const arrBlueBuffer = ArrayToByteArray(arrBlue).buffer // globalThis.lightLineUdp.send(arrBlueBuffer); } //绿灯 export function sendGreen() { const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01]; - const arrGreenBuffer = Array2Byte(arrGreen).buffer + const arrGreenBuffer = ArrayToByteArray(arrGreen).buffer // globalThis.lightLineUdp.send(arrGreenBuffer); } //红灯 export function sendRed() { const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00]; - const arrRedBuffer = Array2Byte(arrRed).buffer + const arrRedBuffer = ArrayToByteArray(arrRed).buffer // globalThis.lightLineUdp.send(arrRedBuffer); } diff --git a/entry/src/main/ets/utils/Common.ets b/entry/src/main/ets/utils/Common.ets index 76d94a3..d6ac4a0 100644 --- a/entry/src/main/ets/utils/Common.ets +++ b/entry/src/main/ets/utils/Common.ets @@ -69,7 +69,7 @@ export function ArrayToByteArray(array: number[]): Uint8Array { } /** - * 字符串转字节数组 + * 数字转字节数组 * @param number 要转换的数字 * @param len 字节数 * @returns 返回字节数组 diff --git a/entry/src/main/ets/utils/business/CenterUdpBusiness.ets b/entry/src/main/ets/utils/business/CenterUdpBusiness.ets index 3b64084..0b8ae4c 100644 --- a/entry/src/main/ets/utils/business/CenterUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/CenterUdpBusiness.ets @@ -6,8 +6,7 @@ import { OtherMessageType, UDPParamType } from '../../model'; -import { fillZero, string2Bytes } from '../../pages/judgeSDK/utils/Common'; -import { ArrayToByteArray, NumberToByteArray } from '../Common'; +import { ArrayToByteArray, FillZero, NumberToByteArray } from '../Common'; import UdpClient from '../UdpUtils'; import { CenterUdpTag, UDPTag } from '../../config'; import { SerialNumberInstance } from '../SerialNumber'; @@ -116,8 +115,8 @@ class CenterUDPBusiness { for (let i = 0; i < dataView?.byteLength; ++i) { arr[i] = dataView?.getUint8(i) } - let id = Math.floor(Number('0x' + fillZero(arr[1].toString(16), 2) + fillZero(arr[0].toString(16), 2)) / 1000) - let length = Number('0x' + fillZero(arr[7].toString(16), 2) + fillZero(arr[6].toString(16), 2)); + let id = Math.floor(Number('0x' + FillZero(arr[1].toString(16), 2) + FillZero(arr[0].toString(16), 2)) / 1000) + let length = Number('0x' + FillZero(arr[7].toString(16), 2) + FillZero(arr[6].toString(16), 2)); let list: number[] = [] for (let i = this.headLength; i <= this.headLength + length - 1; i++) { list.push(arr[i]) @@ -147,9 +146,9 @@ class CenterUDPBusiness { private setMsgHead(params: UDPParamType) { // 自增流水号 const lshNo = SerialNumberInstance.get() - let a = string2Bytes(Number(`${params.id}${fillZero(params.placeId, 3)}`), 2 * 8); - let b = string2Bytes(Number(`${fillZero(params.carNo, 4)}${lshNo}`), 4 * 8); - let c = string2Bytes(params.list.length, 2 * 8); + let a = NumberToByteArray(Number(`${params.id}${FillZero(params.placeId, 3)}`), 2 * 8); + let b = NumberToByteArray(Number(`${FillZero(params.carNo, 4)}${lshNo}`), 4 * 8); + let c = NumberToByteArray(params.list.length, 2 * 8); let result: number[] = [] a?.forEach(item => result.push(item)) b?.forEach(item => result.push(item)) diff --git a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets index 45a4ba3..6c313cf 100644 --- a/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets +++ b/entry/src/main/ets/utils/business/JudgeUdpBusiness.ets @@ -9,7 +9,7 @@ import { UDPParamType } from '../../model'; import { testKm2Items, testKm3Items } from '../../pages/judgeSDK/dataTest'; -import { fillZero, } from '../../pages/judgeSDK/utils/Common'; + import { JudgeConfig } from '../../config'; import { FillZero, NumberToByteArray, StringToASCII } from '../Common'; import UdpClient from '../UdpUtils'; @@ -235,8 +235,8 @@ class JudgeUdpBusiness { private setMsgHead(params: UDPParamType) { const lshNo = SerialNumberInstance.get() - let a = NumberToByteArray(Number(`${params.id}${fillZero(params.placeId, 3)}`), 2 * 8); - let b = NumberToByteArray(Number(`${fillZero(params.carNo, 4)}${lshNo}`), 4 * 8); + let a = NumberToByteArray(Number(`${params.id}${FillZero(params.placeId, 3)}`), 2 * 8); + let b = NumberToByteArray(Number(`${FillZero(params.carNo, 4)}${lshNo}`), 4 * 8); let c = NumberToByteArray(params.list.length, 2 * 8); let result: number[] = [] a?.forEach(item => result.push(item))