lxw
2023-03-02 2ebf5324ac4e7dd23f4857e7080d25d40065b86c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.whyc.abe;
 
import com.whyc.constant.ConstantDefined;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
 
public class ABEDataParseUtil {
    
    /*public List<ABEFBSData> fbsData;
    public ABEDataHeadStart fbsDataStart;
    public ABEDataHeadStop fbsDataStop;
    
    public int parse_result = ConstantDefined.PARSE_RESULT_NULL;*/
    
    public static ABEDataParase readFBSFile(MultipartFile multipartFile) throws IOException {
        List<ABEFBSData> fbsData = null;
        ABEDataHeadStart fbsDataStart = null;
        ABEDataHeadStop fbsDataStop = null;
        int parse_result = ConstantDefined.PARSE_RESULT_NULL;
        //int Data_Type = -1;
        FileInputStream fis = null;
        try {
            //File f = new File(filePath);
            fis = (FileInputStream) multipartFile.getInputStream();
            if(multipartFile.getOriginalFilename().toUpperCase().endsWith(".DDF")) {
                //放电数据文件
                fbsDataStart = new ABEDisDataHeadStart();
                fbsDataStop = new ABETestDataHeadStop();
                //Data_Type = ConstantDefined.DATA_TYPE_DIS;
            }else if(multipartFile.getOriginalFilename().toUpperCase().endsWith(".CCF")) {
                //充电数据文件
                fbsDataStart = new ABEChrDataHeadStart();
                fbsDataStop = new ABETestDataHeadStop();
                //Data_Type = ConstantDefined.DATA_TYPE_DIS;
            }else {
                //非法文件
                parse_result = ConstantDefined.PARSE_RESULT_FILETYPEERR;
            }
            /*if(!f.exists()) {
                //文件不存在
                parse_result = ConstantDefined.PARSE_RESULT_NOTFOUNDFILE;
                //System.out.println("文件不存在..........");
            }
            fis = new FileInputStream(f);*/
            
            /*if(ConstantDefined.DATA_TYPE_DIS == Data_Type) {
                
            }else if(ConstantDefined.DATA_TYPE_CHR == Data_Type) {
                fbsDataStart = new ABEChrDataHeadStart();
                fbsDataStop = new ABETestDataHeadStop();    
            }else if(ConstantDefined.DATA_TYPE_RULE == Data_Type){
                fbsDataStart = new ABECycleDataHeadStart();
                fbsDataStop = new ABECycleDataHeadStop();    
            }else {
                parse_result = ConstantDefined.PARSE_RESULT_FILETYPEERR;
                return;
            }*/
            byte[] buf = new byte[512];
            //解析文件头
            if(fis.read(buf, 0, buf.length) == 512)
            {
                ByteBuffer bf = ByteBuffer.allocate(buf.length);
                bf.order(ByteOrder.LITTLE_ENDIAN);
                bf.put(buf);
                bf.position(0);
                
                if(fbsDataStart.setDataInf(bf) && fbsDataStop.setDataInf(bf)) {
                    parse_result = ConstantDefined.PARSE_RESULT_SUCCESS;
                    
                } else {
                    parse_result = ConstantDefined.PARSE_RESULT_FILEERROR;
                }
                
                fbsData = new ArrayList<>();
                ABEDataType mType = new ABEDataType();
                while(true)
                {                    
                    int tag = mType.checkDataHead(fis);
                    if((0xFD == tag) || (0xFC == tag) || (0xFB == tag))
                    {
                        //解析每一笔单体数据
                        byte[] databuf = new byte[8 + 80 + 2];
                        if(fis.read(databuf) == databuf.length)
                        {
                            ABEFBSData m_FbsData = new ABEFBSData();
                            m_FbsData.m_DataType = mType;
                            m_FbsData.setData(databuf);                            
                            fbsData.add(m_FbsData);
                        }
                    }
                    if(tag == 1) {
                        break;
                    }
                }
            }else {
                parse_result = ConstantDefined.PARSE_RESULT_FILEERROR;
            }
            ABEDataParase parse = new ABEDataParase();
            parse.setFbsData(fbsData);
            parse.setFbsDataStart(fbsDataStart);
            parse.setFbsDataStop(fbsDataStop);
            parse.setParse_result(parse_result);
            return parse;
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    /*public static void main(String[] args) throws FileNotFoundException {
        //fbo.readFboFile("D:/test/F2022-03-09 11.26.12.FBX");
        //fbo.readFboFile("D:/test/F2022-03-09 16.22.37.FBX");
        //fbo.readFboFile("D:/test/F2022-03-10 11.12.30.FBX");
        File file = new File("C:\\\\Users\\\\29550\\\\Documents\\\\WeChat Files\\\\wxid_uh2bej4ad4ya22\\\\FileStorage\\\\File\\\\2022-09\\\\F2022-08-02-094422-1.DDF");
        FileInputStream fileInputStream = new FileInputStream(file);
        ABEDataParase abeDataParase = readFBSFile(file);
        //fbs.readFBSFile("D:\\桌面文件备份\\公司各种设备资料\\硬件数据文件解析相关\\ABE文件解析相关\\数据文件\\F2022-08-02-094422-1.DDF");
        //fbs.readFBSFile("D:\\桌面文件备份\\公司各种设备资料\\硬件数据文件解析相关\\ABE文件解析相关\\数据文件\\F2022-08-02-094422-1.DDF");
        //fbs.readFBSFile("D:\\桌面文件备份\\公司各种设备资料\\硬件数据文件解析相关\\ABE文件解析相关\\数据文件\\F2022-08-03-165222-7.CCF");
        System.out.println(abeDataParase);
    }*/
    
}