// 体重秤读取文件夹模式通讯
|
const fs = require('fs');
|
const fspre = require('fs/promises');
|
const path = require("path");
|
import {sockteStore} from '@/stores/sockteInfo'
|
import { confingInfoStore } from '@/stores/StoresConfing'
|
async function toDataTz(filepath:string) {
|
try {
|
const data = await fspre.readFile(filepath, { encoding: 'utf8' });
|
// console.log('读取文件类容')
|
// console.log(data);
|
const list=data.split('kg')
|
if(list.length===2){
|
const tztext= list[0].substring(list[0].length-7,list[0].length-2)
|
// 替换,当.
|
const res= tztext.replace(",",".")
|
console.log(res)
|
sockteStore().setweightSockte(
|
{
|
deviceName:'seca101',
|
type:"体重秤",
|
result:res,
|
resultTime:new Date().toString(),
|
state:2
|
}
|
)
|
delImg(filepath)
|
}
|
} catch (err) {
|
console.log(err);
|
}
|
}
|
const todatatzs= async (dateStr:string)=>{
|
try {
|
// const pathName='D:/seca101/data'
|
const pathName = confingInfoStore().confingInfo.seca101Path
|
fs.readdir(pathName, function(err: any, files: string | any[]){
|
var dirs: any[] = [];
|
(function iterator(i){
|
if(i == files.length) {
|
var maxtime = 0;
|
var maxfileName = '';
|
for(var j = 0;j<dirs.length;j++){
|
var stat = fs.statSync(path.join(pathName,dirs[j]));
|
console.log(stat)
|
console.log(stat.birthtime)
|
var fileCreateTimeStamp = (new Date(stat.birthtime)).getTime();
|
if(fileCreateTimeStamp>maxtime){
|
maxtime = fileCreateTimeStamp;
|
maxfileName=dirs[j];
|
}
|
}
|
// console.log('最终文件',maxfileName);
|
// 最新的文件路径
|
const param=path.join(pathName,maxfileName)
|
|
// console.log(dateStr,'人年识别时间')
|
var date1 = fs.statSync(param);
|
// const date2=date1.birthtime
|
// console.log('最终文件绝对路径',param);
|
//判断时间要大于人脸识别的时间
|
// console.log(date1.birthtime,'体重写入时间')
|
const times1 = (new Date(date1.birthtime)).getTime();
|
const times2=(new Date(dateStr)).getTime()
|
if(times1>times2){
|
toDataTz(param)
|
}else{
|
console.log(date1,dateStr)
|
delImg(param)
|
console.log('体重时间没有大于人脸识别时间')
|
}
|
return;
|
}
|
fs.stat(path.join(pathName, files[i]), function(err: any, data: { isFile: () => any; }){
|
if(data.isFile()){
|
dirs.push(files[i]);
|
}
|
iterator(i+1);
|
});
|
})(0);
|
})
|
}
|
catch(err){
|
console.log(err);
|
}
|
}
|
//验证后删除文件图片
|
const delImg=(path:string)=>{
|
fs.unlink(path,(err:any)=>{
|
if(err){
|
console.log('删除失败')
|
}else{
|
console.log('删除成功')
|
}
|
})
|
}
|
export {
|
toDataTz,
|
todatatzs
|
}
|