import { GetCurrentTime } from '../Common' import FileUtils from '../FileUtils' import common from '@ohos.app.ability.common' export default class GetDistance { public folderPath: string public timeStr: string public totalDistance: number public totalTime: number public date: string public fd: number //后续文件路径待替换 private fileUtil: FileUtils // 设置文件夹 public initFolder = async () => { const time = await GetCurrentTime() const folderPath = await this.fileUtil.initFolder(`/车辆行驶距离统计`); console.info('surenjun folderPath=>', folderPath); const date = time.split(' ')[0].split('-').join('_') const timeStr = time.split(' ')[1] this.timeStr = timeStr this.folderPath = folderPath; this.totalDistance = 0; this.totalTime = 0; this.date = date; this.fd = await this.fileUtil.editFile( `${folderPath}/${date}.txt`, `程序启动时间:${timeStr} 累计行驶距离:${this.totalDistance}m 累计运行时常:${this.totalTime}min`, this.fd ); return folderPath } constructor(context: common.UIAbilityContext) { const fileUtil = new FileUtils(context) this.fileUtil = fileUtil; } // 过程文件数据 public setTimeData = async (str: number) => { // const { fileUtil, folderPath, timeStr, date, totalDistance } = this; console.log('folderPath', this.folderPath) const content = await this.fileUtil.readFile(`${this.folderPath}/${this.date}.txt`) || ''; const contentArr = content.split('\n').filter(item => item) console.info('surenjun contentArr', JSON.stringify(contentArr)) this.totalDistance += (str * 1 > 200 ? 200 : str * 1) this.totalTime += 1; contentArr[contentArr.length - 1] = `程序启动时间:${this.timeStr} 累计行驶距离:${(this.totalDistance).toFixed(2)}m 累计运行时常:${Math.ceil(this.totalTime / 60)}min` + '\n' console.info('surenjun', contentArr.join('\n')) console.log('folderPath', this.folderPath, this.date) this.uploadData() // await fileUtil.addFile( // `${folderPath}/${date}.txt`,contentArr.join('\n') // ); } //上传行驶里程数据 uploadData = async () => { setInterval(() => { // const { carId } = AppStorage.get('carInfo'); // const { date, timeStr, totalDistance } = this; return //"carid":"1001","startTime":"2024-08-24 08:09:01","time":"111233", "mileage":"1222" }, 5000) } }