feat:新科目三&安全员代码
This commit is contained in:
		
							parent
							
								
									4fa6ad0544
								
							
						
					
					
						commit
						50c16cb56a
					
				| @ -1,14 +1,12 @@ | |||||||
|  | import emitter from '@ohos.events.emitter'; | ||||||
| import UdpClient from './udpClient/index' | import UdpClient from './udpClient/index' | ||||||
| import FileUtil from '../../common/utils/File'; | import FileUtil from '../../common/utils/File'; | ||||||
| import { GlobalConfig } from '../../config/index'; | import { GlobalConfig } from '../../config/index'; | ||||||
| import emitter from '@ohos.events.emitter'; | import {messageIds} from './type' | ||||||
| import RadarClient from './radar/index' | 
 | ||||||
| interface messageIds { |  | ||||||
|   [k:string]:number; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| class Lidar { | class Lidar { | ||||||
|   public localRadarUdp: RadarClient |   public localRadarUdp: UdpClient | ||||||
|   public localVisionUdp: UdpClient |   public localVisionUdp: UdpClient | ||||||
|   //是否新科目三设备
 |   //是否新科目三设备
 | ||||||
|   private isNewKm3: Boolean |   private isNewKm3: Boolean | ||||||
| @ -21,6 +19,49 @@ class Lidar { | |||||||
|     this.init() |     this.init() | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   async init() { | ||||||
|  |     const fileUtil = new FileUtil(globalThis.context) | ||||||
|  |     const addressStr = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | ||||||
|  |     const {deviceType,deviceIpArr,devicePortArr,udplocalIp} = JSON.parse(addressStr); | ||||||
|  | 
 | ||||||
|  |     //新科目三设备
 | ||||||
|  |     if (deviceType == '2') { | ||||||
|  |       //雷达UDP
 | ||||||
|  |       this.localRadarUdp = new UdpClient('radar', { | ||||||
|  |         deviceIpArr, devicePortArr, udplocalIp | ||||||
|  |       }) | ||||||
|  |       this.localRadarUdp.onClientMessage(async (obj) => { | ||||||
|  |         //广播注册的radar消息
 | ||||||
|  |         Reflect.ownKeys(this.radarMessageIds).forEach((messageId:string) => { | ||||||
|  |           emitter.emit({eventId:this.radarMessageIds[messageId]},{data:obj}) | ||||||
|  |         }) | ||||||
|  |       }) | ||||||
|  |       // this.localRadarUdp.onMessage((obj) => {
 | ||||||
|  |       //   //广播注册的radar消息
 | ||||||
|  |       //   Reflect.ownKeys(this.radarMessageIds).forEach((messageId:string) => {
 | ||||||
|  |       //     emitter.emit({eventId:this.radarMessageIds[messageId]},{data:obj})
 | ||||||
|  |       //   })
 | ||||||
|  |       // })
 | ||||||
|  |       //
 | ||||||
|  |       // this.localVisionUdp.onMessage((obj) => {
 | ||||||
|  |       //   //广播注册的sense消息
 | ||||||
|  |       //   Reflect.ownKeys(this.senseMessageIds).forEach((messageId:string) => {
 | ||||||
|  |       //     emitter.emit({eventId:this.radarMessageIds[messageId]},{data:obj})
 | ||||||
|  |       //   })
 | ||||||
|  |       // })
 | ||||||
|  |       this.isNewKm3 = true; | ||||||
|  |     } | ||||||
|  |     //安全员&新科目三均需要视觉
 | ||||||
|  |     //TODO 视觉UDP
 | ||||||
|  |     this.localVisionUdp = new UdpClient('vision', { | ||||||
|  |       deviceIpArr, devicePortArr, udplocalIp | ||||||
|  |     }) | ||||||
|  |     this.localVisionUdp.onClientMessage(async (obj) => { | ||||||
|  | 
 | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   //监听雷达信息
 |   //监听雷达信息
 | ||||||
|   async onRadarMsg(messageId:string,callback:Function) { |   async onRadarMsg(messageId:string,callback:Function) { | ||||||
|     if(this.radarMessageIds[messageId] === undefined){ |     if(this.radarMessageIds[messageId] === undefined){ | ||||||
| @ -47,42 +88,6 @@ class Lidar { | |||||||
|     delete [type === 'radar'?this.radarMessageIds:this.senseMessageIds][messageId] |     delete [type === 'radar'?this.radarMessageIds:this.senseMessageIds][messageId] | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async init() { |  | ||||||
|     const fileUtil = new FileUtil(globalThis.context) |  | ||||||
|     const addressStr = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); |  | ||||||
|     const {deviceType,deviceIpArr,devicePortArr,udplocalIp} = JSON.parse(addressStr); |  | ||||||
| 
 |  | ||||||
|     //新科目三设备
 |  | ||||||
|     if (deviceType == '2') { |  | ||||||
|       //雷达UDP
 |  | ||||||
|       this.localRadarUdp = new RadarClient({ |  | ||||||
|         deviceIpArr, devicePortArr, udplocalIp |  | ||||||
|       }); |  | ||||||
| 
 |  | ||||||
|       //视觉UDP
 |  | ||||||
|       this.localVisionUdp = new UdpClient('vision', { |  | ||||||
|         deviceIpArr, devicePortArr, udplocalIp |  | ||||||
|       }) |  | ||||||
| 
 |  | ||||||
|       this.localRadarUdp.onMessage((obj) => { |  | ||||||
|         //广播注册的radar消息
 |  | ||||||
|         Reflect.ownKeys(this.radarMessageIds).forEach((messageId:string) => { |  | ||||||
|           emitter.emit({eventId:this.radarMessageIds[messageId]},{data:obj}) |  | ||||||
|         }) |  | ||||||
|       }) |  | ||||||
| 
 |  | ||||||
|       this.localVisionUdp.onMessage((obj) => { |  | ||||||
|         //广播注册的sense消息
 |  | ||||||
|         Reflect.ownKeys(this.senseMessageIds).forEach((messageId:string) => { |  | ||||||
|           emitter.emit({eventId:this.radarMessageIds[messageId]},{data:obj}) |  | ||||||
|         }) |  | ||||||
|       }) |  | ||||||
|       this.isNewKm3 = true; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     //TODO 安全员设备
 |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   //随机生成6位的id
 |   //随机生成6位的id
 | ||||||
|   generateRandomNumber(){ |   generateRandomNumber(){ | ||||||
|     let num = ''; |     let num = ''; | ||||||
|  | |||||||
| @ -42,9 +42,7 @@ export default class LidaUdpClient{ | |||||||
|     //bind
 |     //bind
 | ||||||
|     await this.safeFn(async()=>{ |     await this.safeFn(async()=>{ | ||||||
|       await this.udpClient.bind({ |       await this.udpClient.bind({ | ||||||
|         address: this.localIp, |         address: this.localIp, port: port, family: 1 | ||||||
|         port: port, |  | ||||||
|         family: 1 |  | ||||||
|       }); |       }); | ||||||
|     }) |     }) | ||||||
|   } |   } | ||||||
| @ -52,14 +50,18 @@ export default class LidaUdpClient{ | |||||||
|   async onClientMessage(callback){ |   async onClientMessage(callback){ | ||||||
|     this.offMessage() |     this.offMessage() | ||||||
|     //message
 |     //message
 | ||||||
|     this.udpClient.on('message',(str)=>{ |     this.udpClient.on('message',(buffer)=>{ | ||||||
|  |       const str =  this.arrayBuffetToStr(buffer.message); | ||||||
|  |       //解析数据
 | ||||||
|       switch (this.type){ |       switch (this.type){ | ||||||
|       //激光雷达
 |         //激光雷达
 | ||||||
|         case 'radar': |         case 'radar': | ||||||
|           callback(str) |           const radarObj = this.radarStrToObj(str) | ||||||
|  |           callback(radarObj) | ||||||
|           break; |           break; | ||||||
|       //视觉
 |         //TODO 视觉
 | ||||||
|         case 'vision': |         case 'vision': | ||||||
|  |           const visionObj = this.visionStrToObj(str) | ||||||
|           callback(str) |           callback(str) | ||||||
|           break; |           break; | ||||||
|       } |       } | ||||||
| @ -67,6 +69,81 @@ export default class LidaUdpClient{ | |||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   // 雷达数据解析
 | ||||||
|  |   async radarStrToObj(str:string){ | ||||||
|  |     //$MachineVision,30.950^0_-1_1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1^-1,43083,0_0_0_0,-1^-1_-1_-1_1,-1^-1_-1_-1_1,-1^-1_-1_-1_1,10102264201.69632,1.1.1.2,0*+$VisualInfo,1_-7205_3480_-7300_3350_-7655_3610_-7560_3735_,*+[ { "bodyangle": "1.00", "bodyx": "0.490", "bodyy": "31.300", "elevation": "2.403", "geoangle": "323.90", "geolat": "29.92678975", "geolon": "119.89823128", "id": 1, "type": 2 } ]
 | ||||||
|  |     const [machine, visual, obj] = str.split('+'); | ||||||
|  |     const machineArr = machine.split(','); | ||||||
|  |     let tempObj:Machine = {}; | ||||||
|  |     tempObj.sj = await systemTime.getCurrentTime(); | ||||||
|  |     tempObj.xh = Number(machineArr[10]); | ||||||
|  |     ['qf', 'hf', 'zq', 'zh', 'yq', 'yh', 'zc', 'yc', 'zf'].forEach((c,index) => { | ||||||
|  |       tempObj[c] = this.getMachineItem(machineArr[index + 1]); | ||||||
|  |     }); | ||||||
|  |     ['lq', 'lz', 'ly'].forEach((c,index) => { | ||||||
|  |       tempObj[c] = this.getMachineItem(machineArr[index + 13]); | ||||||
|  |     }); | ||||||
|  |     tempObj.wt = this.getMachineWt(machineArr[10]) | ||||||
|  |     tempObj.rc = this.getMachineRc(machineArr[12]) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 视觉数据解析
 | ||||||
|  |   async visionStrToObj(str:string){ | ||||||
|  |     //头部姿态 驾驶行为 前方障碍物
 | ||||||
|  | 
 | ||||||
|  |     //$SBXS,85,20250603115529563,B202,赣A78QK6@360803200602253517@1@20250603115523132@@@@@李从星@001@@001@2506455453517,05,*SBJS
 | ||||||
|  |     //$SBXS,213,20250603090553562,B207,邓凯倩@@320250603085259@000000100000001001001000@14@870@28520185@115902060@1@828@4294943473@359.8704@0@0@1111111111011111111111111111111100000000@0@0000000000@3142@0@0@@C1@赣A78QK6@0@001-001@360121200401040528,01,*SBJS
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   getMachineItem(str){ | ||||||
|  |     const strArr = str.split('_'); | ||||||
|  |     return { | ||||||
|  |       //对象编号
 | ||||||
|  |       bh:strArr[2], | ||||||
|  |       //车类型
 | ||||||
|  |       lx:strArr[3], | ||||||
|  |       //纵向距离
 | ||||||
|  |       jz:strArr[0].split('^')[0], | ||||||
|  |       //横向距离
 | ||||||
|  |       jh:strArr[0].split('^')[1], | ||||||
|  |       //速度
 | ||||||
|  |       sd:strArr[1] | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   //获取前方数据
 | ||||||
|  |   getMachineWt(str){ | ||||||
|  |     return { | ||||||
|  |       xz:str.split('_')[0].split('^')[0], | ||||||
|  |       xh:str.split('_')[0].split('^')[1], | ||||||
|  |       zz:str.split('_')[1].split('^')[0], | ||||||
|  |       zh:str.split('_')[1].split('^')[1], | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   //获取绕车一周数据
 | ||||||
|  |   getMachineRc(str){ | ||||||
|  |     return { | ||||||
|  |       zh:str.split('_')[0], | ||||||
|  |       yh:str.split('_')[1], | ||||||
|  |       yq:str.split('_')[2], | ||||||
|  |       zq:str.split('_')[3] | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   //arrayBuffer转string
 | ||||||
|  |   arrayBuffetToStr(arrayBuffer){ | ||||||
|  |     let dataView = new DataView(arrayBuffer) | ||||||
|  |     let str = "" | ||||||
|  |     for (let i = 0; i < dataView?.byteLength; ++i) { | ||||||
|  |       let c = String.fromCharCode(dataView?.getUint8(i)) | ||||||
|  |       if (c !== "\n") { | ||||||
|  |         str += c | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return str | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   //获取对应的IP和端口
 |   //获取对应的IP和端口
 | ||||||
|   async getIpAPort(): Promise<IPAPORT>{ |   async getIpAPort(): Promise<IPAPORT>{ | ||||||
|     const fileUtil = new FileUtil(globalThis.context) |     const fileUtil = new FileUtil(globalThis.context) | ||||||
| @ -79,6 +156,21 @@ export default class LidaUdpClient{ | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   // 初始化指令
 | ||||||
|  |   async visionInit(){ | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  |   // 开始考试指令
 | ||||||
|  |   async visionExamStart(){} | ||||||
|  |   // 结束考试指令
 | ||||||
|  |   async visionExamEnd(){} | ||||||
|  |   // 安全员开始录像
 | ||||||
|  |   async visionStartRecord(){} | ||||||
|  |   // 安全员结束录像
 | ||||||
|  |   async visionEndRecord(){} | ||||||
|  |   // 安全员信号传输
 | ||||||
|  |   async visionSignalTransfer(){} | ||||||
|  | 
 | ||||||
|   async offMessage(){ |   async offMessage(){ | ||||||
|     this.udpClient.off('message') |     this.udpClient.off('message') | ||||||
|   } |   } | ||||||
| @ -100,4 +192,5 @@ export default class LidaUdpClient{ | |||||||
|       throw e |       throw e | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user