fix: 更新 EntryAbility、SignDisplayCom 和 DrivingDataStorage 组件中的代码逻辑和类型声明,提升代码一致性

This commit is contained in:
wangzhongjie 2025-04-09 09:29:28 +08:00
parent 524eecb149
commit fa24528306
3 changed files with 21 additions and 25 deletions

View File

@ -5,7 +5,6 @@ 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';
@ -42,7 +41,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', {})

View File

@ -2,7 +2,7 @@ import router from '@ohos.router';
import RealTime from '../compontents/judge/RealTime';
import { GPSData, SignalData } from '../../mock';
import { SignalDataType } from '../../model';
import { ObtainSignalData } from '../../utils/business/ObtainSignalData';
import { ObtainUdpBusinessInstance } from '../../utils/business/ObtainUdpBusiness';
@Component
export default struct SignDisplayCom {
@ -327,16 +327,14 @@ export default struct SignDisplayCom {
// const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt')
// this.udplocalIp=JSON.parse(data)?.udplocalIp||'192.168.7.170'
this.ratio = this.ratio * (this.scaleNum || 1);
const that = this
const { showBack, getSignal } = this
if (showBack) {
if (this.showBack) {
this.ratio = 1.4
ObtainSignalData.getData((msg)=>{
if (msg) {
getSignal(msg)
}
ObtainUdpBusinessInstance.onMsg((msg) => {
if (msg) {
this.getSignal(msg)
}
})
} else {
@ -344,7 +342,7 @@ export default struct SignDisplayCom {
this.signalTimer = setInterval(() => {
const msgStr = AppStorage.get<string>('msgStr')
if (msgStr) {
getSignal(msgStr)
this.getSignal(msgStr)
}
}, 200)
@ -353,10 +351,9 @@ export default struct SignDisplayCom {
onPageShow() {
const getSignal = this.getSignal;
const that = this
const showBack = this.showBack;
if (showBack) {
ObtainSignalData.getData((msg)=>{
ObtainUdpBusinessInstance.onMsg((msg) => {
if (msg) {
getSignal(msg)
}

View File

@ -3,26 +3,26 @@ import dayTs from '../Date';
import FileUtils from '../FileUtils';
class drivingDataStorage {
public folderPath: string
public time: string
public totalDistance: number
public totalTime: number
public date: string
public fd: number
private fileUtil: FileUtils
public folderPath?: string
public time?: string
public totalDistance?: number
public totalTime?: number
public date?: string
public fd?: number
private fileUtil?: FileUtils
constructor() {
}
// 初始化文件夹
async initializeTheDrivingDataFolder() {
const folderPath = await this.fileUtil.initFolder(`/车辆行驶距离统计`);
const folderPath = await this.fileUtil!.initFolder(`/车辆行驶距离统计`);
this.time = dayTs().format("HH:mm:ss")
this.date = dayTs().format("YYYY_MM_DD")
this.folderPath = folderPath;
this.totalDistance = 0;
this.totalTime = 0;
this.fd = await this.fileUtil.editFile(
this.fd = await this.fileUtil!.editFile(
`${folderPath}/${this.date}.txt`,
`程序启动时间:${this.time} 累计行驶距离:${this.totalDistance}m 累计运行时常:${this.totalTime}min`)
return folderPath
@ -30,12 +30,13 @@ class drivingDataStorage {
// 设置行驶过程数据
async setDrivingProcessData(str: number) {
const content = await this.fileUtil.readFile(`${this.folderPath}/${this.date}.txt`) || '';
const content = await this.fileUtil!.readFile(`${this.folderPath}/${this.date}.txt`) || '';
const contentArr = content.split('\n').filter(item => item)
this.totalDistance += (str * 1 > 200 ? 200 : str * 1)
this.totalDistance = (this.totalDistance || 0) + (str * 1 > 200 ? 200 : str * 1);
this.totalTime += 1;
contentArr[contentArr.length - 1] =
`程序启动时间:${this.time} 累计行驶距离:${(this.totalDistance).toFixed(2)}m 累计运行时常:${Math.ceil(this.totalTime /
`程序启动时间:${this.time} 累计行驶距离:${(this.totalDistance ||
0).toFixed(2)}m 累计运行时常:${Math.ceil(this.totalTime /
60)}min` + '\n'
}