错误修改
This commit is contained in:
		
							parent
							
								
									df6ccfa689
								
							
						
					
					
						commit
						ab9d50f47e
					
				| @ -5,12 +5,10 @@ import { GlobalConfig } from '../config/global'; | ||||
| import Want from '@ohos.app.ability.Want'; | ||||
| import AbilityConstant from '@ohos.app.ability.AbilityConstant'; | ||||
| import { BaseInfoType, CarInfoType, EnvironmentConfigurationType, ExaminerInfoType } from '../model'; | ||||
| import { tcpUtil } from '../utils/TcpRequest'; | ||||
| import DB from '../utils/DbSql'; | ||||
| import { DrivingDataStorage } from '../utils/business/DrivingDataStorage'; | ||||
| import { InitTable } from '../utils/table/Operation'; | ||||
| import FileUtils from '../utils/FileUtils'; | ||||
| import { lightUDPClient } from '../utils/UdpUtils'; | ||||
| 
 | ||||
| export default class EntryAbility extends UIAbility { | ||||
|   async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { | ||||
| @ -34,7 +32,7 @@ export default class EntryAbility extends UIAbility { | ||||
|   onDestroy() { | ||||
|     const arrClose = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00] | ||||
|     // TODO UDP缺失 | ||||
|     lightUDPClient.sendMsg(new Uint8Array(arrClose).buffer) | ||||
|     // lightUDPClient.sendMsg(new Uint8Array(arrClose).buffer) | ||||
|     DrivingDataStorage.close() | ||||
|     hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); | ||||
|   } | ||||
| @ -42,7 +40,6 @@ export default class EntryAbility extends UIAbility { | ||||
|   async onWindowStageCreate(windowStage: window.WindowStage) { | ||||
|     // Main window is created, set main page for this ability | ||||
|     hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); | ||||
|     await tcpUtil.init() | ||||
| 
 | ||||
|     AppStorage.setOrCreate<CarInfoType>('carInfo', {}) | ||||
|     AppStorage.setOrCreate<ExaminerInfoType>('examinerInfo', {}) | ||||
|  | ||||
| @ -7,7 +7,7 @@ import { BusinessError } from '@ohos.base' | ||||
| 
 | ||||
| 
 | ||||
| export default class FileUtils { | ||||
|   public destFile: string | ||||
|   public destFile: string = '' | ||||
|   public filePathFdObj: Record<string, fs.File> = {} | ||||
|   private context: common.UIAbilityContext | ||||
|   private absolutePath = '/mnt/hmdfs/100/account/device_view/local/files/duolun' | ||||
| @ -59,6 +59,7 @@ export default class FileUtils { | ||||
|       return file.fd | ||||
|     } catch (e) { | ||||
|       this.handleError('editFile文件失败', e, filePath) | ||||
|       return undefined | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -35,6 +35,10 @@ function xmlToJSON(target: string): object { | ||||
|   return transfer(result['_elements']) | ||||
| } | ||||
| 
 | ||||
| function setObj(target: Record<string, object>, key: string, value: object) { | ||||
|   target[key] = value | ||||
| } | ||||
| 
 | ||||
| function transfer(target: Array<object>, name?: string): object { | ||||
|   const result: object = new Object() | ||||
| 
 | ||||
| @ -44,7 +48,7 @@ function transfer(target: Array<object>, name?: string): object { | ||||
|       if (_elements === undefined) { | ||||
|         return | ||||
|       } | ||||
|       const jsonObj: object = result[el['_name']] | ||||
|       const jsonObj: Record<string, object> = result[el['_name']] as Record<string, object> | ||||
|       const handleCommonArray = (obj: object) => { | ||||
|         if (Array.isArray(jsonObj)) { | ||||
|           result[el['_name']].push(obj) | ||||
| @ -57,16 +61,18 @@ function transfer(target: Array<object>, name?: string): object { | ||||
|         if (jsonObj) { | ||||
|           handleCommonArray(_elements[0]['_text']) | ||||
|         } else { | ||||
|           jsonObj[el['_name']] = _elements[0]["_text"] | ||||
|           setObj(jsonObj, el['_name'], _elements[0]["_text"]) | ||||
|           // jsonObj[el['_name'] as string] = _elements[0]["_text"] as object | ||||
|         } | ||||
|       } else { | ||||
|         if (jsonObj) { | ||||
|           handleCommonArray(transfer(el['_elements'], el['_name'])) | ||||
|         } else { | ||||
|           jsonObj[el['_name']] = transfer(el['_elements'], el['_name']) | ||||
|           setObj(jsonObj, el['_name'], transfer(el['_elements'], el['_name'])) | ||||
|           // jsonObj[el['_name'] as string] = transfer(el['_elements'], el['_name']) | ||||
|         } | ||||
|       } | ||||
|     } else if (el['_attributes']) { | ||||
|     } else if (el['_attributes'] && name) { | ||||
|       result[name] = { | ||||
|         value: el['_text'], | ||||
|         attributes: el['__attributes'] | ||||
| @ -82,7 +88,7 @@ interface CenterCodeResult { | ||||
|   keystr?: string | ||||
| } | ||||
| 
 | ||||
| function dealCenterCode(message: string, isNewCenter: boolean): CenterCodeResult { | ||||
| function dealCenterCode(message: string, isNewCenter: boolean = false): CenterCodeResult | undefined { | ||||
|   if (isNewCenter) { | ||||
|     const msg: object = JSON.parse(message); | ||||
|     const result: object = msg?.['data'][0]?.result; | ||||
| @ -107,9 +113,10 @@ function dealCenterCode(message: string, isNewCenter: boolean): CenterCodeResult | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|   return undefined | ||||
| } | ||||
| 
 | ||||
| export default function Request<T = object>(options: RequestOption): Promise<T> { | ||||
| export default function Request<T extends Object>(options: RequestOption): Promise<T | CenterCodeResult> { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     const instance = http.createHttp() | ||||
|     const baseURL = options.host || base | ||||
| @ -123,13 +130,13 @@ export default function Request<T = object>(options: RequestOption): Promise<T> | ||||
|       readTimeout: options.timeout || 30 * 1000 | ||||
|     }).then(async data => { | ||||
|       let result = options.xml ? xmlToJSON(data.result as string) : data.result | ||||
|       let resObj: object | ||||
|       let resObj: object = new Object() | ||||
|       if (typeof result === 'string') { | ||||
|         result = JSON.parse(result) | ||||
|       } | ||||
|       if (result['Envelope']) { | ||||
|       if (result['Envelope'] !== undefined) { | ||||
|         const msgXml: string = result['Envelope']['Body']['writeObjectOutResponse']['return']; | ||||
|         resolve(dealCenterCode(msgXml, options.isNewCenter) as T) | ||||
|         resolve(dealCenterCode(msgXml, options.isNewCenter)) | ||||
|         return | ||||
|       } | ||||
|       if (!options.xml) { | ||||
|  | ||||
| @ -4,6 +4,7 @@ import { CarInfoType, EnvironmentConfigurationType } from '../model' | ||||
| import { UDPTag } from '../config' | ||||
| import { BusinessError } from '@ohos.base' | ||||
| import { FillZero, StringToBytes, StringToASCII } from './Common' | ||||
| import { CenterUDPClientInstance } from './business/CenterUdpBusiness' | ||||
| 
 | ||||
| interface MsgExt { | ||||
|   id: number, | ||||
| @ -36,14 +37,14 @@ export default class UdpClient { | ||||
|   private localIpPort: string = '' | ||||
|   private oppositeIp: string = '' | ||||
|   private oppositeIpPort: string = '' | ||||
|   protected udp: socket.UDPSocket = null | ||||
|   protected udp: socket.UDPSocket | null = null | ||||
|   private messageEvents: Array<Function> = [] | ||||
|   private errorEvents: Array<Function> = [] | ||||
|   private dealMethod: DealMethod<object> | ||||
|   private dealMethod?: DealMethod<object> | ||||
| 
 | ||||
|   private bindEvent() { | ||||
|     this.udp?.on("message", value => { | ||||
|       let result = this.dealMethod(value.message) | ||||
|       let result = this.dealMethod?.(value.message) | ||||
|       this.messageEvents.forEach(cb => { | ||||
|         cb(result) | ||||
|       }) | ||||
| @ -73,7 +74,7 @@ export default class UdpClient { | ||||
|   close(): Promise<void> { | ||||
|     this.messageEvents = [] | ||||
|     this.errorEvents = [] | ||||
|     this.dealMethod = null | ||||
|     this.dealMethod = undefined | ||||
|     this.udp?.off("message") | ||||
|     this.udp?.off("error") | ||||
|     return this.udp?.close() | ||||
| @ -134,179 +135,3 @@ export default class UdpClient { | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| // 获取后置机信号 | ||||
| class ObjUdpClient extends UdpClient { | ||||
|   private static instance: ObjUdpClient | ||||
| 
 | ||||
|   constructor() { | ||||
|     super() | ||||
|     if (!ObjUdpClient.instance) { | ||||
|       ObjUdpClient.instance = this | ||||
|     } | ||||
|     return ObjUdpClient.instance | ||||
|   } | ||||
| 
 | ||||
|   async init(): Promise<void> { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       try { | ||||
|         let result: EnvironmentConfigurationType = | ||||
|           AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||
|         if (result) { | ||||
|           this.create(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | ||||
|             .then(resolve) | ||||
|             .catch(reject) | ||||
|         } | ||||
|       } catch (e) { | ||||
|         promptAction.showToast({ | ||||
|           message: "初始化obj udp失败" | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
| 
 | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| // 中心 | ||||
| class CenterUDPClient extends UdpClient { | ||||
|   private static instance: CenterUDPClient | ||||
| 
 | ||||
|   constructor() { | ||||
|     super() | ||||
|     if (!CenterUDPClient.instance) { | ||||
|       CenterUDPClient.instance = this | ||||
|     } | ||||
|     return CenterUDPClient.instance | ||||
|   } | ||||
| 
 | ||||
|   async init(): Promise<void> { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       try { | ||||
|         let result: EnvironmentConfigurationType = | ||||
|           AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||
|         const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo') | ||||
|         if (result) { | ||||
|           this.create(result.udplocalIp, '8800', carInfo?.udpAddress, carInfo?.messagePort).then(resolve).catch(reject) | ||||
|         } | ||||
|       } catch (e) { | ||||
|         reject(e) | ||||
|         promptAction.showToast({ | ||||
|           message: "初始化中心 udp失败" | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| // 顶灯 | ||||
| class LightUDPClient extends UdpClient { | ||||
|   private static instance: LightUDPClient | ||||
| 
 | ||||
|   constructor() { | ||||
|     super() | ||||
|     if (!LightUDPClient.instance) { | ||||
|       LightUDPClient.instance = this | ||||
|     } | ||||
|     return LightUDPClient.instance | ||||
|   } | ||||
| 
 | ||||
|   async init(): Promise<void> { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       try { | ||||
|         let result: EnvironmentConfigurationType = | ||||
|           AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||
|         if (result) { | ||||
|           this.create(result.udplocalIp, '55509', result.udpOppositeIp, result.udpOppositeIpPort) | ||||
|             .then(resolve) | ||||
|             .catch(reject) | ||||
|         } | ||||
|       } catch (e) { | ||||
|         reject(e) | ||||
|         promptAction.showToast({ | ||||
|           message: "初始化灯光 udp失败" | ||||
|         }) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| // 评判udp | ||||
| class JudgeUdpClient extends UdpClient { | ||||
|   private udpIndex = 0; | ||||
|   private currentUdpIndex = 0; | ||||
|   private static instance: JudgeUdpClient | ||||
| 
 | ||||
|   constructor() { | ||||
|     super() | ||||
|     if (!JudgeUdpClient.instance) { | ||||
|       JudgeUdpClient.instance = this | ||||
|     } | ||||
|     return JudgeUdpClient.instance | ||||
|   } | ||||
| 
 | ||||
|   async init(): Promise<void> { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       try { | ||||
|         let result: EnvironmentConfigurationType = | ||||
|           AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||
|         const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo') | ||||
|         if (result) { | ||||
|           this.create(result.udplocalIp, '8080', carInfo.udpAddress, carInfo.hintPort).then(resolve).catch(reject) | ||||
|           setInterval(() => { | ||||
|             this.udpIndex += 1 | ||||
|           }, 1000) | ||||
|         } | ||||
|       } catch (e) { | ||||
|         reject(e) | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
| 
 | ||||
|   send(bytes: number[]) { | ||||
|     const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo') | ||||
|     if (this.udpIndex > this.currentUdpIndex) { | ||||
|       this.sendMsgExt({ | ||||
|         id: 45, | ||||
|         list: bytes, | ||||
|         carNo: carInfo.carNo, | ||||
|         placeId: carInfo.examinationRoomId | ||||
|       }) | ||||
|       this.currentUdpIndex = this.udpIndex | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   //申请远程扣分查询 | ||||
|   askKf(directives: number) { | ||||
|     const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo') | ||||
|     centerUDPClient.sendMsgExt({ | ||||
|       id: 35, | ||||
|       list: [directives], | ||||
|       carNo: carInfo.carNo, | ||||
|       placeId: carInfo.examinationRoomId, | ||||
|     }) | ||||
|     console.info('surenjun', `考车查询扣分项目内容,请求指令为:${directives}`) | ||||
|   } | ||||
| 
 | ||||
|   //确定远程扣分 | ||||
|   confirmKf(directives: number, code: number) { | ||||
|     const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo') | ||||
|     centerUDPClient.sendMsgExt({ | ||||
|       id: 37, | ||||
|       list: [directives, code], | ||||
|       carNo: carInfo.carNo, | ||||
|       placeId: carInfo.examinationRoomId | ||||
|     }) | ||||
|     console.info('surenjun', `考车发送确定扣分指令,指令为:${directives}`) | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| // obj | ||||
| export const objUDPClient = new ObjUdpClient() | ||||
| 
 | ||||
| // 中心 | ||||
| export const centerUDPClient = new CenterUDPClient() | ||||
| 
 | ||||
| // 灯光 | ||||
| export const lightUDPClient = new LightUDPClient() | ||||
| 
 | ||||
| // 评判 | ||||
| export const judgeUDPClient = new JudgeUdpClient() | ||||
|  | ||||
| @ -24,7 +24,7 @@ class drivingDataStorage { | ||||
|     this.totalTime = 0; | ||||
|     this.fd = await this.fileUtil.editFile( | ||||
|       `${folderPath}/${this.date}.txt`, | ||||
|       `程序启动时间:${this.time} 累计行驶距离:${this.totalDistance}m 累计运行时常:${this.totalTime}min`) | ||||
|       `程序启动时间:${this.time} 累计行驶距离:${this.totalDistance}m 累计运行时常:${this.totalTime}min`)! | ||||
|     return folderPath | ||||
|   } | ||||
| 
 | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| import emitter from '@ohos.events.emitter'; | ||||
| import { judgeUDPClient } from '../UdpUtils'; | ||||
| import { JudgeUdpBusinessInstance } from './JudgeUdpBusiness'; | ||||
| 
 | ||||
| enum EventId { | ||||
|   //远程扣分处理 | ||||
| @ -80,7 +80,7 @@ class JudgeEmitter { | ||||
|   //监听扣分指令 | ||||
|   public sendOnKf(directives: string) { | ||||
|     this.directives = directives | ||||
|     judgeUDPClient.askKf(Number(directives)) | ||||
|     JudgeUdpBusinessInstance.askKf(Number(directives)) | ||||
|   } | ||||
| 
 | ||||
|   // 获取扣分项目编号 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user