// 中心心跳/发送消息 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 private headLenth: number = 9 private sendId: number = 0 constructor() { } // 初始化 init() { let config: EnvironmentConfigurationType = AppStorage.get("EnvironmentConfiguration") this.centralHeartbeatUdp = new UdpClient(); this.centralHeartbeatUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp, config.udpOppositeIpPort); } // 接受中心远程指令 getData(callback: (data: centerCallBackMsgType) => void) { this.centralHeartbeatUdp.onMsg((data: ArrayBuffer) => { 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) 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); }); } setWholeMsg(params: UDPParamType) { let head: Array = this.setMsgHead(params); let headJudge = this.setMessageExclusive(head); let bodyJudge = this.setMessageExclusive(params.list); let end = [13, 10]; const arr: Array = [...head, ...headJudge, ...params.list, ...bodyJudge, ...end] return this.Array2Byte(arr).buffer } //length消息体bufferlength id消息类型id bodyStr消息体string // setMsyBody(id,bodyByte){ Array2Byte(array: Array) { 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) { 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); } // 发送消息 sendHeartData() { // 组装消息,一秒发送一次 // let data = "1"; this.timer = setInterval(() => { const signNum = AppStorage.get('signNum') const statue = AppStorage.get('statue') const lsh = AppStorage.get('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('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();