fix: 优化中心udp

This commit is contained in:
wangzhongjie 2025-06-18 14:15:05 +08:00
parent 59b95dc609
commit 5ea66a4bae
6 changed files with 76 additions and 30 deletions

View File

@ -31,4 +31,7 @@ export const RequestTag = '[Request]';
export const EntryTag = '[Entry]';
//worker tag
export const WorkerTag = '[Worker]';
export const WorkerTag = '[Worker]';
//中心udp
export const CenterUdpTag = '[CenterUdpBusiness]'

View File

@ -37,6 +37,7 @@ struct Index {
@State fd: number = -1;
@State carInfo: CarInfoType = {};
@State num: number = 0;
@State isPlay: boolean = false;
// 请求网络表等待弹窗
customDialogController: CustomDialogController = new CustomDialogController({
builder: LoadingComponent(),
@ -77,7 +78,10 @@ struct Index {
async onPageShow(): Promise<void> {
console.log("首页 onPageShow")
await UseAuth(this.context)
this.avPlayer.playAudio(['welcome.wav'])
if (!this.isPlay) {
this.avPlayer.playAudio(['welcome.wav'])
this.isPlay = true
}
this.baseInfo = AppStorage.get<BaseInfoType>('baseInfo')!
this.initParams()

View File

@ -28,7 +28,8 @@ export default struct SignalDisplayComponent {
aboutToAppear(): void {
DifferentialAndSignal.onMsg((data: string) => {
const result: WorkerBackMessage = JSON.parse(data)
if (result.type === "centerUdpData") {
console.log("wzj", data)
if (result.type === "obtainUdpData") {
this.getSignal(result.data as string)
this.rawData = result.data as string
}
@ -36,11 +37,11 @@ export default struct SignalDisplayComponent {
}
getSignal = (msg: string) => {
const strachArr = msg.split(',')
if (strachArr[0] != '#DN_GD') {
const newArr = msg.split(',')
if (newArr[0] != '#DN_GD') {
return
}
this.signArr = strachArr
this.signArr = newArr
for (let i = 0; i <= 12; i++) {
this.vehicleSignal[i].value = this.signArr[i+2]
@ -155,6 +156,7 @@ export default struct SignalDisplayComponent {
CoordinateComponent({
isItHorizontal: false,
heightNum: 40,
data: this.GPSColum
})
}.width("20%")
@ -358,7 +360,7 @@ struct TabItemComponent {
@Component
struct GPSComponent {
@State data: Array<string> = []
@Prop data: Array<string>
@State ip: string = ""
aboutToAppear(): void {
@ -439,7 +441,7 @@ struct GPSComponent {
@Component
struct CarComponent {
@State data: Array<SignalDataType> = SignalData
@Prop data: Array<SignalDataType>
build() {
Flex({
@ -457,7 +459,7 @@ struct CarComponent {
@Component
struct CoordinateComponent {
@State data: Array<SignalDataType> = GPSData
@Prop data: Array<SignalDataType>
@State isItHorizontal: boolean = true
@State heightNum: number = 65
@ -490,7 +492,7 @@ struct CoordinateComponent {
@Component
struct blockComponent {
@State label: string = "发送次数"
@State value: string = "1"
@Prop value: string = "1"
@State widthNum: number = 130
@State heightNum: number = 65
@State color: string = "#FDF5E7"

View File

@ -7,9 +7,9 @@ import {
UDPParamType
} from '../../model';
import { fillZero, string2Bytes } from '../../pages/judgeSDK/utils/Common';
import { NumberToByteArray } from '../Common';
import { ArrayToByteArray, NumberToByteArray } from '../Common';
import UdpClient from '../UdpUtils';
import { UDPTag } from '../../config';
import { CenterUdpTag, UDPTag } from '../../config';
import { SerialNumberInstance } from '../SerialNumber';
// 中心UDP业务逻辑
@ -41,29 +41,33 @@ class CenterUDPBusiness {
}
startHeartBeat() {
console.log("心跳")
console.log(CenterUdpTag, "心跳", JSON.stringify(this.carInfo))
// 组装消息,一秒发送一次
this.timer = setInterval(() => {
// 生成流水号
SerialNumberInstance.generate()
console.log(CenterUdpTag, "流水号生成")
// SetSerialNumber()
const arr = [this.signNum || 0, this.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 = this.lsh || '0000000000000'
for (let i = 0; i < str.length; i++) {
tmpList.push(NumberToByteArray(str.charCodeAt(i), 1 * 8)[0])
}
console.log(CenterUdpTag, "学员流水号", JSON.stringify(tmpList))
const data: UDPParamType = {
id: 31,
list: tmpList,
carNo: this.carInfo.carNo!,
placeId: this.carInfo.examinationRoomId!
}
console.log(CenterUdpTag, "查看生成的", JSON.stringify(data))
const param = this.setWholeMsg(data)
console.log("查看")
console.log(CenterUdpTag, "查看", JSON.stringify(param))
this.udp.sendMsg(param);
}, 1000);
}
@ -84,12 +88,16 @@ class CenterUDPBusiness {
// 初始化
async init(config: EnvironmentConfigurationType, carInfo: CarInfoType, otherMessage: OtherMessageType) {
console.log(CenterUdpTag, "初始化", JSON.stringify(otherMessage), JSON.stringify(carInfo))
try {
this.signNum = otherMessage.signNum || 0
this.statue = otherMessage.statue || '1'
this.lsh = otherMessage.lsh || '0000000000000'
this.carInfo = carInfo
this.udp.create(config.udplocalIp!, '8800', carInfo.udpAddress!, carInfo.messagePort!)
// TODO
// this.udp.create(config.udplocalIp!, '8800', carInfo.udpAddress!, carInfo.messagePort!)
this.udp.create(config.udplocalIp!, '8800', "112.80.35.83", "11056")
this.udp.setDealMethod(this.dealMsg)
} catch (e) {
console.error(UDPTag, "初始化中心 udp失败")
@ -133,16 +141,7 @@ class CenterUDPBusiness {
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;
return ArrayToByteArray(arr).buffer
}
private setMsgHead(params: UDPParamType) {

View File

@ -304,34 +304,59 @@ class JudgeUdpBusiness {
let origin = plc.split(",")
let p = origin.map(item => Number(item))
let sensor: Sensor = {
// 安全带
aqd: p[19],
// 主驾驶门
mkg: p[14],
// 手刹
ssc: p[13],
// 脚刹
jsc: p[12],
// 副刹车
fsc: p[18],
// 离合
lhq: p[17],
// 喇叭
lb: p[4],
// 示廓灯
skd: p[9],
// 近光灯
jgd: p[7],
// 远光灯
ygd: p[8],
//左方向灯 右方向灯 双跳灯 雾灯 雨刮器 点火1 点火2 发动机转速 档位 车速
//左方向灯
zfxd: p[2],
// 右方向灯
yfxd: p[3],
// 双跳灯
shtd: p[20],
// 雾灯
wd: p[10],
// 雨刮器
ygq: p[11],
// 点火1
dh1: p[5],
// 点火2
dh2: p[6],
// 发动机转速
fdjzs: p[25],
// 档位
dw: p[28],
// 车速
cs: p[23],
// 方向盘角度
fxp: Number(origin[27].split('_')[0]),
//累计脉冲 溜车脉冲 超声波左后 超声波右后 超声波右前 超声波左前 座椅 仪表盘 后视镜 倒车镜 光照 雨量
//累计脉冲
ljmc: p[24],
// 溜车脉冲
lcmc: 0,
// 超声波4 左后
csbzh: p[32],
// 超声波2 右后
csbyh: p[30],
// 超声波3 右前
csbyq: p[31],
// 超声波1 左前
csbzq: p[29],
zy: 0,
tbp: 0,
@ -347,20 +372,34 @@ class JudgeUdpBusiness {
mndg: ''
}
let gps: Gps = {
// GPS板卡类型
bklx: p[56],
// GPS状态
dwzt: p[83],
//航向角状态-收星数
jdzt: Number(origin[92].split('-')[0]),
sj: time,
// 纬度
jd: p[96],
// 经度
wd: p[95],
// 航向角
hxj: p[90],
// 俯仰角
fyj: p[91],
// 海拔高
hbg: p[85],
// 高度差
gdc: p[86],
// 速度
sd: p[97],
// 龄期
age: p[87],
// 经度因子
jdyz: p[89],
// 维度因子
wdyz: p[88],
// 收星数
dwsxs: p[84],
jdsxs: Number(origin[92].split('-')[1])
}

View File

@ -4,7 +4,6 @@ import { WorkerTag } from '../config';
import { CenterCallBackMsgType, WorkerBackMessage, WorkerMessage } from '../model';
import { CenterUDPBusinessInstance } from '../utils/business/CenterUdpBusiness';
import { DifferentialSignal } from '../utils/business/DifferentialSignal';
import { JudgeUdpBusinessInstance } from '../utils/business/JudgeUdpBusiness';
import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
@ -39,7 +38,7 @@ function initFn(result: WorkerMessage) {
// 中心心跳
CenterUDPBusinessInstance.startHeartBeat()
// 初始化考试过程UDP
JudgeUdpBusinessInstance.init(result.config, result.carInfo, result.singlePlay, result.otherMessage.lsh)
// JudgeUdpBusinessInstance.init(result.config, result.carInfo, result?.singlePlay || false, result.otherMessage.lsh)
}
@ -48,7 +47,7 @@ function getDataFn() {
DifferentialSignal.getData((data: ArrayBuffer) => {
console.log(WorkerTag, "Received differential signal data:", data.byteLength, "bytes")
// TCP拿到差分改正数发给后置机
ObtainUdpBusinessInstance.sendData(data)
// ObtainUdpBusinessInstance.sendData(data)
})
// 后置机回执PLC和GPS
ObtainUdpBusinessInstance.onMsg((data: string) => {