whychdw
2019-12-20 0a707989b1e298d6c22575ef573b6a4f65d0e036
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.concentrator;
 
import android.util.Log;
 
import com.alarms.*;
import com.alarms.BattAlarmData;
import com.socket.BattDataThread;
import com.util.ComBase;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
public class Concentrator_State {
    public static final String TAG = "Concentrator_State";
 
    public static final int WORKSTATE_FLOAT = 0;            //浮充
    public static final int WORKSTATE_DISCH = 1;            //放电
    public static final int WORKSTATE_CHARG = 2;            //充电
 
 
    public static final int BYTE_LEN = 30;
    public static final int REG_LEN = 30;
 
    public static final int MON_REG_COUNT = 100;            //单体数据一次最多的汇集器数量
    public static final int SYS_REG_COUNT = 15;                //系统状态的汇集器数量
 
 
    public long testTimelong = 0;                            //测试时长
    public Date testStartTime = new Date();                    //测试开始时间
    public Date record_time = new Date();                    //记录时间
    public int test_type;                                    //测试类型
    public int test_record_count;                            //当前测试次数(包括充放电)
    public Integer record_num;                                //记录笔数
 
    public int monCount = 24;                                //单体数量
 
 
    private double sysversion;                                //系统版本(0.1)
    private int sysstate;                                    //系统状态         0-常规模式    1-内阻模式    2-编址模式
    private float groupvol;                                //组端电压(0.1)
    private float groupcurr;                                //组端电流(0.1)
    public int battstate;                                    //电池状态    0-浮充     1-放电        2-充电
    private int currdirection;                                //电流方向    0-充电       1-放电
 
    private int backup1;
    private int backup2;
    private int backup3;
    private int backup4;
    private int backup5;
    private int backup6;
    private int backup7;
    private int backup8;
    private int restestcount;                                //内阻测试次数
 
    public BattAlarmData groupVol_AlarmData;                //组端电压告警
    public BattAlarmData battCurr_AlarmData;                //电池电流告警
    public BattAlarmData[] monVol_AlarmData = new BattAlarmData[Concentrator_ComBase.MonomerCountMax];                //单体电压告警
    public BattAlarmData[] monTmp_AlarmData = new BattAlarmData[Concentrator_ComBase.MonomerCountMax];                //单体温度告警
    public BattAlarmData[] monRes_AlarmData = new BattAlarmData[Concentrator_ComBase.MonomerCountMax];                //单体内阻告警
 
 
    public double[]    mon_vols    = new double[Concentrator_ComBase.MonomerCountMax];                    //单体电压
    public double[]    mon_tmps    = new double[Concentrator_ComBase.MonomerCountMax];                    //单体温度
    public double[]    mon_ress    = new double[Concentrator_ComBase.MonomerCountMax];                    //单体内阻
    public int max_monnum;                                    //最大单体电压编号
    public double max_monvol;                                //最大单体电压
    public int min_monnum;                                    //最小单体电压编号
    public double min_monvol;                                //最小单体电压
 
    public int isOutTime = 0;                                //是否超时
 
    //复制设备状态
    public void cloneSystemState(Concentrator_State state){
        this.sysversion = state.sysversion;                                                            //系统版本(0.1)
        this.sysstate = state.sysstate;                                                                //系统状态         0-常规模式    1-内阻模式    2-编址模式
        this.groupvol = state.groupvol;                                                                //组端电压(0.1)
        this.groupcurr = state.groupcurr;                                                            //组端电流(0.1)
        this.battstate = state.battstate;                                                            //电池状态    0-浮充     1-放电        2-充电
        this.currdirection = state.currdirection;                                                    //电流方向    0-充电       1-放电
 
        this.backup1 = state.backup1;
        this.backup2 = state.backup2;
        this.backup3 = state.backup3;
        this.backup4 = state.backup4;
        this.backup5 = state.backup5;
        this.backup6 = state.backup6;
        this.backup7 = state.backup7;
        this.backup8 = state.backup8;
        this.restestcount = state.restestcount;                                                        //内阻测试次数
 
        this.groupvol = groupvol+(float)Math.random()*10;                                            //随机组端电压
        this.groupvol = (float)Math.round(groupvol*100)/100;
        this.groupcurr = groupcurr+(float)Math.random()*5;                                            //随机组端电流
        this.groupcurr = (float)Math.round(groupcurr*100)/100;
    }
 
    //复制单体电压
    public void cloneMonVol(Concentrator_State state,int index){
        for(int i=index;i<(index+100) && i<300;i++){
            this.mon_vols[i] = state.mon_vols[i];                                                    //单体电压
        }
        initmonvols();
    }
 
