259 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			259 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import systemTime from '@ohos.systemDateTime';
 | |
| import { Array2Byte, fillZero, string2Bytes, stringToASC } from '../../common/utils/tools';
 | |
| import {  testKm2Items,testKm3Items } from '../../pages/judgeSDK/dataTest/index';
 | |
| import { judgeConfig } from '../../pages/judgeSDK/utils/judgeConfig';
 | |
| import { setJudgeUdp, setTopLineUdp } from './GlobalUdp';
 | |
| 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_1(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 } = carInfo;
 | |
|   const ksyh='0000000000000'
 | |
|   const {fourInOneScreen:{gpsDigit}} = judgeConfig
 | |
|   const asclshArr = stringToASC(fillZero(
 | |
|     globalThis.singlePlay
 | |
|       ?'1111111111111'
 | |
|       : globalThis.lsh,
 | |
|     13));
 | |
|   const ascksyhArr = stringToASC(fillZero('1111111111111', 13))
 | |
|   const ascsbxhArr = stringToASC('00000000')
 | |
|   // const ascsbxhArr = stringToASC('153216400880')
 | |
|   const serialIndex = globalThis.serialIndex
 | |
|   const tempData = await getPlcData(msg);
 | |
|   const examType = examSubject == 2?2:3
 | |
|   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(gpsDigit) * Math.pow(10, gpsDigit);
 | |
|   //@ts-ignore
 | |
|   const translateWd = convertGpsCoord2(jd).toFixed(gpsDigit) * Math.pow(10, gpsDigit)
 | |
|   //@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(`${0}${'00:00:00'}`, 4 * 8),
 | |
|     // 消息序号
 | |
|     string2Bytes(serialIndex, 2 * 8),
 | |
|     translateSignals,
 | |
|     //@ts-ignore
 | |
|     string2Bytes(Math.floor(gps.sd*1.852*100) , 2 * 8), string2Bytes(fdjzs / 100, 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) * 100, 2 * 8), string2Bytes(fyj * 100, 2 * 8), string2Bytes(hbg * 100, 4 * 8),
 | |
|     //dddd
 | |
|     translateProjects,
 | |
|     //当前项目编号
 | |
|     string2Bytes(0, 8),
 | |
|     //场地设备编号
 | |
|     ascsbxhArr.map(sbxh => string2Bytes(sbxh, 8)[0]),
 | |
|     //本次考试行驶距离
 | |
|     string2Bytes(0, 2 * 8),
 | |
|     //扣分值
 | |
|     string2Bytes(0, 2 * 8),
 | |
|     //扣分数
 | |
|     string2Bytes(0, 2 * 8),
 | |
|     //扣分项数量
 | |
|     string2Bytes(0, 8),
 | |
|     //n个扣分序号
 | |
|     [].map(kf => string2Bytes(kf.markcatalog, 8)),
 | |
|     //牵引车第二gps精度、纬度
 | |
|     string2Bytes(0, 4 * 8), string2Bytes(0, 4 * 8),
 | |
|     //牵引车第二航向角
 | |
|     string2Bytes(0, 2 * 8),
 | |
|     //摩托压线 Byte[20],
 | |
|     string2Bytes(0, 20 * 8),
 | |
|     //考试用时
 | |
|     string2Bytes(fillZero(0+'',4), 4 * 8),
 | |
|     //项目用时
 | |
|     string2Bytes(fillZero(0,2), 2 * 8),
 | |
|     //设备信号状态
 | |
|     string2Bytes(0, 4 * 8),
 | |
|   ]
 | |
|   let tempArr = [];
 | |
|   arr.forEach(itemArr => {
 | |
|     tempArr = tempArr.concat(itemArr)
 | |
|   })
 | |
|   console.log('kmkmkm',tempArr.length)
 | |
| 
 | |
|   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 = () => {
 | |
|   const examSubject =globalThis.examSubject;
 | |
| 
 | |
|   const tempItems = (examSubject == 2 ? testKm2Items:testKm3Items).map(code => {
 | |
|     return {
 | |
|       code, status:'0'
 | |
|     }
 | |
|   })
 | |
|   const arr = [];
 | |
|   for (let i = 0; i <= 4; i++) {
 | |
|     const temp = tempItems.slice(i * 4, (i + 1) * 4);
 | |
|     let tempArr = temp.map(item => item.status)
 | |
|     if (i === 4) {
 | |
|       tempArr = examSubject == 2
 | |
|         //bit36-bit39保留
 | |
|         ? tempArr.concat(['00', '00'])
 | |
|         //bit30-bit39保留
 | |
|         : tempArr.concat(['00', '00','00'])
 | |
|     }
 | |
|     // if (i === 3 && examSubject == 3) {
 | |
|     //   tempArr = tempArr.concat(['00'])
 | |
|     // }
 | |
| 
 | |
|     arr.push(tempArr.join(''));
 | |
|   }
 | |
|   return arr
 | |
| }
 | |
| 
 | |
| // 中心实时项目状态转换
 | |
| 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
 | |
| } |