fix: 优化一些公共方法

This commit is contained in:
wangzhongjie 2025-06-18 14:42:01 +08:00
parent 5ea66a4bae
commit 163793b5b1
5 changed files with 19 additions and 62 deletions

View File

@ -31,17 +31,6 @@ export function deepClone<T extends Object>(target: T): T {
return target; 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 { export function convertGpsCoord2(num: number): number {
@ -50,34 +39,3 @@ export function convertGpsCoord2(num: number): number {
const fen = tempNum % 100 + num - tempNum; const fen = tempNum % 100 + num - tempNum;
return du + fen / 60 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;
}

View File

@ -1,10 +1,10 @@
import { testRealExam } from '../dataTest/index'; import { testRealExam } from '../dataTest/index';
import { NumberToByteArray } from '../../../utils/Common'; import { ArrayToByteArray, NumberToByteArray } from '../../../utils/Common';
import systemTime from '@ohos.systemDateTime'; import systemTime from '@ohos.systemDateTime';
import { Array2Byte } from './Common';
import { CarInfoType } from '../../../model'; 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 {} interface Extend {}
@ -699,21 +699,21 @@ export const getTimeStr = async () => {
//蓝灯 //蓝灯
export function sendBlue() { export function sendBlue() {
const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00]; const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00];
const arrBlueBuffer = Array2Byte(arrBlue).buffer const arrBlueBuffer = ArrayToByteArray(arrBlue).buffer
// globalThis.lightLineUdp.send(arrBlueBuffer); // globalThis.lightLineUdp.send(arrBlueBuffer);
} }
//绿灯 //绿灯
export function sendGreen() { export function sendGreen() {
const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01]; const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01];
const arrGreenBuffer = Array2Byte(arrGreen).buffer const arrGreenBuffer = ArrayToByteArray(arrGreen).buffer
// globalThis.lightLineUdp.send(arrGreenBuffer); // globalThis.lightLineUdp.send(arrGreenBuffer);
} }
//红灯 //红灯
export function sendRed() { export function sendRed() {
const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00]; const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00];
const arrRedBuffer = Array2Byte(arrRed).buffer const arrRedBuffer = ArrayToByteArray(arrRed).buffer
// globalThis.lightLineUdp.send(arrRedBuffer); // globalThis.lightLineUdp.send(arrRedBuffer);
} }

View File

@ -69,7 +69,7 @@ export function ArrayToByteArray(array: number[]): Uint8Array {
} }
/** /**
* 字符串转字节数组 * 字转字节数组
* @param number 要转换的数字 * @param number 要转换的数字
* @param len 字节数 * @param len 字节数
* @returns 返回字节数组 * @returns 返回字节数组

View File

@ -6,8 +6,7 @@ import {
OtherMessageType, OtherMessageType,
UDPParamType UDPParamType
} from '../../model'; } from '../../model';
import { fillZero, string2Bytes } from '../../pages/judgeSDK/utils/Common'; import { ArrayToByteArray, FillZero, NumberToByteArray } from '../Common';
import { ArrayToByteArray, NumberToByteArray } from '../Common';
import UdpClient from '../UdpUtils'; import UdpClient from '../UdpUtils';
import { CenterUdpTag, UDPTag } from '../../config'; import { CenterUdpTag, UDPTag } from '../../config';
import { SerialNumberInstance } from '../SerialNumber'; import { SerialNumberInstance } from '../SerialNumber';
@ -116,8 +115,8 @@ class CenterUDPBusiness {
for (let i = 0; i < dataView?.byteLength; ++i) { for (let i = 0; i < dataView?.byteLength; ++i) {
arr[i] = dataView?.getUint8(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 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 length = Number('0x' + FillZero(arr[7].toString(16), 2) + FillZero(arr[6].toString(16), 2));
let list: number[] = [] let list: number[] = []
for (let i = this.headLength; i <= this.headLength + length - 1; i++) { for (let i = this.headLength; i <= this.headLength + length - 1; i++) {
list.push(arr[i]) list.push(arr[i])
@ -147,9 +146,9 @@ class CenterUDPBusiness {
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))

View File

@ -9,7 +9,7 @@ import {
UDPParamType UDPParamType
} from '../../model'; } from '../../model';
import { testKm2Items, testKm3Items } from '../../pages/judgeSDK/dataTest'; import { testKm2Items, testKm3Items } from '../../pages/judgeSDK/dataTest';
import { fillZero, } from '../../pages/judgeSDK/utils/Common';
import { JudgeConfig } from '../../config'; import { JudgeConfig } from '../../config';
import { FillZero, NumberToByteArray, StringToASCII } from '../Common'; import { FillZero, NumberToByteArray, StringToASCII } from '../Common';
import UdpClient from '../UdpUtils'; import UdpClient from '../UdpUtils';
@ -235,8 +235,8 @@ class JudgeUdpBusiness {
private setMsgHead(params: UDPParamType) { private setMsgHead(params: UDPParamType) {
const lshNo = SerialNumberInstance.get() const lshNo = SerialNumberInstance.get()
let a = NumberToByteArray(Number(`${params.id}${fillZero(params.placeId, 3)}`), 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 b = NumberToByteArray(Number(`${FillZero(params.carNo, 4)}${lshNo}`), 4 * 8);
let c = NumberToByteArray(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))