subject-two/entry/src/main/ets/utils/business/CentralHeartbeat.ets

131 lines
4.2 KiB
Plaintext
Raw Normal View History

// 中心心跳/发送消息
2025-03-28 11:33:07 +08:00
import { CarInfoType, centerCallBackMsgType, EnvironmentConfigurationType, UDPParamType } from '../../model';
import { fillZero, string2Bytes } from '../../pages/judgeSDK/utils/Common';
import { NumberToByteArray } from '../Common';
import UdpClient from '../UdpUtils';
class centralHeartbeat {
private centralHeartbeatUdp: UdpClient;
private timer: number = -1
2025-03-28 11:33:07 +08:00
private headLenth: number = 9
private sendId: number = 0
constructor() {
}
// 初始化
init() {
let config: EnvironmentConfigurationType =
2025-04-07 14:05:15 +08:00
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")
this.centralHeartbeatUdp = new UdpClient();
this.centralHeartbeatUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp,
config.udpOppositeIpPort);
}
// 接受中心远程指令
2025-03-28 11:33:07 +08:00
getData(callback: (data: centerCallBackMsgType) => void) {
2025-04-07 14:05:15 +08:00
this.centralHeartbeatUdp.onMsg((data: ArrayBuffer) => {
2025-03-28 11:33:07 +08:00
let arr: number[] = []
let dataView = new DataView(data)
for (let i = 0; i < dataView?.byteLength; ++i) {
arr[i] = dataView?.getUint8(i)
}
let idNum = '0x' + fillZero(arr[1].toString(16), 2) + fillZero(arr[0].toString(16), 2);
let id = Math.floor(Number(idNum) / 1000)
2025-03-28 11:33:07 +08:00
let lengthNum = '0x' + fillZero(arr[7].toString(16), 2) + fillZero(arr[6].toString(16), 2);
let length = Number(lengthNum);
let list: number[] = []
for (let i = this.headLenth; i <= this.headLenth + length - 1; i++) {
list.push(arr[i])
}
const result: centerCallBackMsgType = {
id,
length,
body: list,
sendId: this.sendId
}
callback(result)
// callback(data);
});
}
2025-03-28 11:33:07 +08:00
setWholeMsg(params: UDPParamType) {
let head: Array<number> = this.setMsgHead(params);
let headJudge = this.setMessageExclusive(head);
let bodyJudge = this.setMessageExclusive(params.list);
let end = [13, 10];
const arr: Array<number> = [...head, ...headJudge, ...params.list, ...bodyJudge, ...end]
return this.Array2Byte(arr).buffer
}
//length消息体bufferlength id消息类型id bodyStr消息体string
// setMsyBody(id,bodyByte){
Array2Byte(array: Array<number>) {
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;
}
setMsgHead(params: UDPParamType) {
let a = string2Bytes(Number(`${params.id}${fillZero(params.placeId, 3)}`), 2 * 8);
let b = string2Bytes(Number(`${fillZero(params.carNo, 4)}${AppStorage.get('lshNo')}`), 4 * 8);
let c = string2Bytes(params.list.length, 2 * 8);
return [...a, ...b, ...c];
}
//异或运算
setMessageExclusive(tmpList: Array<number>) {
let result: number = tmpList[0];
for (let i = 1; i < tmpList.length; i++) {
result = result ^ tmpList[i]
}
return [result];
}
sendData(data: UDPParamType) {
this.sendId = data.id
const param = this.setWholeMsg(data)
this.centralHeartbeatUdp.sendMsg(param);
}
// 发送消息
2025-03-28 11:33:07 +08:00
sendHeartData() {
// 组装消息,一秒发送一次
2025-03-28 11:33:07 +08:00
// let data = "1";
this.timer = setInterval(() => {
2025-03-28 11:33:07 +08:00
const signNum = AppStorage.get<number>('signNum')
const statue = AppStorage.get<string>('statue')
const lsh = AppStorage.get<string>('lsh')
const arr = [signNum || 0, statue || 1]
let tmpList: number[] = [];
tmpList.push(NumberToByteArray(Number(arr[0]), 1 * 8)[0])
tmpList.push(NumberToByteArray(Number(arr[1]), 1 * 8)[0])
const str = lsh || '0000000000000'
for (let i = 0; i < str.length; i++) {
tmpList.push(NumberToByteArray(str.charCodeAt(i), 1 * 8)[0])
}
const carInfo = AppStorage.get<CarInfoType>('carInfo')
const data: UDPParamType = {
id: 31,
list: tmpList,
carNo: carInfo.carNo,
placeId: carInfo.examinationRoomId
}
const param = this.setWholeMsg(data)
this.centralHeartbeatUdp.sendMsg(param);
}, 1000);
}
// 关闭所有动作
close() {
clearInterval(this.timer);
this.centralHeartbeatUdp.close()
}
}
export const CentralHeartbeat = new centralHeartbeat();