149 lines
4.7 KiB
Plaintext
149 lines
4.7 KiB
Plaintext
// 中心心跳/发送消息
|
|
import { CarInfoType, centerCallBackMsgType, EnvironmentConfigurationType, UDPParamType } from '../../model';
|
|
import { fillZero, string2Bytes } from '../../pages/judgeSDK/utils/Common';
|
|
import { NumberToByteArray } from '../Common';
|
|
import UdpClient from '../UdpUtils';
|
|
import { UDPTag } from '../../config';
|
|
|
|
// 中心UDP业务逻辑
|
|
class CenterUDPBusiness {
|
|
private static instance: CenterUDPBusiness
|
|
private udp: UdpClient = new UdpClient()
|
|
private timer: number = -1
|
|
private headLength: number = 9
|
|
private sendId: number = 0
|
|
|
|
constructor() {
|
|
if (CenterUDPBusiness.instance) {
|
|
CenterUDPBusiness.instance = this
|
|
}
|
|
return CenterUDPBusiness.instance
|
|
}
|
|
|
|
private dealMsg(msg: ArrayBuffer): centerCallBackMsgType {
|
|
let arr: number[] = []
|
|
let dataView = new DataView(msg)
|
|
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 list: number[] = []
|
|
for (let i = this.headLength; i <= this.headLength + length - 1; i++) {
|
|
list.push(arr[i])
|
|
}
|
|
return {
|
|
id,
|
|
length,
|
|
body: list,
|
|
sendId: this.sendId
|
|
}
|
|
}
|
|
|
|
private 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: number[] = []
|
|
head.forEach(item => arr.push(item))
|
|
headJudge.forEach(item => arr.push(item))
|
|
params.list?.forEach(item => arr.push(item))
|
|
bodyJudge.forEach(item => arr.push(item))
|
|
end.forEach(item => arr.push(item))
|
|
return this.array2Byte(arr).buffer
|
|
}
|
|
|
|
private 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;
|
|
}
|
|
|
|
private 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);
|
|
let result: number[] = []
|
|
a?.forEach(item => result.push(item))
|
|
b?.forEach(item => result.push(item))
|
|
c?.forEach(item => result.push(item))
|
|
return result;
|
|
}
|
|
|
|
private 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.udp.sendMsg(param);
|
|
}
|
|
|
|
startHeartBeat() {
|
|
// 组装消息,一秒发送一次
|
|
this.timer = setInterval(() => {
|
|
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.udp.sendMsg(param);
|
|
}, 1000);
|
|
}
|
|
|
|
onMsg(cb: (param: centerCallBackMsgType) => void) {
|
|
this.udp.onMsg(cb)
|
|
}
|
|
|
|
offMsg(cb: (param: centerCallBackMsgType) => void) {
|
|
this.udp.offMsg(cb)
|
|
}
|
|
|
|
async init(): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
try {
|
|
let result: EnvironmentConfigurationType =
|
|
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")!
|
|
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
|
|
this.udp.create(result.udplocalIp!, '8800', carInfo.udpAddress!, carInfo.messagePort!)
|
|
.then(resolve)
|
|
.catch(reject)
|
|
this.udp.setDealMethod(this.dealMsg)
|
|
} catch (e) {
|
|
reject(e)
|
|
console.error(UDPTag, "初始化中心 udp失败")
|
|
}
|
|
})
|
|
}
|
|
|
|
// 关闭所有动作
|
|
async close() {
|
|
clearInterval(this.timer)
|
|
return this.udp.close()
|
|
}
|
|
}
|
|
|
|
export const CenterUDPClientInstance = new CenterUDPBusiness(); |