cpp
This commit is contained in:
		
							parent
							
								
									7cabbbd29a
								
							
						
					
					
						commit
						2d82810d61
					
				| @ -1,11 +1,10 @@ | ||||
| // import apiJudgeSdk from 'libJudgeSdk.so'; | ||||
| import { setMapDrawing } from '../utils/native/NativeSDK' | ||||
| 
 | ||||
| @Component | ||||
| export default struct RealTime { | ||||
|   @State draw: boolean = false | ||||
|   @State draw: boolean = true | ||||
|   @State serialNumber: number = 0 | ||||
| 
 | ||||
| 
 | ||||
|   build() { | ||||
|     Column() { | ||||
|       if (this.draw) { | ||||
| @ -17,11 +16,10 @@ export default struct RealTime { | ||||
|           .width('100%') | ||||
|           .height('100%') | ||||
|           .onLoad(() => { | ||||
|             this.draw = true; | ||||
|             // apiJudgeSdk.examJudgeMapSetDrawing(true); //停止绘制地图轨迹,false:表示结束绘制 | ||||
|             setMapDrawing(true); | ||||
|           }) | ||||
|           .onDestroy(() => { | ||||
|             // apiJudgeSdk.examJudgeMapSetDrawing(false); //停止绘制地图轨迹,false:表示结束绘制 | ||||
|             setMapDrawing(false); | ||||
|             this.draw = false; | ||||
|           }) | ||||
|       } else { | ||||
|  | ||||
| @ -11,6 +11,7 @@ import { OutWireControl } from '../utils/wireControl/OutWireControl'; | ||||
| import { nativeLogCallback, nativeSDKInit, setBasePoint, setConfigFile, setImageSize } from '../utils/native/NativeSDK'; | ||||
| import { LogHelper } from '../utils/LogHelper'; | ||||
| import util from '@ohos.util'; | ||||
| import { CarInfo, GpsInfo, parseCar, parseGps } from '../utils/udp/Gps'; | ||||
| 
 | ||||
| 
 | ||||
| // 直角转弯 (0, 0) (0, 1)   ... | ||||
| @ -89,14 +90,16 @@ struct Index { | ||||
|         this.paths = (JSON.parse(line) as ConfigProps<Array<Array<number>>>).features | ||||
|       }) | ||||
|       await nativeSDKInit() | ||||
|       // await nativeLogCallback((message: string, len: number) => { | ||||
|       //   LogHelper.I("NativeSDK", message) | ||||
|       // }) | ||||
|       await nativeLogCallback((message: string, len: number) => { | ||||
|         LogHelper.I("NativeSDK", message) | ||||
|       }) | ||||
|       await setImageSize(500, 300) | ||||
|       await setConfigFile(this.basePoint, this.fieldModel, this.teachPoint, this.teachPointParams, this.carModel, this.line, this.lib) | ||||
|       this.outWireControl = new OutWireControl() | ||||
|       await this.outWireControl.init("192.168.7.181", 30021) | ||||
|       await this.initPlatformUDP() | ||||
|       await this.initAudioPlayer() | ||||
|       await this.initBackMachineUDP() | ||||
|     } catch (err) { | ||||
|       promptAction.showToast({ | ||||
|         message: JSON.stringify(err), | ||||
| @ -126,9 +129,9 @@ struct Index { | ||||
|     this.simulatorUdpClient = new SimulatorUdpClient("0.0.0.0", 8990, "88.22.10.118", 9001) | ||||
|     try { | ||||
|       await this.simulatorUdpClient.bindUdp() | ||||
|       this.simulatorUdpClient.heart() | ||||
|       // this.simulatorUdpClient.heart() | ||||
|       setInterval(() => { | ||||
|         this.simulatorUdpClient.uploadPosition() | ||||
|         this.simulatorUdpClient.uploadPosition(new GpsInfo(), new CarInfo()) | ||||
|       }, 1000) | ||||
|       this.simulatorUdpClient.onMessage(({type, data = [] }: MessageCallBackParams) => { | ||||
|         if (type === CommandType.Start) { | ||||
| @ -165,7 +168,10 @@ struct Index { | ||||
|     this.backupUdpClient = new BackMachineUdpClient("", 0, "", 0) | ||||
|     try { | ||||
|       await this.backupUdpClient.bindUdp() | ||||
|       this.backupUdpClient.onMessage(() => { | ||||
|       this.backupUdpClient.onMessage((arr) => { | ||||
|         let gps = parseGps(arr) | ||||
|         let car = parseCar(arr) | ||||
|         this.simulatorUdpClient?.uploadPosition(gps, car) | ||||
|       }) | ||||
|     } catch (err) { | ||||
|       throw new Error(JSON.stringify(err)) | ||||
|  | ||||
| @ -3,25 +3,19 @@ import { LogHelper } from '../LogHelper' | ||||
| 
 | ||||
| const Tag = "NativeSDK" | ||||
| 
 | ||||
| function useNativeAPI(cb: Function, ...args: Object[]) { | ||||
| // sdk初始化 | ||||
| export function nativeSDKInit() { | ||||
|   return new Promise<void>((resolve, reject) => { | ||||
|     let params = args.map(item => JSON.stringify(item)) | ||||
|     let result = cb(...params) | ||||
|     LogHelper.I(Tag, cb.name, "receive sdk result ", result) | ||||
|     const result = libJudgeSdk.autoServiceInit() | ||||
|     if (result === 0) { | ||||
|       LogHelper.I(Tag, "use native function ", cb.name, " success") | ||||
|       LogHelper.I(Tag, "use native function ", "autoServiceInit", " success") | ||||
|       resolve() | ||||
|     } else { | ||||
|       reject("use native function " + cb.name + " failed") | ||||
|       reject("use native function " + "autoServiceInit" + " failed, error code " + result) | ||||
|     } | ||||
|   }) | ||||
| } | ||||
| 
 | ||||
| // sdk初始化 | ||||
| export function nativeSDKInit(data?: Object) { | ||||
|   return useNativeAPI(libJudgeSdk.autoServiceInit, data) | ||||
| } | ||||
| 
 | ||||
| // 日志回调 | ||||
| export async function nativeLogCallback(callback: Function) { | ||||
|   return new Promise<void>((resolve, reject) => { | ||||
| @ -191,6 +185,19 @@ export function registerSoundCallback(callback: Function) { | ||||
|   }) | ||||
| } | ||||
| 
 | ||||
| // 绘图控制 | ||||
| export function setMapDrawing(drawing: boolean) { | ||||
|   return new Promise<void>((resolve, reject) => { | ||||
|     const result = libJudgeSdk.examJudgeMapSetDrawing(drawing) | ||||
|     if (result === 0) { | ||||
|       LogHelper.I(Tag, "use native function ", "examJudgeMapSetDrawing", " success") | ||||
|       resolve() | ||||
|     } else { | ||||
|       reject("use native function " + "examJudgeMapSetDrawing" + " failed, error code " + result) | ||||
|     } | ||||
|   }) | ||||
| } | ||||
| 
 | ||||
| // 设置配置文件 | ||||
| export function setConfigFile( | ||||
|   basePoint: string, | ||||
|  | ||||
| @ -1,5 +1,6 @@ | ||||
| import socket from '@ohos.net.socket'; | ||||
| import { LogHelper } from '../LogHelper'; | ||||
| import { CarInfo, GpsInfo } from '../udp/Gps'; | ||||
| import { byte2string, DataToByte } from '../Utils'; | ||||
| 
 | ||||
| const TAG = 'SimulatorUDP' | ||||
| @ -82,30 +83,30 @@ export class SimulatorUdpClient { | ||||
|     return this.bindUdp() | ||||
|   } | ||||
| 
 | ||||
|   uploadPosition() { | ||||
|   uploadPosition(gps: GpsInfo, car: CarInfo) { | ||||
|     const type = new Uint8Array([2]) | ||||
|     const student = new Uint8Array(DataToByte("0", 32)) | ||||
|     const time = new Uint8Array(DataToByte("0830102933", 32)) | ||||
|     const status = new Uint8Array([0b1111, 0b1111, 0b1111]) | ||||
|     const project = new Uint8Array(new ArrayBuffer(5)) | ||||
|     const v = new Uint8Array(DataToByte(1000, 16)) | ||||
|     const r = new Uint8Array(DataToByte(2000, 16)) | ||||
|     const lng = new Uint8Array(DataToByte(118.86541404 * Math.pow(10, 8), 6 * 8)) | ||||
|     const lat = new Uint8Array(DataToByte(31.9279647 * Math.pow(10, 8), 6 * 8)) | ||||
|     const v = new Uint8Array(DataToByte(gps.v * 100, 16)) //速度 | ||||
|     const r = new Uint8Array(DataToByte(car.r * 100, 16)) //发动机转速 | ||||
|     const lng = new Uint8Array(DataToByte(gps.lng * Math.pow(10, 8), 6 * 8)) //经度 | ||||
|     const lat = new Uint8Array(DataToByte(gps.lat * Math.pow(10, 8), 6 * 8)) //纬度 | ||||
|     const heading = new Uint8Array(DataToByte(gps.heading * 100, 16)) | ||||
|     const pit = new Uint8Array(DataToByte(gps.pitch * 100, 16)) | ||||
|     const baseLng = new Uint8Array(DataToByte(118.86538625 * Math.pow(10, 8), 6 * 8)) | ||||
|     const baseLat = new Uint8Array(DataToByte(31.92784807 * Math.pow(10, 8), 6 * 8)) | ||||
| 
 | ||||
|     const scene = new Uint8Array(new ArrayBuffer(16)) | ||||
|     const body = new Uint8Array(new ArrayBuffer(92)) | ||||
|     body.set([...type, ...student, ...time, ...status, ...project, ...v, ...r, ...lng, ...lat, ...DataToByte(1000, 16), | ||||
|       ...DataToByte(100, 16), ...DataToByte(10000, 24), ...DataToByte(230, 24), ...new Uint8Array([1]), ...baseLng, ...baseLat, | ||||
|     const body = new Uint8Array([...type, ...student, ...time, ...status, ...project, ...v, ...r, ...lng, ...lat, ...heading, | ||||
|       ...pit, ...DataToByte(10000, 24), ...DataToByte(230, 24), ...new Uint8Array([1]), ...baseLng, ...baseLat, | ||||
|       ...new Uint8Array([10]), ...new Uint8Array(([10])), ...scene]) | ||||
| 
 | ||||
|     const [head, headCheckCode] = generateHead(25, 601, 1254, 0, body.length) | ||||
|     const bodyCheckCode = generateCheckCode(body) | ||||
| 
 | ||||
|     const end = new Uint8Array(new ArrayBuffer(2)) | ||||
|     end.set([0x0D, 0x0A]) | ||||
|     const end = new Uint8Array([0x0D, 0x0A]) | ||||
|     const message = new Uint8Array([...head, headCheckCode, ...body, bodyCheckCode, ...end]) | ||||
|     this.sendMsg(message.buffer) | ||||
|   } | ||||
| @ -143,8 +144,7 @@ export class SimulatorUdpClient { | ||||
|     } | ||||
|     const [head, headCheckCode] = generateHead(26, 601, 1254, 0, body.length) | ||||
|     const bodyCheckCode = generateCheckCode(body) | ||||
|     const end = new Uint8Array(new ArrayBuffer(2)) | ||||
|     end.set([0x0D, 0x0A]) | ||||
|     const end = new Uint8Array([0x0D, 0x0A]) | ||||
|     const message = new Uint8Array([...head, headCheckCode, ...body, bodyCheckCode, ...end]) | ||||
|     this.sendMsg(message.buffer, 11111) | ||||
|   } | ||||
| @ -162,11 +162,10 @@ export class SimulatorUdpClient { | ||||
|     } | ||||
|     const [body, bodyCheckCode] = getMessage() | ||||
|     const [head, headCheckCode] = generateHead(this.lastCommand, 6001, 1254, 0, body.length) | ||||
|     const end = new Uint16Array(new ArrayBuffer(2)) | ||||
|     end.set([0x0D, 0x0A]) | ||||
|     const end = new Uint8Array([0x0D, 0x0A]) | ||||
|     let result = new Uint8Array([...head, headCheckCode, ...body, bodyCheckCode, ...end]) | ||||
|     setInterval(() => { | ||||
|       this.sendMsg(result.buffer) | ||||
|       this.sendMsg(result.buffer, 11111) | ||||
|     }, 1000) | ||||
|   } | ||||
| 
 | ||||
| @ -186,7 +185,7 @@ export class SimulatorUdpClient { | ||||
|       } | ||||
|     }) | ||||
|       .then(() => { | ||||
|         LogHelper.I(TAG, `[${this.oppositeIp}:${this.oppositeIpPort}]: send success`) | ||||
|         LogHelper.I(TAG, `[${this.oppositeIp}:${port ?? this.oppositeIpPort}]: send success`) | ||||
|       }) | ||||
|       .catch(err => { | ||||
|         LogHelper.E(TAG, JSON.stringify(err)) | ||||
|  | ||||
							
								
								
									
										124
									
								
								entry/src/main/ets/utils/udp/Gps.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										124
									
								
								entry/src/main/ets/utils/udp/Gps.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,124 @@ | ||||
| export class GpsInfo { | ||||
|   // 状态
 | ||||
|   public status: number = 0 | ||||
|   // 收星数
 | ||||
|   // public
 | ||||
|   // 海拔高
 | ||||
|   public altitude: number = 0 | ||||
|   // 高度差
 | ||||
|   public heightDiff: number = 0 | ||||
|   // 龄期
 | ||||
|   public age: number = 0 | ||||
|   // 维度因子
 | ||||
|   public lngFactor: number = 0 | ||||
|   // 经度因子
 | ||||
|   public latFactor: number = 0 | ||||
|   // 航向角
 | ||||
|   public heading: number = 0 | ||||
|   // 俯仰角
 | ||||
|   public pitch: number = 0 | ||||
|   // 航向角状态-收星数
 | ||||
|   public headingStatus: number = 0 | ||||
|   // 年月日
 | ||||
|   public date: Date = new Date() | ||||
|   // 时分秒
 | ||||
|   // 经度
 | ||||
|   public lng: number = 0 | ||||
|   // 纬度
 | ||||
|   public lat: number = 0 | ||||
|   // 速度
 | ||||
|   public v: number = 0 | ||||
| 
 | ||||
|   constructor(gpsArr?: string[]) { | ||||
|     if (gpsArr) { | ||||
|       this.status = Number(gpsArr[0]) | ||||
|       this.altitude = Number(gpsArr[2]) | ||||
|       this.heightDiff = Number(gpsArr[3]) | ||||
|       this.age = Number(gpsArr[4]) | ||||
|       this.lngFactor = Number(gpsArr[5]) | ||||
|       this.latFactor = Number(gpsArr[6]) | ||||
|       this.heading = Number(gpsArr[7]) | ||||
|       this.pitch = Number(gpsArr[8]) | ||||
|       this.headingStatus = Number(gpsArr[9]) | ||||
|       this.date = this.parseDate(gpsArr[10], gpsArr[11]) | ||||
|       this.lng = Number(gpsArr[12]) | ||||
|       this.lat = Number(gpsArr[13]) | ||||
|       this.v = Number(gpsArr[14]) | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   private parseDate(str1: string, str2: string) { | ||||
|     let year = new Date().getFullYear() | ||||
|     let month = Number(str1.slice(2, 4)) - 1 | ||||
|     let date = Number(str1.slice(4)) | ||||
|     let hour = Number(str2.slice(0, 2)) | ||||
|     let minute = Number(str2.slice(2, 4)) | ||||
|     let second = Number(str2.slice(4, 6)) | ||||
|     return new Date(year, month, date, hour, minute, second) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export class CarInfo { | ||||
|   // 左方向灯
 | ||||
|   // 右方向灯
 | ||||
|   // 喇叭
 | ||||
|   // 点火1
 | ||||
|   // 点火2
 | ||||
|   // 近光灯
 | ||||
|   // 远光灯
 | ||||
|   // 示廓灯
 | ||||
|   // 雾灯
 | ||||
|   // 雨刮器
 | ||||
|   // 脚刹
 | ||||
|   // 手刹
 | ||||
|   // 主驾驶门
 | ||||
|   // NC
 | ||||
|   // SA15
 | ||||
|   // 离合
 | ||||
|   // 副刹车
 | ||||
|   // 安全带
 | ||||
|   // 双跳灯
 | ||||
|   // 其他门
 | ||||
|   // 转速过高
 | ||||
|   // 车速
 | ||||
|   // 累计脉冲
 | ||||
|   // 发动机转速
 | ||||
|   public r: number = 0 | ||||
| 
 | ||||
|   // 熄火次数
 | ||||
|   // 方向盘角度
 | ||||
|   // 档位
 | ||||
|   // 超声波1
 | ||||
|   // 超声波2
 | ||||
|   // 超声波3
 | ||||
|   // 超声波4
 | ||||
|   // 触摸1
 | ||||
|   // 触摸2
 | ||||
|   // 发送次数
 | ||||
|   // 方向盘类型
 | ||||
|   // 汽车类型
 | ||||
|   // 接口心跳
 | ||||
|   // 本机IP
 | ||||
|   // 固件版本
 | ||||
|   // 按键数值
 | ||||
|   // GPS板卡类型
 | ||||
|   // GPS板卡软件版本
 | ||||
|   // 改正数次数/改正数大小
 | ||||
|   // GPS数据次数/数据长度
 | ||||
|   // GPS错误次数
 | ||||
|   // 已工作时长/设定的工作时长
 | ||||
| 
 | ||||
|   constructor(carArr?: string[]) { | ||||
|     if (carArr) { | ||||
|       this.r = Number(carArr[23]) | ||||
|     } | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export function parseGps(backupArr: string[]) { | ||||
|   return new GpsInfo(backupArr.slice(83)) | ||||
| } | ||||
| 
 | ||||
| export function parseCar(backArr: string[]) { | ||||
|   return new CarInfo(backArr.slice(2)) | ||||
| } | ||||
| @ -7,6 +7,8 @@ import Store from '../Store'; | ||||
| 
 | ||||
| const TAG = 'BackMachineUDP' | ||||
| 
 | ||||
| export type MessageCallback = (msg: string[]) => void | ||||
| 
 | ||||
| export class BackMachineUdpClient { | ||||
|   private localIp: string = '' | ||||
|   private localIpPort: number = 0 | ||||
| @ -60,7 +62,7 @@ export class BackMachineUdpClient { | ||||
|       }) | ||||
|   } | ||||
| 
 | ||||
|   onError(callback?) { | ||||
|   onError(callback?: MessageCallback) { | ||||
|     this.udp.on('error', async err => { | ||||
|       this.bindUdp() | ||||
|       this.sendMsg('111') | ||||
| @ -68,7 +70,7 @@ export class BackMachineUdpClient { | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   onMessage(callback?) { | ||||
|   onMessage(callback?: MessageCallback) { | ||||
|     this.udp.on('message', value => { | ||||
|       this.plcUdpError = false | ||||
|       if (value) { | ||||
| @ -85,10 +87,9 @@ export class BackMachineUdpClient { | ||||
|           return | ||||
|         } | ||||
|         receiveArr[28] = Store.serialPortMsg || '0' | ||||
|         const newArr = JSON.parse(JSON.stringify(receiveArr)) | ||||
|         callback?.(newArr.toString()) | ||||
|         callback?.(receiveArr) | ||||
|       } else { | ||||
|         callback?.('') | ||||
|         callback?.([]) | ||||
|       } | ||||
|     }); | ||||
|     setInterval(async () => { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user