fix: 优化一些公共方法
This commit is contained in:
parent
5ea66a4bae
commit
163793b5b1
@ -16,7 +16,7 @@ export function chunkArr<T>(arr: T[], size: number): T[][] {
|
||||
}
|
||||
|
||||
//对象深拷贝
|
||||
export function deepClone<T extends Object>(target: T): T {
|
||||
export function deepClone<T extends Object>(target: T): T {
|
||||
// 如果是对象,且不是原始值null
|
||||
if (typeof target === 'object' && target !== null) {
|
||||
// 创建容器
|
||||
@ -31,17 +31,6 @@ export function deepClone<T extends Object>(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;
|
||||
}
|
||||
@ -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<CarInfoType>('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);
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ export function ArrayToByteArray(array: number[]): Uint8Array {
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转字节数组
|
||||
* 数字转字节数组
|
||||
* @param number 要转换的数字
|
||||
* @param len 字节数
|
||||
* @returns 返回字节数组
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user