    //复制单体单体温度
    public void cloneMonTmp(Concentrator_State state,int index){
        for(int i=index;i<(index+100);i++){
            this.mon_tmps[i] = state.mon_tmps[i];                                                    //单体温度
        }
    }
 
    //复制单体单体内阻
    public void cloneMonRes(Concentrator_State state,int index){
        for(int i=index;i<(index +100);i++){
            this.mon_ress[i] = state.mon_ress[i];                                                    //单体内阻
        }
    }
 
    public ByteBuffer getReadBuffer() {
        ByteBuffer tmp = ByteBuffer.allocate(2);
        tmp.order(ByteOrder.BIG_ENDIAN);
        tmp.putShort(ComBase.changeIntToShort(REG_LEN));
        tmp.flip();
        return tmp;
    }
 
    public boolean putByteBuffer(ByteBuffer bf) {
        if(bf.limit() < BYTE_LEN) {
            return false;
        }
        ByteBuffer tmpbuf = bf;
        tmpbuf.position(0);
 
        sysversion = ComBase.changeShortToDouble(tmpbuf.getShort())/10;            //系统版本
        sysstate = ComBase.changeShortToInt(tmpbuf.getShort());                    //系统状态
        groupvol = (float)ComBase.changeShortToDouble(tmpbuf.getShort())/10;            //组端电压
        groupcurr = (float)ComBase.changeShortToDouble(tmpbuf.getShort())/10;            //组端电流
        battstate = ComBase.changeShortToInt(tmpbuf.getShort());                //电池状态
        currdirection = ComBase.changeShortToInt(tmpbuf.getShort());            //电流方向
        this.currdirection=0;
        this.battstate = 2;
        if(currdirection == 1){
            groupcurr = groupcurr *(-1);                                        //改变当前电流方向
        }
 
        backup1 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup2 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup3 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup4 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup5 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup6 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup7 = ComBase.changeShortToInt(tmpbuf.getShort());
        backup8 = ComBase.changeShortToInt(tmpbuf.getShort());
        restestcount = ComBase.changeShortToInt(tmpbuf.getShort());            //内阻测试次数
 
        return true;
    }
 
    public List<BattAlarmData> makeAlarmData(BattDataThread battDataThread, AlarmParam alarmParam){
        List<BattAlarmData> alarmDatas = new ArrayList<>();
        Concentrator_State state = battDataThread.state;
 
        //判断组端电压告警
        AlarmParam.AlarmParamCell groupVol_param = alarmParam.alm_GroupVol.CreateAlarmParamCell(battDataThread.battinf);
        state.groupVol_AlarmData.makeAlarmData(alarmDatas,groupVol_param,state.groupvol);
 
        //判断电池电流告警
        AlarmParam.AlarmParamCell battCurr_param = alarmParam.alm_BattCurr.CreateAlarmParamCell(battDataThread.battinf);
        state.battCurr_AlarmData.makeAlarmData(alarmDatas,battCurr_param,state.groupcurr);
 
        //判断单体告警
        for(int i=0;i<monCount;i++){
            //判断单体电压告警
            AlarmParam.AlarmParamCell monVol_param = alarmParam.alm_MonVol.CreateAlarmParamCell(battDataThread.battinf);
            //Log.e(TAG, "makeAlarmData: monvol  告警值hig"+monVol_param.alm_high_coe+"\tlow"+monVol_param.alm_low_coe );
            state.monVol_AlarmData[i].makeAlarmData(alarmDatas,monVol_param,state.mon_vols[i]);
 
            //判断单体温度告警
            AlarmParam.AlarmParamCell monTmp_param = alarmParam.alm_MonTmp.CreateAlarmParamCell(battDataThread.battinf);
            state.monTmp_AlarmData[i].makeAlarmData(alarmDatas,monTmp_param,state.mon_tmps[i]);
 
            //判断单体内阻告警
            AlarmParam.AlarmParamCell monRes_param = alarmParam.alm_MonRes.CreateAlarmParamCell(battDataThread.battinf);
            state.monRes_AlarmData[i].makeAlarmData(alarmDatas,monRes_param,state.mon_ress[i]);
 
        }
        return alarmDatas;
    }
 
