From b35bc24f85e6de0f774c5ae2676096ae1740c69f Mon Sep 17 00:00:00 2001 From: lv <1344032923@qq.com> Date: Fri, 28 Mar 2025 11:33:07 +0800 Subject: [PATCH] =?UTF-8?q?udp=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/model/Common.ets | 8 ++ entry/src/main/ets/pages/CarCheck.ets | 6 +- entry/src/main/ets/pages/Index.ets | 37 ++++--- entry/src/main/ets/pages/UserInfo.ets | 33 +++--- .../ets/pages/compontents/FaceCompare.ets | 58 +++++----- entry/src/main/ets/utils/UdpUtils.ets | 3 +- .../ets/utils/business/CentralHeartbeat.ets | 100 ++++++++++++++++-- 7 files changed, 170 insertions(+), 75 deletions(-) diff --git a/entry/src/main/ets/model/Common.ets b/entry/src/main/ets/model/Common.ets index e7670b44..fb343e38 100644 --- a/entry/src/main/ets/model/Common.ets +++ b/entry/src/main/ets/model/Common.ets @@ -160,4 +160,12 @@ interface VersionType { interface VersionInfo { km2: string[]; km3: string[]; +} + + +export interface centerCallBackMsgType { + id: number; + length: number; + body:number[]; + sendId:number; } \ No newline at end of file diff --git a/entry/src/main/ets/pages/CarCheck.ets b/entry/src/main/ets/pages/CarCheck.ets index 3f22ade5..85ee7913 100644 --- a/entry/src/main/ets/pages/CarCheck.ets +++ b/entry/src/main/ets/pages/CarCheck.ets @@ -8,6 +8,7 @@ import { CarCheckDataType, CarConfigurationParams, CarInfoType, SpzdType } from import { BusinessError } from '@ohos.base'; import { voiceService } from '../utils/Voice'; import dayTs from '../utils/Date'; +import { ObtainSignalData } from '../utils/business/ObtainSignalData'; @Entry @Component @@ -179,9 +180,7 @@ struct Index { getPLCInfo() { const that = this - globalThis.udpClient.onMessage_1 && globalThis.udpClient.onMessage_1((msg: string) => { - console.log('getUDPonMessage_1byCarCheck2') - + ObtainSignalData.getData((msg)=>{ if (!this.breakFlag) { return } @@ -240,6 +239,7 @@ struct Index { } }) + } stopCheck() { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index c24fd614..8e6cc709 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -10,7 +10,7 @@ import promptAction from '@ohos.promptAction'; import errorMsgDialog from './compontents/errorMsgDialog'; import imageBtn from './compontents/imageBtn'; import VoiceAnnounce from './judgeSDK/utils/voiceAnnouncements'; -import { BaseInfoType } from '../model/Common'; +import { BaseInfoType, centerCallBackMsgType } from '../model/Common'; import { CarInfoType, InitializeTheCentralTableType, @@ -24,6 +24,8 @@ import { delPic } from '../utils/Video'; import { FileHelper } from '../utils/FileHelp'; import GetDistance from '../utils/business/GetDistance'; import { GetCurrentTime, NumberToByteArray } from '../utils/Common'; +import { ObtainSignalData } from '../utils/business/ObtainSignalData'; +import { CentralHeartbeat } from '../utils/business/CentralHeartbeat'; @Entry @@ -431,17 +433,17 @@ struct Index { } async heartMsg() { - 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 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]) + // } // TODO 未改 // const param = { // id: 31, @@ -502,11 +504,18 @@ struct Index { async initParams() { //设置plc udp 同步requesthost - await getUDP(this.context, false) + // await getUDP(this.context, false) + ObtainSignalData.init(); + CentralHeartbeat.init(); + CentralHeartbeat.getData((data:centerCallBackMsgType)=>{ + if (data.id == 32) { + AppStorage.setOrCreate('signNum', data.body[1]) + } + },) this.loading = false await GetDeviceInfo(this.context) await GetCarInfo() - await getUDP2(this.context, false) + // await getUDP2(this.context, false) getTCP() this.carInfo = AppStorage.get('carInfo') this.deviceId = this.carInfo.carNo diff --git a/entry/src/main/ets/pages/UserInfo.ets b/entry/src/main/ets/pages/UserInfo.ets index 35ae7a2b..14748a8c 100644 --- a/entry/src/main/ets/pages/UserInfo.ets +++ b/entry/src/main/ets/pages/UserInfo.ets @@ -22,6 +22,7 @@ import { CryptoJS } from '@ohos/crypto-js'; import { BeginExamRequest, CarInfoType, + centerCallBackMsgType, DrvexamType, ExaminationStuAbsentParams, ExaminerLoginInfo, @@ -44,6 +45,7 @@ import dayTs from '../utils/Date'; import { GetCurrentTime, NumberToByteArray } from '../utils/Common'; import FileUtils from '../utils/FileUtils'; import DB from '../utils/DbSql'; +import { CentralHeartbeat } from '../utils/business/CentralHeartbeat'; @Entry @Component @@ -466,20 +468,19 @@ struct UserInfo { async heartMsg() { // TODO UDP缺失 - // globalThis.udpClient2 & globalThis.udpClient2.setMsgCallBack(async (val) => { - // if (val.id == '32') { - // AppStorage.setOrCreate('signNum', val.body[1]) - // if (val.body[0] == '7') { - // //缺考处理 - // this.getqkFn() - // this.signNum = val.body[1] - // } - // } else if (val.id == '42') { - // //收到中心缺考确认消息 - // console.log('qkfnqkfn', val.body[0]) - // this.qkFn() - // } - // }) + CentralHeartbeat.getData((val:centerCallBackMsgType)=>{ + if (val.id == 32) { + AppStorage.setOrCreate('signNum', val.body[1]) + if (val.body[0] == 7) { + //缺考处理 + this.getqkFn() + this.signNum = val.body[1] + } + } else if (val.id == 42) { + //收到中心缺考确认消息 + this.qkFn() + } + },) } //考点端查询缺考指令内容消息请求 @@ -816,8 +817,8 @@ struct UserInfo { carNo: this.carInfo.carNo, placeId: this.carInfo.examinationRoomId } - - globalThis.udpClient2.sendMsgExt(param, this.context) + CentralHeartbeat.sendData(param) + // globalThis.udpClient2.sendMsgExt(param, this.context) if (res.examinationStuAbsentRsp.head.resultCode == '0') { this.pageIndex = 0 this.qkFlag = false diff --git a/entry/src/main/ets/pages/compontents/FaceCompare.ets b/entry/src/main/ets/pages/compontents/FaceCompare.ets index 0843e8b9..bfd5e3d7 100644 --- a/entry/src/main/ets/pages/compontents/FaceCompare.ets +++ b/entry/src/main/ets/pages/compontents/FaceCompare.ets @@ -9,6 +9,7 @@ import { VideoConfigData } from '../../mock'; import { CarInfoType, UDPParamType, VideoConfig } from '../../model'; import { NumberToByteArray } from '../../utils/Common'; import FileUtils from '../../utils/FileUtils'; +import { CentralHeartbeat } from '../../utils/business/CentralHeartbeat'; interface ParamType { id?: number; @@ -185,19 +186,6 @@ export default struct FaceCompare { this.vocObj && this.vocObj.releasePlayer() } - getqkFn() { - let tmpList: number[] = []; - tmpList.push(NumberToByteArray(AppStorage.get ('signNum'), 1 * 8)[0]) - const param: ParamType = { - id: 41, - list: tmpList, - carNo: this.carInfo.carNo, - placeId: this.carInfo.examinationRoomId - } - // TODO UPD缺失 - // globalThis.udpClient2.sendMsgExt(param, this.context) - } - async faceComparFn() { console.log('mmmmm0', 2) this.param.pztd = this.param.rlls @@ -254,6 +242,7 @@ export default struct FaceCompare { this.callBackFlag = true } } + CentralHeartbeat.sendData(param) // UDP缺失 // globalThis.udpClient2 && globalThis.udpClient2.sendMsgExt(param, this.context) clearInterval(this.interval) @@ -265,31 +254,32 @@ export default struct FaceCompare { carNo: this.carInfo.carNo, placeId: this.carInfo.examinationRoomId, } + CentralHeartbeat.sendData(param2) + // TODO UDP缺失 // globalThis.udpClient2 && globalThis.udpClient2.sendMsgExt(param2, this.context) } }, 1000) - // TODO UDP缺失 - // globalThis.udpClient2.setMsgCallBack((val) => { - // if (AppStorage.get('statue') != 3) { - // return - // } - // if (val.id == '48') { - // if (val.body[13] == '1') { - // this.showFaceCompareFlag = !this.showFaceCompareFlag - // this.showFaceCompare = !this.showFaceCompare - // this.vocObj && this.vocObj.releasePlayer() - // this.faceCompareSucess = 1 - // clearInterval(this.interval) - // } else if (val.body[13] == '0' && this.callBackFlag) { - // AppStorage.setOrCreate('statue', 2) - // this.vocObj && this.vocObj.playAudio({ - // type: 1, - // name: 'face_chekc_fail.wav' - // }) - // } - // } - // }) + CentralHeartbeat.getData((val)=>{ + if (AppStorage.get('statue') != 3) { + return + } + if (val.id == 48) { + if (val.body[13] == 1) { + this.showFaceCompareFlag = !this.showFaceCompareFlag + this.showFaceCompare = !this.showFaceCompare + this.vocObj && this.vocObj.releasePlayer() + this.faceCompareSucess = 1 + clearInterval(this.interval) + } else if (val.body[13] == 1 && this.callBackFlag) { + AppStorage.setOrCreate('statue', 2) + this.vocObj && this.vocObj.playAudio({ + type: 1, + name: 'face_chekc_fail.wav' + }) + } + } + }) } diff --git a/entry/src/main/ets/utils/UdpUtils.ets b/entry/src/main/ets/utils/UdpUtils.ets index b3222b05..9bde64ef 100644 --- a/entry/src/main/ets/utils/UdpUtils.ets +++ b/entry/src/main/ets/utils/UdpUtils.ets @@ -3,6 +3,7 @@ import common from '@ohos.app.ability.common' import FileUtils from './FileUtils' import promptAction from '@ohos.promptAction' import { CarInfoType } from '../model' +import buffer from '@ohos.buffer' type DealMethod = (value: ArrayBuffer) => string @@ -54,7 +55,7 @@ export default class UdpClient { this.errorEvents.push(callback) } - sendMsg(data: string): Promise { + sendMsg(data: ArrayBuffer|string): Promise { return this.udp?.getState().then(() => { return this.udp.send({ data, diff --git a/entry/src/main/ets/utils/business/CentralHeartbeat.ets b/entry/src/main/ets/utils/business/CentralHeartbeat.ets index 8555a68e..f7ee9acb 100644 --- a/entry/src/main/ets/utils/business/CentralHeartbeat.ets +++ b/entry/src/main/ets/utils/business/CentralHeartbeat.ets @@ -1,10 +1,14 @@ // 中心心跳/发送消息 -import { EnvironmentConfigurationType } from '../../model'; +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() { } @@ -12,26 +16,108 @@ class centralHeartbeat { // 初始化 init() { let config: EnvironmentConfigurationType = - AppStorage.get("EnvironmentConfiguration") + AppStorage.get("EnvironmentConfiguration") this.centralHeartbeatUdp = new UdpClient(); this.centralHeartbeatUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp, config.udpOppositeIpPort); } // 接受中心远程指令 - getData(callback: (data: ArrayBuffer) => void) { + getData(callback: (data: centerCallBackMsgType) => void) { this.centralHeartbeatUdp.onMessage((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) - callback(data); + 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); + } + // 发送消息 - sendData() { + sendHeartData() { // 组装消息,一秒发送一次 - let data = "1"; + // let data = "1"; this.timer = setInterval(() => { - this.centralHeartbeatUdp.sendMsg(data); + 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); }