190 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-01-05 11:11:15 +08:00
import systemTime from '@ohos.systemDateTime';
import {deepClone,getCurrentTime,stringToASC,string2Bytes,fillZero,Array2Byte} from '../../common/utils/tools'
2024-05-16 09:53:10 +08:00
import {testMarkRules,testAllitems,testRealExam,testUIAllitems,testKmItems} from '../../pages/judgeSDK/dataTest/index'
2024-01-05 11:11:15 +08:00
import {setJudgeUdp,setTopLineUdp} from './GlobleUdp'
import {convertGpsCoord2} from '../utils/tools'
export const initJudgeUdp = async () => {
globalThis.serialIndex = 0;
globalThis.udpIndex = 0;
const udp = await setJudgeUdp()
globalThis.judgeUdp = udp
const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00];
const arrBlueBuffer= Array2Byte(arrBlue).buffer
const lightLineUdp = await setTopLineUdp();
if(lightLineUdp){
lightLineUdp.send(arrBlueBuffer);
}
globalThis.lightLineUdp = lightLineUdp
/*******************************************/
globalThis.udpClient.onMessage(async (msg) => {
const stachArr= msg.split(',')
if (stachArr[0] != '#DN_GD') {
return
}
const udpIndex = globalThis.udpIndex;
const isJudge = globalThis.isJudge;
if(udpIndex % 5 === 0 && !isJudge){
const bytes = await getMessageHeartbeat(msg);
udp.send(bytes)
}
globalThis.udpIndex+=1
})
}
export const getMessageHeartbeat = async (msg) => {
const carInfo = globalThis.carInfo;
const { examSubject,plateNo,ksyh } = carInfo;
2024-01-31 14:35:16 +08:00
const asclshArr = stringToASC(fillZero(globalThis.lsh|| 0 , 13));
const ascksyhArr = stringToASC(fillZero(globalThis.ksyh|| 0, 13))
2024-01-05 11:11:15 +08:00
const ascsbxhArr = stringToASC('0000000000')
const serialIndex = globalThis.serialIndex
const tempData = await getPlcData(msg);
const {sensor,gps} = tempData;
const {zfxd,yfxd,shtd,ygd,jgd,skd,dh1,dh2,lhq,jsc,ssc,fsc,lb,mkg,aqd,ygq,cs,fdjzs} = sensor
const {jd,wd, hxj, fyj, hbg,} = gps
const translateSignals = getTranslateSignals(
[zfxd, yfxd, shtd, ygd, jgd, skd, dh1, dh2, lhq, jsc,ssc,fsc, lb, mkg, aqd, 0,0,0,0 , 0,0, ygq, sensor.wd, 0 ]
)
const translateProject = getTranslateProject();
//@ts-ignore
const translateJd = convertGpsCoord2(wd).toFixed(7) * Math.pow(10,7)
//@ts-ignore
const translateWd = convertGpsCoord2(jd).toFixed(7) * Math.pow(10,7)
//@ts-ignore
const translateProjects= translateProject.map(numStr => string2Bytes(parseInt(numStr,2),8)[0])
const arr = [
//考生号
asclshArr.map(lsh => string2Bytes(lsh,8)[0]),
//考试员号
ascksyhArr.map(ksyh => string2Bytes(ksyh,8)[0]),
// 科目类型(0:未考试 1:科目二 2:科目三) + 考试开始时间
string2Bytes('00000000000',4 * 8),
// 消息序号
string2Bytes(serialIndex,2 * 8),
translateSignals,
//@ts-ignore
string2Bytes(cs,2*8), string2Bytes(fdjzs / 60,8), string2Bytes(translateJd,4*8), string2Bytes(translateWd,4*8), string2Bytes(1,8),
//GPS东向距离
string2Bytes(0,4*8),
//GPS北向距离
string2Bytes(0,4*8),
//航向角 俯仰角 高程(海拔)
//@ts-ignore
string2Bytes((hxj+90) * 100,2*8), string2Bytes(fyj*100,2*8), string2Bytes(hbg*100,4*8),
translateProjects,
//当前项目编号
string2Bytes(0,8),
//场地设备编号
ascsbxhArr.map(sbxh => string2Bytes(sbxh,8)[0]),
//本次考试行驶距离
string2Bytes(0,2*8),
//扣分
string2Bytes(0,2*8),
//扣分项数量
string2Bytes(0,8),
//n个扣分序号
[].map(kf => string2Bytes(kf.markcatalog,8))
]
let tempArr = [];
arr.forEach(itemArr =>{ tempArr = tempArr.concat(itemArr)})
globalThis.serialIndex = 0;
return Array2Byte(tempArr)
}
// 中心plc实时信号转换成字节
const getTranslateSignals = (tempItems) => {
const len = Math.floor(tempItems.length / 8);
const arr = [];
for(let i = 0;i < len;i++){
const temp = tempItems.slice( i*8 , (i+1)*8 );
arr.push(temp.join(''));
}
const temp = arr.map(numStr => parseInt(numStr,2))
return temp.map(item => string2Bytes(item , 8)[0])
}
// 中心所有项目转换
const getTranslateProject = () => {
2024-05-16 09:53:10 +08:00
const allKmItemsObj = {};
testKmItems.forEach(item => {
allKmItemsObj[item.code] = item;
2024-01-05 11:11:15 +08:00
})
2024-05-16 09:53:10 +08:00
const tempItems = testKmItems.map(item=>{
const current = allKmItemsObj[item.code];
2024-01-05 11:11:15 +08:00
return {
code:item.code,
status:getCenterProjectStatus(current.status)
}
})
const len = Math.floor(tempItems.length / 4);
const arr = [];
for(let i = 0;i <= len;i++){
const temp = tempItems.slice( i*4 , (i+1)*4 );
let tempArr = temp.map(item => item.status)
if(i === 4){
tempArr = tempArr.concat(['00','00'])
}
arr.push(tempArr.join(''));
}
return arr.map(numStr => parseInt(numStr,2));
}
// 中心实时项目状态转换
const getCenterProjectStatus = (status) => {
switch (status){
//不考
case 0:return '00'
//未考
case 1:return '01'
//已考
case 2:return '10'
}
}
// plc数据转换成对象
const getPlcData = async (plc:string) => {
const time = await systemTime.getCurrentTime()
const p = plc.split(',').map((val,key)=>{
if(key !== 27 && key !== 92){
//@ts-ignore
return val*1
}else{
return val
}
});
const tempData = {
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], dh1:p[5], dh2:p[6], fdjzs:p[25], dw:p[28], cs:p[23],
//@ts-ignore 方向盘
fxp:p[27].split('_')[0]*1,
//累计脉冲 溜车脉冲 超声波左后 超声波右后 超声波右前 超声波左前 座椅 仪表盘 后视镜 倒车镜 光照 雨量
ljmc:p[24], lcmc:0, csbzh:p[32], csbyh:p[30], csbyq:p[31], csbzq:p[29], zy:0, tbp:0, hsj:0, dcj:0, gx:0, yl:0
},
gps:{
//TODO 办卡类型 定位差分状态
bklx:p[56], dwzt:p[83],
//@ts-ignore 角度差分状态
jdzt:p[92].split('-')[0]*1,
//TODO gps数据
//gps时间 经度 纬度 航向角 俯仰角 海拔高 高度差 速度
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],
//@ts-ignore 角度搜星数
jdsxs:p[92].split('-')[1]*1
},
}
return tempData
}