import socket from '@ohos.net.socket' import promptAction from '@ohos.promptAction' import { CarInfoType, EnvironmentConfigurationType } from '../model' import { UDPTag } from '../config' import { BusinessError } from '@ohos.base' import { FillZero, StringToBytes, StringToASCII } from './Common' import { CenterUDPBusinessInstance } from './business/CenterUdpBusiness' interface MsgExt { id: number, list: number[], carNo: string, placeId: string, } function generateHead(param: MsgExt): number[] { let lshNo = AppStorage.get("lsh") let a = new Uint16Array(StringToASCII(`${param.id}${FillZero(param.placeId, 3)}`)) let b = new Uint32Array(StringToASCII(`${FillZero(param.carNo, 4)}${lshNo}`)) let c = new Uint16Array(param.list.length) return [...a, ...b, ...c]; } function exclusive(target: number[]): number[] { let result = target[0] for (let i = 1; i < target.length; i++) { result = result ^ target[i] } return [result]; } type DealMethod = (value: ArrayBuffer) => T export default class UdpClient { private localIp: string = '' private localIpPort: string = '' private oppositeIp: string = '' private oppositeIpPort: string = '' protected udp: socket.UDPSocket | null = null private messageEvents: Array = [] private errorEvents: Array = [] private dealMethod?: DealMethod private bindEvent() { this.udp?.on("message", value => { let result = this.dealMethod?.(value.message) this.messageEvents.forEach(cb => { cb(result) }) }) this.udp?.on("error", (err) => { console.log(UDPTag, 'udp error', JSON.stringify(err)) this.errorEvents.forEach(cb => { cb(err) }) }) } bindUdp(): Promise | undefined { return this.udp?.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 }) } async reBind() { await this.close() this.udp = socket.constructUDPSocketInstance(); this.bindEvent() await this.bindUdp() } close(): Promise | undefined { this.messageEvents = [] this.errorEvents = [] this.dealMethod = undefined this.udp?.off("message") this.udp?.off("error") return this.udp?.close() } setDealMethod(fun: DealMethod) { this.dealMethod = fun } sendMsg(data: ArrayBuffer | string): Promise | undefined { return this.udp?.send({ data, address: { address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1 } }) } setWholeMsg(params: MsgExt): ArrayBuffer { let head = generateHead(params); let headJudge = exclusive(head); let body = params.list; let bodyJudge = exclusive(body); let end = [13, 10]; const arr = [...head, ...headJudge, ...body, ...bodyJudge, ...end] return new Uint8Array(arr).buffer } sendMsgExt(param: MsgExt) { const msgData = this.setWholeMsg(param) this.sendMsg(msgData) } async create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { await this.close() this.localIp = udpLocalIp this.oppositeIp = udpOppositeIp this.localIpPort = udpLocalIpPort this.oppositeIpPort = udpOppositeIpPort this.udp = socket.constructUDPSocketInstance(); this.bindEvent() return this.bindUdp() } onMsg(callback: Function) { if (this.messageEvents.findIndex(cb => cb === callback)) { return } this.messageEvents.push(callback) } onError(callback: Function) { this.errorEvents.push(callback) } offMsg(callback: Function) { this.messageEvents = this.messageEvents.filter(cb => cb !== callback) } }