From ab9d50f47ebb491a6bd307f3c7a4318b188a6b97 Mon Sep 17 00:00:00 2001 From: lixiao <932184220@qq.com> Date: Wed, 9 Apr 2025 10:46:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 5 +- entry/src/main/ets/utils/FileUtils.ets | 3 +- entry/src/main/ets/utils/Request.ets | 25 ++- entry/src/main/ets/utils/UdpUtils.ets | 185 +----------------- .../ets/utils/business/DrivingDataStorage.ets | 2 +- .../src/main/ets/utils/business/UdpEvent.ets | 4 +- 6 files changed, 27 insertions(+), 197 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 7126995f..da33384f 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -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('carInfo', {}) AppStorage.setOrCreate('examinerInfo', {}) diff --git a/entry/src/main/ets/utils/FileUtils.ets b/entry/src/main/ets/utils/FileUtils.ets index 1cd00a01..1808c9bc 100644 --- a/entry/src/main/ets/utils/FileUtils.ets +++ b/entry/src/main/ets/utils/FileUtils.ets @@ -7,7 +7,7 @@ import { BusinessError } from '@ohos.base' export default class FileUtils { - public destFile: string + public destFile: string = '' public filePathFdObj: Record = {} 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 } } diff --git a/entry/src/main/ets/utils/Request.ets b/entry/src/main/ets/utils/Request.ets index 0d99c2ec..f35c8667 100644 --- a/entry/src/main/ets/utils/Request.ets +++ b/entry/src/main/ets/utils/Request.ets @@ -35,6 +35,10 @@ function xmlToJSON(target: string): object { return transfer(result['_elements']) } +function setObj(target: Record, key: string, value: object) { + target[key] = value +} + function transfer(target: Array, name?: string): object { const result: object = new Object() @@ -44,7 +48,7 @@ function transfer(target: Array, name?: string): object { if (_elements === undefined) { return } - const jsonObj: object = result[el['_name']] + const jsonObj: Record = result[el['_name']] as Record const handleCommonArray = (obj: object) => { if (Array.isArray(jsonObj)) { result[el['_name']].push(obj) @@ -57,16 +61,18 @@ function transfer(target: Array, 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(options: RequestOption): Promise { +export default function Request(options: RequestOption): Promise { return new Promise((resolve, reject) => { const instance = http.createHttp() const baseURL = options.host || base @@ -123,13 +130,13 @@ export default function Request(options: RequestOption): Promise 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) { diff --git a/entry/src/main/ets/utils/UdpUtils.ets b/entry/src/main/ets/utils/UdpUtils.ets index 6f5fcfa3..2d64a3b5 100644 --- a/entry/src/main/ets/utils/UdpUtils.ets +++ b/entry/src/main/ets/utils/UdpUtils.ets @@ -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 = [] private errorEvents: Array = [] - private dealMethod: DealMethod + private dealMethod?: DealMethod 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 { 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 { - return new Promise((resolve, reject) => { - try { - let result: EnvironmentConfigurationType = - AppStorage.get("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 { - return new Promise((resolve, reject) => { - try { - let result: EnvironmentConfigurationType = - AppStorage.get("EnvironmentConfiguration") - const carInfo: CarInfoType = AppStorage.get('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 { - return new Promise((resolve, reject) => { - try { - let result: EnvironmentConfigurationType = - AppStorage.get("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 { - return new Promise((resolve, reject) => { - try { - let result: EnvironmentConfigurationType = - AppStorage.get("EnvironmentConfiguration") - const carInfo: CarInfoType = AppStorage.get('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('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('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('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() diff --git a/entry/src/main/ets/utils/business/DrivingDataStorage.ets b/entry/src/main/ets/utils/business/DrivingDataStorage.ets index d83d8ae1..67ac62d1 100644 --- a/entry/src/main/ets/utils/business/DrivingDataStorage.ets +++ b/entry/src/main/ets/utils/business/DrivingDataStorage.ets @@ -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 } diff --git a/entry/src/main/ets/utils/business/UdpEvent.ets b/entry/src/main/ets/utils/business/UdpEvent.ets index 7e6949fc..b5ae3b12 100644 --- a/entry/src/main/ets/utils/business/UdpEvent.ets +++ b/entry/src/main/ets/utils/business/UdpEvent.ets @@ -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)) } // 获取扣分项目编号