mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.dev.ntm.data.fbo;
 
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 com.dev.ntm.cmd.NTM_Crc16;
import com.dev.ntm.data.idc.IdcData;
 
public class FboData {
    public static int BattSumMax    =     500;
    public FboDataType m_DataType = new FboDataType();
    public int CRC16;
    public FboTestTime m_TestTime = new FboTestTime();
    public int BattGroup;
    public int BattSum;
    public float OnlineVol;
    public float SumVoltage;
    public float SumCurrent;
    public float[] SubCurrent = new float[4];
    public float AllCap;
    public float[] SubCap = new float[4];
    public float[] SingleVol = null;
    
    public FboData(int batt_sum) {
        BattSum = batt_sum;
        if(BattSum > BattSumMax)
            BattSum = BattSumMax;
        else if (BattSum < 0) {
            BattSum = 0;
        }
        
        SingleVol = new float[BattSum];
    }
    
    public boolean setData(byte[] buf)
    {
        ByteBuffer bf = ByteBuffer.allocate(2048);
        bf.order(ByteOrder.LITTLE_ENDIAN);
        bf.put((byte) (m_DataType.TypeTag0&0xFF));
        bf.put((byte) (m_DataType.TypeTag1&0xFF));
        bf.put((byte) (m_DataType.TypeTag2&0xFF));
        bf.put((byte) (m_DataType.TypeTag3&0xFF));
        bf.put(buf);
        bf.flip();
        /*
        m_DataType.TypeTag0 = (int) (bf.get()&0xFF);
        m_DataType.TypeTag1 = (int) (bf.get()&0xFF);
        m_DataType.TypeTag2 = (int) (bf.get()&0xFF);
        m_DataType.TypeTag3 = (int) (bf.get()&0xFF);
        */
        CRC16 = bf.getShort(4)&0xFFFF;
        bf.putShort(4, (short) 0x0000);
        int calcrc = NTM_Crc16.CalCRC16(bf, bf.limit());
        //System.out.println("CRC16:" + CRC16 + ",  calcrc:" + calcrc);
        if(CRC16 != calcrc) {
            return false;
        }
        
        bf.position(6);
        m_TestTime.hour = (int) (bf.get()&0xFF);
        m_TestTime.minute = (int) (bf.get()&0xFF);
        m_TestTime.second = (int) (bf.get()&0xFF);
        BattGroup = (int) (bf.get()&0xFF);
        
        int batt_cnt = (int) (bf.getShort()&0xFFFF);
        if(batt_cnt > BattSum)
            batt_cnt = BattSum;
        else if (batt_cnt < 0) {
            batt_cnt = 0;
        }
        
        OnlineVol = ((float) (bf.getShort()&0xFFFF)) / 10;
        SumVoltage = ((float) (bf.getShort()&0xFFFF)) / 10;
        
        SumCurrent = ((float) (bf.getShort()&0xFFFF)) / 10;
        if(0xFD == m_DataType.TypeTag0) {
            SumCurrent *= -1;
        }
        for(int n=0; n<4; n++)
        {
            SubCurrent[n] = ((float) (bf.getShort()&0xFFFF)) / 10;
            if(0xFD == m_DataType.TypeTag0) {
                SubCurrent[n] *= -1;
            }
        }
        
        AllCap = ((float) (bf.getShort()&0xFFFF));
        if(0xFD == m_DataType.TypeTag0) {
            AllCap *= -1;
        }
        for(int n=0; n<4; n++)
        {
            SubCap[n] = ((float) (bf.getShort()&0xFFFF));
            if(0xFD == m_DataType.TypeTag0) {
                SubCap[n] *= -1;
            }
        }
        
        for(int n=0; n<batt_cnt; n++) {
            SingleVol[n] = ((float) (bf.getShort()&0xFFFF)) / 1000;
        }
        
        return true;
    }
    
    public static void checkFboFile(File file, FboDataInf data_inf, ArrayList<FboData> al_fbo_data)
    {
        File f = file;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            byte[] buf = new byte[256];
            if(fis.read(buf, 0, buf.length) == 256)
            {
                data_inf.setDataInf(buf);
                while(true)
                {
                    FboDataType mType = new FboDataType();
                    int tag = mType.checkDataHead(fis);
                    if((0xFD == tag) || (0xFC == tag))
                    {
                        byte[] databuf = new byte[data_inf.BattSum*2 + 32];
                        if(fis.read(databuf) == databuf.length)
                        {
                            FboData m_FboData = new FboData(data_inf.BattSum);
                            m_FboData.m_DataType = mType;
                            if(true == m_FboData.setData(databuf)) {
                                al_fbo_data.add(m_FboData);
                            }
                        }
                    }
                    if(tag == 1)
                        break;
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    
    public void setData(IdcData i_data) {
        m_DataType.TypeTag0 = i_data.DataType;
        m_DataType.TypeTag1 = i_data.DataType;
        m_DataType.TypeTag2 = i_data.DataType;
        m_DataType.TypeTag3 = i_data.DataType;
        
        OnlineVol = 0;
        SumVoltage = i_data.SumVol;
        SumCurrent = i_data.SumCur;
        SubCurrent[0] = SumCurrent;
        
        AllCap = i_data.SumAH;
        SubCap[0] = AllCap;
        
        int param = (-1);
        if(0xFC == m_DataType.TypeTag0) {
            param = 1;
        }
        SumCurrent = Math.abs(SumCurrent) * param;
        SubCurrent[0] = Math.abs(SubCurrent[0]) * param;
        
        AllCap = Math.abs(AllCap) * param;
        SubCap[0] = Math.abs(SubCap[0]) * param;
        
        for(int n=0; n<400; n++)
        {
            SingleVol[n] = i_data.MonomerVol[n];
        }
    }
    
    public int getMinIndex()
    {
        int num = 0;
        float min = 800000;
        for(int n=0; n<SingleVol.length; n++)
        {
            if((SingleVol[n] > 0.1) && (SingleVol[n] < min))
            {
                min = SingleVol[n];
                num = n;
            }
        }
        
        return num;
    }
    
    public int getMaxIndex()
    {
        int num = 0;
        float max = 0;
        for(int n=0; n<SingleVol.length; n++)
        {
            if((SingleVol[n] > 0.1) && (max < SingleVol[n]))
            {
                max = SingleVol[n];
                num = n;
            }
        }
        
        return num;
    }
    
    public float getMinVol()
    {
        float min = 800000;
        for(int n=0; n<SingleVol.length; n++)
        {
            if((SingleVol[n] > 0.1) && (SingleVol[n] < min))
            {
                min = SingleVol[n];
            }
        }
        
        return min;
    }
    
    public float getMaxVol()
    {
        float max = 0;
        for(int n=0; n<SingleVol.length; n++)
        {
            if((SingleVol[n] > 0.1) && (max < SingleVol[n]))
            {
                max = SingleVol[n];
            }
        }
        
        return max;
    }
}