    public boolean putMonDataBuffer(ByteBuffer bf,int reg_addr){
        if(bf.limit() < BYTE_LEN) {
            return false;
        }
        bf.position(0);
        int index = 0;
        int dataType = 1;
        int range = 1000;
 
        //Log.e("Concentrator_State", "putMonDataBuffer:3########################## "+reg_addr);
        if(reg_addr >= 0x201B && reg_addr <= 0x2146){
            //单体电压
            dataType = 1;
            index = (reg_addr - 0x201B)/100;        //设置当前索引
        }else if(reg_addr >= 0x2147 && reg_addr<= 0x2272){
            //单体温度
            dataType = 2;
            index = (reg_addr - 0x2147)/100;
        }else if(reg_addr >= 0x2273 && reg_addr <= 0x239E){
            //单体内阻
            dataType = 3;
            index = (reg_addr - 0x2273)/100;
        }
        for(int i=0;i<100;i++){
            switch (dataType){
                case 1:{
                    mon_vols[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/1000;
                }break;
                case 2:{
                    mon_tmps[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/10;
                }break;
                case 3:{
                    mon_ress[index*100+i] = ComBase.changeShortToDouble(bf.getShort())/1000;
                }break;
            }
        }
        if(dataType == 1){
            initmonvols();
        }
        return true;
    }
 
    //统计当前电池组的最值
    public void initmonvols(){
        this.max_monnum = 1;
        this.max_monvol = this.mon_vols[0];
        this.min_monnum = 1;
        this.min_monvol = this.mon_vols[0];
        for (int i=0;i<this.monCount;i++){
            if(this.mon_vols[i] > this.max_monvol){
                this.max_monvol = this.mon_vols[i];
                this.max_monnum = i+1;
            }
            if(this.mon_vols[i]<this.min_monvol){
                this.min_monvol = this.mon_vols[i];
                this.min_monnum = i+1;
            }
        }
        //Log.e(TAG, "initmonvols: 初始化最大最小值maxmonvol:"+max_monvol+"\tminmonvol:"+min_monvol);
    }
 
    public int getBYTE_LEN() {
        return BYTE_LEN;
    }
 
 
    public double getSysversion() {
        return sysversion;
    }
 
    public int getSysstate() {
        return sysstate;
    }
 
    public double getGroupvol() {
        return groupvol;
    }
 
    public double getGroupcurr() {
        return groupcurr;
    }
 
    public int getBattstate() {
        return battstate;
    }
 
    public int getCurrdirection() {
        return currdirection;
    }
 
    public int getBackup1() {
        return backup1;
    }
 
    public int getBackup2() {
        return backup2;
    }
 
    public int getBackup3() {
        return backup3;
    }
 
    public int getBackup4() {
        return backup4;
    }
 
    public int getBackup5() {
        return backup5;
    }
 
    public int getBackup6() {
        return backup6;
    }
 
    public int getBackup7() {
        return backup7;
    }
 
    public int getBackup8() {
        return backup8;
    }
 
    public int getRestestcount() {
        return restestcount;
    }
 
 
    public void setSysversion(double sysversion) {
        this.sysversion = sysversion;
    }
 
    public void setSysstate(int sysstate) {
        this.sysstate = sysstate;
    }
 
    public void setGroupvol(float groupvol) {
        this.groupvol = groupvol;
    }
 
    public void setGroupcurr(float groupcurr) {
        this.groupcurr = groupcurr;
    }
 
    public void setBattstate(int battstate) {
        this.battstate = battstate;
    }
 
    public void setCurrdirection(int currdirection) {
        this.currdirection = currdirection;
    }
 
    public void setBackup1(int backup1) {
        this.backup1 = backup1;
    }
 
    public void setBackup2(int backup2) {
        this.backup2 = backup2;
    }
 
    public void setBackup3(int backup3) {
        this.backup3 = backup3;
    }
 
    public void setBackup4(int backup4) {
        this.backup4 = backup4;
    }
 
    public void setBackup5(int backup5) {
        this.backup5 = backup5;
    }
 
    public void setBackup6(int backup6) {
        this.backup6 = backup6;
    }
 
    public void setBackup7(int backup7) {
        this.backup7 = backup7;
    }
 
    public void setBackup8(int backup8) {
        this.backup8 = backup8;
    }
 
    public void setRestestcount(int restestcount) {
        this.restestcount = restestcount;
    }
 
    @Override
    public String toString() {
        return "Concentrator_State [sysversion=" + sysversion + ", sysstate=" + sysstate
                + ", groupvol=" + groupvol + ", groupcurr=" + groupcurr + ", battstate=" + battstate
                + ", currdirection=" + currdirection + ", backup1=" + backup1 + ", backup2=" + backup2 + ", backup3="
                + backup3 + ", backup4=" + backup4 + ", backup5=" + backup5 + ", backup6=" + backup6 + ", backup7="
                + backup7 + ", backup8=" + backup8 + ", restestcount=" + restestcount + "]";
    }
}