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]'; export const EntryTag = '[Entry]';
//worker tag //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 fd: number = -1;
@State carInfo: CarInfoType = {}; @State carInfo: CarInfoType = {};
@State num: number = 0; @State num: number = 0;
@State isPlay: boolean = false;
// 请求网络表等待弹窗 // 请求网络表等待弹窗
customDialogController: CustomDialogController = new CustomDialogController({ customDialogController: CustomDialogController = new CustomDialogController({
builder: LoadingComponent(), builder: LoadingComponent(),
@ -77,7 +78,10 @@ struct Index {
async onPageShow(): Promise<void> { async onPageShow(): Promise<void> {
console.log("首页 onPageShow") console.log("首页 onPageShow")
await UseAuth(this.context) 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.baseInfo = AppStorage.get<BaseInfoType>('baseInfo')!
this.initParams() this.initParams()

View File

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

View File

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

View File

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

View File

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