2024-08-22 13:59:33 +08:00
|
|
|
import FileUtil from './File'
|
|
|
|
|
import {getCurrentTime} from './tools'
|
|
|
|
|
|
|
|
|
|
const LOGTAG = 'GetDistance'
|
|
|
|
|
export default class GetDistance {
|
|
|
|
|
|
|
|
|
|
//后续文件路径待替换
|
|
|
|
|
private fileUtil: FileUtil
|
|
|
|
|
public folderPath: string
|
|
|
|
|
public timeStr: string
|
|
|
|
|
public totalDistance: number
|
|
|
|
|
public date: string
|
|
|
|
|
|
|
|
|
|
constructor(context) {
|
|
|
|
|
const fileUtil = new FileUtil(context)
|
|
|
|
|
this.fileUtil = fileUtil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置文件夹
|
|
|
|
|
public initFolder= async () => {
|
|
|
|
|
const {fileUtil} = this
|
|
|
|
|
const time = await getCurrentTime()
|
|
|
|
|
const folderPath = await 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.date = date;
|
|
|
|
|
await fileUtil.editFile(
|
|
|
|
|
`${folderPath}/${date}.txt`,`程序启动时间:${timeStr} 累计行驶距离:${this.totalDistance}m`
|
|
|
|
|
);
|
|
|
|
|
return folderPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 过程文件数据
|
|
|
|
|
public setTimeData = async (str:number) => {
|
|
|
|
|
const {fileUtil,folderPath,timeStr,date,totalDistance} = this;
|
|
|
|
|
const content = await fileUtil.readFile(`${folderPath}/${date}.txt`) || '';
|
|
|
|
|
console.info('surenjun',str)
|
|
|
|
|
const contentArr = content.split('\n').filter(item => item)
|
|
|
|
|
console.info('surenjun contentArr',JSON.stringify(contentArr))
|
2024-08-23 15:59:56 +08:00
|
|
|
this.totalDistance += str * 1
|
|
|
|
|
contentArr[contentArr.length - 1] = `程序启动时间:${timeStr} 累计行驶距离:${(this.totalDistance).toFixed(2)}m`+ '\n'
|
2024-08-22 13:59:33 +08:00
|
|
|
console.info('surenjun',contentArr.join('\n'))
|
|
|
|
|
await fileUtil.addFile(
|
|
|
|
|
`${folderPath}/${date}.txt`,contentArr.join('\n')
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|