lxw
2023-05-17 bb215de679c27ea2b93269dd178bf9121d45765b
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
package com.whyc.charge;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
 
import com.whyc.dto.ComFn;
import com.whyc.dto.Fbs5100ComBase;
import org.springframework.stereotype.Component;
 
/**
 *     配网电源
 * @author LiJun
 *
 */
@Component
public class Fbs5100DisChargeData {
    public Fbs5100BattParam battParam;        //电池参数
    public Fbs5100TestParam testParam;        //测试参数
    public Fbs5100CapState capState;        //容量状态
    
    public List<Fbs5100FbsData> fbsDatas;
    
    public int parse_result = Fbs5100ComBase.PARSE_RESULT_NULL;
    
    public Fbs5100DisChargeData() {
        battParam = new Fbs5100BattParam();
        testParam = new Fbs5100TestParam();
        capState = new Fbs5100CapState();
        fbsDatas = new ArrayList<Fbs5100FbsData>();
    }
    
    public void parseFileData(String filePath)
    {
        
        FileInputStream fis = null;
        try {
            File f = new File(filePath);
            //既不是核容放电也不是监测放电
            if(!filePath.endsWith(".BCP") && !filePath.endsWith(".MCP")) {
                parse_result = Fbs5100ComBase.PARSE_RESULT_FILETYPEERR;
                return;
            }
            if(!f.exists()) {
                parse_result = Fbs5100ComBase.PARSE_RESULT_NOTFOUNDFILE;
                return;
                //System.out.println("文件不存在..........");
            }
            fis = new FileInputStream(f);
            byte[] buf = new byte[1024];
            if(fis.read(buf, 0, buf.length) == 1024)
            {
                if(this.readDataHead(buf)) {
                    parse_result = Fbs5100ComBase.PARSE_RESULT_SUCCESS;
                }else {
                    parse_result = Fbs5100ComBase.PARSE_RESULT_FILEERROR;
                }
                
                while(true)
                {                    
                    Fbs5100FbsData fbsData = new Fbs5100FbsData();
                    int tag = fbsData.checkDataHead(fis);
                    if((0xFD == tag) || (0xFC == tag) || (0xFB == tag))
                    {
                        byte[] databuf = new byte[Fbs5100FbsData.BYTE_LEN - 4];
                        if(fis.read(databuf) == databuf.length)
                        {
                            //System.out.println(ComFn.bytesToHexString(databuf, databuf.length));
                            
                            if(fbsData.putByteBuffer(databuf)) {                                
                                fbsDatas.add(fbsData);
                            }
                        }
                    }
                    if(tag == 1)
                        break;
                }
            }else {
                parse_result = Fbs5100ComBase.PARSE_RESULT_FILEERROR;
            }
                
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    //解析数据头
    public boolean readDataHead(byte[] buf) {
        //System.out.println(ComFn.bytesToHexString(buf, buf.length));
        boolean flag = false;
        if(buf.length < (Fbs5100BattParam.BYTE_LEN + Fbs5100TestParam.BYTE_LEN + Fbs5100CapState.BYTE_LEN)) {
            //System.out.println("头部数据长度异常");
            return flag;
        }        
        ByteBuffer bf = ByteBuffer.allocate(buf.length);
        bf.order(ByteOrder.LITTLE_ENDIAN);
        bf.put(buf);
        bf.position(0);
        flag = this.battParam.putByteBuffer(bf);
        //System.out.println("battParam:"+flag);
        flag &= this.testParam.putByteBuffer(bf);
        //System.out.println("testParam:"+flag);
        flag &= this.capState.putByteBuffer(bf);
        //System.out.println("capState:"+flag);
        return flag;
    }
 
    @Override
    public String toString() {
        return "Fbs5100DisChargeData [battParam=" + battParam + ", testParam=" + testParam + ", capState=" + capState
                + ", fbsDatas=" + fbsDatas + ", parse_result=" + parse_result + "]";
    }
    
    
}