chenyc
2024-12-10 b5646e3f08bf45a27a3db92f5e27568a5448cb03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 体重秤读取文件夹模式通讯
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
}