DELL
2025-05-13 f9b5693cf3aaae3087e0372827d85f3fe8208ee6
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
package com.battdata_rt;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.base.AppConfig;
import com.base.ComBase;
import com.dec.fbs9100.AppParam;
import com.dec.fbs9100.FBS9100_ComBase;
import com.dec.fbs9100.FBS9100_SysState;
import com.dec.fbs9100.MysqlConnPool;
import com.dec.fbs9100.Sql_Mysql;
 
public class BattData_RT {
    
    public static final int SIGN_TYPE_Default     = 0;        //标准1托1设备(含2台1托1)    
    public static final int SIGN_TYPE_Stand         = 1;        //标准1托2设备
    
    public static final int DEF_CommTimeOutMax    = 7200;
    private MysqlConnPool m_Conn_Pool;
    
    private boolean MonDataFromCInterfaceOk = false;
    
    private boolean MonDataIsUseForIdce8200 = false;
    private boolean mIDCE8200_ConState = false;
    private int mIDCE8200CommTimeout = 0;
    
    public String StationId;
    public String StationName;
    public byte[] StationIp = new byte[4];
    public int BattGroupId = 0;
    public int BattCurrDeviceId = 0;
    public int[] BattCurrValueId = new int[16];
    
    public int FBSDeviceId = 0;
    public String FBSDeviceIp = " ";
    public String FBSDeviceName = " ";
    private String FBSRegCode = " ";
    public int GroupIndexInFBSDevice = 1;
    public String FBSDeviceIp_WG = " ";
    public String FBSDeviceIp_YM = " ";
    
    public int BattGroupNum;
    public int BattState;
    public int MonCount = 1;
    public float MonStdVol;
    public float MonStdCap;
    
    private boolean StoreBattRes_DataEn = false;
    private boolean StoreDataBusy = false;
    private boolean MysqlDataTable_Exist = false;
    public boolean MysqlDataResInfTable_Exist = false;
    public boolean MysqlRecordInf_Exist = false;
    
    public InsertDataToMysql_Task mSqlTask = null;
    
    public ArrayList<MonVolData> al_MonVol = new ArrayList<MonVolData>();
    public ArrayList<MonVolData> al_MonVol_RtOld = new ArrayList<MonVolData>();
    public ArrayList<MonVolData> al_MonVol_History = new ArrayList<MonVolData>();
    public ArrayList<ArrayList<Integer>> al_MonVol_C_Id = new ArrayList<ArrayList<Integer>>();
    
    public BattStatData  mTestData;
    
    private int mCommTimeCoutCount = 0;
    public int mMaxMonNum = 0;
    public float mMaxMonVol = 0;
    public int mMinMonNum = 0;
    public float mMinMonVol = 0;
    
    private AppParam mAppParam  = new AppParam();
    
    private float mBattFloatCurrLevel = (float) 15.0;
    private float mBattFloatVolLevel = (float) (MonStdVol*MonCount*1.125);
    
    private int mTestTimeLongMin = 1*60;
    public int mSaveDataTimeInterval = 10;
    private float mDataHistoryNewLevel = (float) 0.01;
    private int mTestDataRecordCountMax = 10000;
    
    public String ukeyID = "";                            //绑定UKEYID
    public int sign_type = SIGN_TYPE_Default;
    
    Logger logger = null;
    /*********************************************************************************/
    /*********************************************************************************/
    public BattData_RT(AppConfig cfg, AppParam param, MysqlConnPool pool)
    {
        logger = LogManager.getLogger(this.getClass());
        
        mTestData = new BattStatData(cfg.getSourceBattDataType(), cfg.getSybSrvCurrChargeDirPos());
        
        mAppParam.copyAppParam(param);
        //System.out.println("mAppParam.getTestTimeLongMinimum(AppParam.AppParam_Discharge):"+mAppParam.getTestTimeLongMinimum(AppParam.AppParam_Discharge));
        m_Conn_Pool = pool;
        if(null != m_Conn_Pool)
        {
            mSqlTask = new InsertDataToMysql_Task();
        }
    }
    /*********************************************************************************/
    public void updata_BattRtSate_To_RamDb() {
        BattData_RT_SQL.updateBattState_RT_RamDB_Table(m_Conn_Pool, this);
    }
    /*********************************************************************************/
    public void updata_BattRtData_To_RamDb() {
        BattData_RT_SQL.updateBattData_RT_RamDB_Table(m_Conn_Pool, this);
    }
    /*********************************************************************************/
    public void updata_BattRtAlARM_To_RamDb() {
        BattData_RT_SQL.updateBattAlarm_RT_RamDB_Table(m_Conn_Pool, this);
    }
    /*********************************************************************************/
    public BattData_RT updata_SetIp() {
        return BattData_RT_SQL.selectRT_BttInf_Table(m_Conn_Pool, this);
    }
    /*********************************************************************************/
    
    public void make_al_MonVol_C_Id()
    {
        al_MonVol_C_Id.clear();
        ArrayList<Integer> tmp_al_id = null;
        
        int start_index = 0;
        for(int m=0; m<al_MonVol.size(); m++)
        {
            int tmp_id = al_MonVol.get(m).cInterfaceId;
            
            if((start_index < 1) || (start_index > 24) 
                || ((tmp_id-2) > tmp_al_id.get(start_index - 1))
                || ((tmp_id+2) < tmp_al_id.get(start_index - 1)))
            {
                start_index = 0;
                tmp_al_id = new ArrayList<Integer>();
                al_MonVol_C_Id.add(tmp_al_id);
            }
            
            tmp_al_id.add(tmp_id);
            start_index++;
        }
    }
    
    public void make_al_MonVol_Auto_C_Id()
    {
        al_MonVol_C_Id.clear();
        ArrayList<Integer> tmp_al_id = null;
        
        tmp_al_id = new ArrayList<Integer>();
        for(int m=0; m<al_MonVol.size(); m++)
        {
            int tmp_id = al_MonVol.get(m).cInterfaceId;
            tmp_al_id.add(tmp_id);
        }
        
        al_MonVol_C_Id.add(tmp_al_id);
    }
    
    public void setStoreBattRes_DataEnStat(boolean stat)
    {
        StoreBattRes_DataEn = stat;
    }
    public boolean getStoreBattRes_DataEnStat()
    {
        return StoreBattRes_DataEn;
    }
    
    public void setMonitorParam(int test_type)
    {
        float det_vol = MonStdVol / 2;
        if(det_vol < 1)
            det_vol = 1;
        
        if(BattStatData.BATTSTATE_DISCHARGE == test_type) {
            mTestTimeLongMin = mAppParam.getTestTimeLongMinimum(AppParam.AppParam_Discharge);
            mSaveDataTimeInterval = mAppParam.getSaveDataTimeInterval(AppParam.AppParam_Discharge);
            mDataHistoryNewLevel = det_vol * mAppParam.getMonVolChangeLevel(AppParam.AppParam_Discharge);
            mTestDataRecordCountMax = mAppParam.getTestDataRecordCountMax(AppParam.AppParam_Discharge);
        } else {
            mTestTimeLongMin = mAppParam.getTestTimeLongMinimum(AppParam.AppParam_Charge);
            mSaveDataTimeInterval = mAppParam.getSaveDataTimeInterval(AppParam.AppParam_Charge);
            mDataHistoryNewLevel = det_vol * mAppParam.getMonVolChangeLevel(AppParam.AppParam_Charge);
            mTestDataRecordCountMax = mAppParam.getTestDataRecordCountMax(AppParam.AppParam_Charge);
        }
    }
    
    public void updateMonCnt(int new_mon_cnt)
    {
        if((new_mon_cnt>0) && (new_mon_cnt<FBS9100_ComBase.MonomerCountMax)) {
            int mon_cnt = this.al_MonVol.size();
            if(new_mon_cnt > mon_cnt) {
                while(new_mon_cnt > this.al_MonVol.size()) {
                    MonVolData data = new MonVolData(0, 0, this.BattGroupId*1000 + this.al_MonVol.size()+1);
                    this.al_MonVol.add(data);
                    this.al_MonVol_RtOld.add(data.clone());
                    
                    /**
                     * 2022-07-05 @lijun this.al_MonVol_RtOld添加默认值
                     */
                    setMonVol_RtOldDefaultVal();
                    this.al_MonVol_History.add(data.clone());
                }
                //----------- 增加的时候最后设置单体数量 ----------------
                this.MonCount = new_mon_cnt;
                
            } else if(new_mon_cnt < mon_cnt) {
                //----------- 减少的时候最先设置单体数量 ----------------
                this.MonCount = new_mon_cnt;
                
                while(new_mon_cnt < this.al_MonVol.size()) {
                    this.al_MonVol.remove(this.al_MonVol.size()-1);
                    this.al_MonVol_RtOld.remove(this.al_MonVol_RtOld.size()-1);
                    this.al_MonVol_History.remove(this.al_MonVol_History.size()-1);
                }
            }
        }
    }
    
    public void setBattFloatVolCurrLevel(float vol_lev, float curr_level)
    {
        mBattFloatVolLevel = vol_lev;
        mBattFloatCurrLevel = curr_level;
    }
    
    public float getBattFloatVolLev()
    {
        return mBattFloatVolLevel;
    }
    public float getBattFloatCurrLev()
    {
        return mBattFloatCurrLevel;
    }
    public String getDevRegCode()
    {
        return FBSRegCode;
    }
    public void setDevRegCode(String reg_code)
    {
        FBSRegCode = reg_code;
    }
    
    public boolean isMonDataFromCInterfaceOk()
    {
        return MonDataFromCInterfaceOk;
    }
    public void setMonDataFromCInterfaceOk(boolean stat)
    {
        MonDataFromCInterfaceOk = stat;
    }
    
    public boolean isMonDataUseForIdce8200()
    {
        return MonDataIsUseForIdce8200;
    }
    public void setMonDataUseForIdce8200(boolean stat)
    {
        MonDataIsUseForIdce8200 = stat;
    }
    
    /********************************************************************************/
    public boolean checkIfIdce8200ComMTimeout()
    {
        boolean time_ount = false;
        if(mIDCE8200CommTimeout >= 60)
        {
            time_ount = true;
            mIDCE8200_ConState = false;
            setMonDataUseForIdce8200(false);
        }
        if(mIDCE8200CommTimeout < 1000)
            mIDCE8200CommTimeout++;
        
        return time_ount;
    }
    public float MonStdVol() {
        return MonStdVol;
    }
    public float MonStdCap() {
        return MonStdCap;
    }
    public boolean getIdce8200ComState()
    {
        return mIDCE8200_ConState;
    }
    
    public void updateMonVolFromIDCE8200(int mon_index, float vol)
    {
        mIDCE8200_ConState = true;
        mIDCE8200CommTimeout = 0;
        setMonDataUseForIdce8200(true);
        
        al_MonVol.get(mon_index).monVol = vol;
    }
    
    public void updateMonVolFromSQL_SERVER()
    {
        if(true == isMonDataFromCInterfaceOk())
        {
            return;
        }
        
        float sumvol = 0;
        for(int bt_index=0; bt_index<al_MonVol.size(); bt_index++)
        {
            if(false == isMonDataUseForIdce8200())
            {
                al_MonVol.get(bt_index).monVol = al_MonVol.get(bt_index).monVol_TMP;
            }
            sumvol += al_MonVol.get(bt_index).monVol;
        }
        mTestData.groupVol = sumvol;
    }
    
    public void updateMonVolFrom_C_Interface(int mon_c_id, float mon_vol)
    {
        float sumvol = 0;
        for(int var=0; var<al_MonVol.size(); var++)
        {
            MonVolData mvol = al_MonVol.get(var);
            if(mon_c_id == mvol.cInterfaceId)
            {
                if(false == isMonDataUseForIdce8200())
                {
                    mvol.monVol = mon_vol;
                }
                
                if((mon_vol > 0.1) && (false == isMonDataFromCInterfaceOk()))
                {
                    setMonDataFromCInterfaceOk(true);
                }
            }
            
            sumvol += al_MonVol.get(var).monVol;
        }
        mTestData.groupVol = sumvol;
    }
    /********************************************************************************/
    
    public int getBattCount()
    {
        return MonCount;
    }
    public byte getBattState()
    {
        return mTestData.battState;
    }
    public byte getBattTestType()
    {
        return mTestData.battTestState;
    }
    public float getOnlineVol()
    {
        return mTestData.onlineVol;
    }
    public float getGroupVol()
    {
        return mTestData.groupVol;
    }
    public float getGroupTmp()
    {
        return mTestData.groupTmp;
    }
    public float getTestCurr()
    {
        return mTestData.testCurr;
    }
    public float getTestCap()
    {
        return mTestData.testCap;
    }
    public long getTestStartTime()
    {
        return mTestData.startTestTime.getTime();
    }
    public long getTestRecordTime()
    {
        return mTestData.recordTime.getTime();
    }
    public int getTestTimeLong()
    {
        return mTestData.testTimeLong;
    }
    public float getBattRealCap()
    {
        return mTestData.battRealCap;
    }
    public float getBattRestCap()
    {
        return mTestData.battRestCap;
    }
    public int getBattRestTime()
    {
        return mTestData.battRestTime;
    }
    public float getGroupCurr() {
        return mTestData.group_curr;
    }
    
    /**
     * ��������������������������������������������������������������
     * @param al_vol
     */
    public void makeMaxMinMonVol(ArrayList<MonVolData> al_vol)
    {
        if(null != al_vol)
        {
            float maxvol = -80000;
            float minvol = 80000;
            for(int n=0; n<al_vol.size(); n++)
            {
                if(maxvol < al_vol.get(n).monVol)
                {
                    maxvol = al_vol.get(n).monVol;
                    mMaxMonNum = n+1;
                }
                if(minvol > al_vol.get(n).monVol)
                {
                    minvol = al_vol.get(n).monVol;
                    mMinMonNum = n+1;
                }
            }
            mMaxMonVol = maxvol;
            mMinMonVol = minvol;
        }
    }
    
    
    public float getMaxMonVol()
    {
        float maxvol = -80000;
        for(int n=0; n<al_MonVol.size(); n++)
        {
            if(maxvol < al_MonVol.get(n).monVol)
            {
                maxvol = al_MonVol.get(n).monVol;
                mMaxMonNum = n+1;
            }
        }
        return maxvol;
    }
    public float getMinMonVol()
    {
        float minvol = 80000;
        for(int n=0; n<al_MonVol.size(); n++)
        {
            if(minvol > al_MonVol.get(n).monVol)
            {
                minvol = al_MonVol.get(n).monVol;
                mMinMonNum = n+1;
            }
        }
        return minvol;
    }
    public float getGroupVolFromMonVol()
    {
        float gvol = 0;
        for(int n=0; n<al_MonVol.size(); n++)
        {
            gvol += al_MonVol.get(n).monVol;
        }
        return gvol;
    }
    //------------------------------------------------------------------------------------//
    public void makeDataClearByCommTimeOut() {
        if(this.mCommTimeCoutCount < (DEF_CommTimeOutMax+100)) {
            this.mCommTimeCoutCount += 1;
        }
        if(this.mCommTimeCoutCount >= DEF_CommTimeOutMax) {
            mTestData.TestCurr_RT = 0;
            mTestData.testCurr = 0;
        }
    }
    public void makeDataResetByCommTimeOut() {
        this.mCommTimeCoutCount = 0;
    }
    //------------------------------------------------------------------------------------//
    private boolean make_al_MonVol_History()
    {
        boolean data_new = false;
        for(int n=0; n<al_MonVol_History.size(); n++)
        {
            float tmp_dt = al_MonVol.get(n).monVol;
            if(Math.abs((al_MonVol_History.get(n).monVol - tmp_dt)) > mDataHistoryNewLevel)
            {
                al_MonVol_History.get(n).data_new = true;
                al_MonVol_History.get(n).monVol = tmp_dt;
                data_new = true;
            }
        }
        
        return data_new;
    }
    public float get_al_MonVol_History(int index)
    {
        float vol = 0;
        if((index>=0) && (index<al_MonVol_History.size()))
        {
            vol = al_MonVol_History.get(index).monVol;
        }
        return vol;
    }
    public boolean is_al_MonVol_History_New(int index)
    {
        boolean isnew = false;
        if((index>=0) && (index<al_MonVol_History.size()))
        {
            isnew = al_MonVol_History.get(index).data_new;
        }
        return isnew;
    }
    private void clear_al_MonVol_History_New()
    {
        for(int n=0; n<al_MonVol_History.size(); n++)
            al_MonVol_History.get(n).data_new = false;
    }
    private void set_al_MonVol_History_New()
    {
        for(int n=0; n<al_MonVol_History.size(); n++)
        {
            //if(al_MonVol_History.get(n).monVol != al_MonVol.get(n).monVol)
            {
                al_MonVol_History.get(n).data_new = true;
                al_MonVol_History.get(n).monVol = al_MonVol.get(n).monVol;
            }
        }
    }
    //------------------------------------------------------------------------------------//
    private void calBattRestCap(double b_curr, double b_testcap)
    {
        double curr = Math.abs(b_curr);
        double testcap = Math.abs(b_testcap);
        int HourRate = ComBase.GetHourRate(MonStdCap, curr);
        
        mTestData.battRestCap = (float) Math.abs(ComBase.GetMonomerCap(MonStdCap, 
                                                                            HourRate, 
                                                                            testcap, 
                                                                            getMaxMonVol(),
                                                                            getMinMonVol(), 
                                                                            MonStdVol, 
                                                                            ComBase.CapType_Rest));
        mTestData.battRestTime = ComBase.GetRestTimeSecond(mTestData.battRestCap, curr);
        mTestData.battRealCap = (float) (mTestData.battRestCap + testcap * ComBase.N_TO_10H(HourRate));
    }
    //==============================================================//
    //==============================================================//
    //����al_MonVol����������������������0.001
    public boolean checkIfHaveZeroMonVol()
    {
        boolean have_zero_vol = false;
        
        for(int n=0; n<al_MonVol.size(); n++)
        {
            if(n >= 10000)
                break;
            
            if(al_MonVol.get(n).monVol <= 0.001)
            {
                have_zero_vol = true;
                break;
            }
        }
        
        return have_zero_vol;
    }
    
    private void initTestData(byte test_type)
    {
        int count = 1;
        if(BattStatData.BATTSTATE_FLOAT != test_type)
        {
            Sql_Mysql sql = new Sql_Mysql(m_Conn_Pool);
            count = sql.getBattTestRecordCountNew(BattGroupId, Sql_Mysql.BattTestDataInf_Table);
            sql.close_con();
        }
        
        if(count <= 0)
            return;
        
        setMonitorParam(test_type);
        mTestData.init(mTestData.getDevFBS9100S_WorkState(), test_type, count);
        
        MysqlDataTable_Exist = false;
        MysqlDataResInfTable_Exist = false;
        MysqlRecordInf_Exist = false;
    }
 
    public boolean checkIfDataNeedStore()
    {
        boolean store_state = false;
        /***********************************************************************/
        if(false == getStoreDataBusyTag())
        {
            float tmp_curr = mTestData.TestCurr_RT;
            
            if((Math.abs(tmp_curr) < mBattFloatCurrLevel)
                && (FBS9100_SysState.IEC61850_SYS_STATE_DISCHARGING != mTestData.getDevFBS9100S_WorkState()))
            {
                if(BattStatData.BATTSTATE_FLOAT != mTestData.battTestState)
                {
                    mTestData.battTestState = BattStatData.BATTSTATE_FLOAT;
                    mTestData.battState = BattStatData.BATTSTATE_FLOAT;
                    store_state = true;
                }
            }
            
            if(false == store_state)
            {
                if(tmp_curr >= (mBattFloatCurrLevel+2))
                {
                    if(BattStatData.BATTSTATE_DISCHARGE == mTestData.battTestState)
                    {
                        mTestData.battTestState = BattStatData.BATTSTATE_FLOAT;
                        mTestData.battState = BattStatData.BATTSTATE_FLOAT;
                        store_state = true;
                    }
                    else if(BattStatData.BATTSTATE_FLOAT == mTestData.battTestState)
                    {
                        initTestData(BattStatData.BATTSTATE_CHARGE);
                    }
                }
                else if(tmp_curr <= ((mBattFloatCurrLevel+2)*(-1)))
                {
                    if(BattStatData.BATTSTATE_CHARGE == mTestData.battTestState)
                    {
                        mTestData.battTestState = BattStatData.BATTSTATE_FLOAT;
                        mTestData.battState = BattStatData.BATTSTATE_FLOAT;
                        store_state = true;
                    }
                    else if(BattStatData.BATTSTATE_FLOAT == mTestData.battTestState)
                    {
                        initTestData(BattStatData.BATTSTATE_DISCHARGE);
                    }
                } else {//edit by mxpopstar @2020.08.21
                    if(BattStatData.BATTSTATE_FLOAT == mTestData.battTestState) {
                        if(FBS9100_SysState.IEC61850_SYS_STATE_DISCHARGING == mTestData.getDevFBS9100S_WorkState()) {
                            initTestData(BattStatData.BATTSTATE_DISCHARGE);
                            //printBattRtDataDebugInf_FBS9100S(6);
                        } /*else if(FBS9100_SysState.IEC61850_SYS_STATE_CHARGING == mTestData.getDevFBS9100S_WorkState()) {
                            initTestData(BattStatData.BATTSTATE_CHARGE);
                            //printBattRtDataDebugInf_FBS9100S(7);
                        }*/
                    }
                }
            }
            
            if(false == store_state)
            {
                mTestData.testCurr = tmp_curr;
            }
        }
        
        if((BattStatData.BATTSTATE_DISCHARGE == mTestData.battTestState)
            || (BattStatData.BATTSTATE_CHARGE == mTestData.battTestState))
        {
            mTestData.calTestCap(1);
            calBattRestCap(mTestData.testCurr, mTestData.testCap);
            
            boolean dt_new = make_al_MonVol_History();
            if(mTestData.recordNum < mTestDataRecordCountMax) 
            {
                if((true == dt_new) || ((mTestData.dataCalCount % mSaveDataTimeInterval)==0))
                {
                    set_al_MonVol_History_New();
                    store_state = true;
                }
            }
        }
        else
        {
            if(mTestData.groupVol > mBattFloatVolLevel) {
                mTestData.battState = BattStatData.BATTSTATE_JUNCHARGE;
            } else {
                mTestData.battState = BattStatData.BATTSTATE_FLOAT;
            }
        }
        
        /************************** edit by mxpopstar @ 2020-08-30 ****************************/
        if(true == store_state) {
            mTestData.checkAndSetLoaderType();
        }
        /*************************************************************************************/
        
        if(true == getStoreDataBusyTag())
            store_state = false;
        else
        {
            if(true == store_state)
                setStoreDataBusyTag();
        }
        
        /***********************************************************************/
        return store_state;
    }
    
    public void clearStoreDataBusyTag()
    {
        StoreDataBusy = false;
    }
    private boolean getStoreDataBusyTag()
    {
        return StoreDataBusy;
    }
    private void setStoreDataBusyTag()
    {
        StoreDataBusy = true;
    }
    
    /**
     * 2022-07-05 新增给al_MonVol_RtOld设置默认值
     * @lijun
     */
    public void setMonVol_RtOldDefaultVal() {
        if(null != al_MonVol_RtOld) {
            for(int n=0;n<al_MonVol_RtOld.size();n++) {
                al_MonVol_RtOld.get(n).monVol = -1;
                al_MonVol_RtOld.get(n).monTmp = -1;
                al_MonVol_RtOld.get(n).monRes = -1;
                al_MonVol_RtOld.get(n).monSer = -1;
                al_MonVol_RtOld.get(n).connRes = -1;
                al_MonVol_RtOld.get(n).mon_JH_curr = -1;
            }
        }
    }
    
    public class InsertDataToMysql_Task implements Runnable {
        
        private void inserTestDataToDB(Sql_Mysql sql_v, int test_stoptype_t) {
            boolean result = true;
            for(int c=0; c<3; c++) {
                try {
                    sql_v.mysql_con.setAutoCommit(false);
                    //------------------------------------------------------------//
                    String sql_str = BattData_RT_SQL.getInsertBattTestDataStr(BattData_RT.this);
                    sql_v.sqlMysqlExecute(sql_str);
                    //------------------------------------------------------------//
                    ArrayList<String> al_sql_str = BattData_RT_SQL.getInsertOrUpdateBattTestDataStopStr(BattData_RT.this);
                    for(int n=0; n<al_sql_str.size(); n++)
                    {
                        sql_v.sqlMysqlExecute(al_sql_str.get(n));
                    }
                    //------------------------------------------------------------//
                    if(1 == BattData_RT.this.mTestData.testRecordCount) {
                        MysqlRecordInf_Exist = true;
                    }
                    //------------------------------------------------------------//
                    ResultSet rest_t = sql_v.sqlMysqlQuery("SELECT COUNT(*) FROM " + Sql_Mysql.BattTestDataInf_Table
                                        + " WHERE BattGroupId=" + BattData_RT.this.BattGroupId
                                        + " AND test_record_count=" + BattData_RT.this.mTestData.testRecordCount);
                    if(rest_t.next()) {
                        if(rest_t.getInt(1) < 1) {
                            MysqlRecordInf_Exist = false;
                        }
                    }
                    //------------------------------------------------------------//
                    /**
                    *     2021-08-17 edit @lijun 注释为原方法,下面的方法为了在记录历史放电放电数据时记录一笔当前电池组的内阻数据
                    */
                    //sql_str = BattData_RT_SQL.getInsertOrUpdateBattTestDataInfStr(BattData_RT.this, test_stoptype_t);
                    sql_str = BattData_RT_SQL.getInsertOrUpdateBattTestDataInfStr(m_Conn_Pool,BattData_RT.this, test_stoptype_t);
                    sql_v.sqlMysqlExecute(sql_str);
                    //------------------------------------------------------------//
                    sql_v.mysql_con.commit();
                    
                    mTestData.recordNum += 1;
                    mTestData.recordNum_BPM7100 += 1;
                    clear_al_MonVol_History_New();
                    
                    MysqlDataResInfTable_Exist = true;
                    MysqlRecordInf_Exist = true;
                } catch (SQLException e) {
                    try {
                        sql_v.mysql_con.rollback();
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        logger.error(e1.toString(), e1);
                    }
                    
                    logger.error(e.toString(), e);
                    result = false;
                } finally {
                    try {
                        sql_v.mysql_con.setAutoCommit(true);
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        logger.error(e1.toString(), e1);
                    }
                    if(true == result)
                        break;
                    else
                    {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            logger.error(e.toString(), e);
                        }
                    }
                }
            }
        }
        
        @Override
        public void run() {
            Sql_Mysql sql = new Sql_Mysql(m_Conn_Pool);
            String sql_str = null;
            try {
                sql.sqlMysqlUseDB(Sql_Mysql.DB_BATT_TESTDATA);
                
                if(false == MysqlDataTable_Exist)
                {
                    sql_str = BattData_RT_SQL.getCreateBattTestDataStr("`tb_BattTestDataStop_" + BattGroupId + "` ");
                    sql.sqlMysqlExecute(sql_str);
                    MysqlDataTable_Exist = sql.sqlMysqlCheckIfTableExist("tb_BattTestData_" + BattGroupId);
                }
                
                if(false == MysqlDataTable_Exist)
                {
                    logger.warn("tb_BattTestData_" + BattGroupId + " is not exist, create it now...");
                    sql_str = BattData_RT_SQL.getCreateBattTestDataStr("`tb_BattTestData_" + BattGroupId + "` ");
                    sql.sqlMysqlExecute(sql_str);
                }
                
                if(BattStatData.BATTSTATE_FLOAT != mTestData.battTestState) {
                    inserTestDataToDB(sql, -1);
                } else {
                    if(BattStatData.TEST_LOADER_FBS9100S == mTestData.getLoaderType()) {
                        //set_al_MonVol_History_New(); edit by mxpopstar @20200821
                        inserTestDataToDB(sql, mTestData.test_stoptype);
                    } else {
                        //save all discharge and charge data when tested by btse, edit by mxpopstar @20200922
                        if(mTestData.testTimeLong < mTestTimeLongMin) {
                            try {
                                sql.mysql_con.setAutoCommit(false);
                                ArrayList<String> al_str = BattData_RT_SQL.getDeleteBattTestDataStr(BattData_RT.this);
                                for(int n=0; n<al_str.size(); n++) {
                                    sql.sqlMysqlExecute(al_str.get(n));
                                    logger.warn(al_str.get(n));
                                }
                                sql.mysql_con.commit();
                                
                            } catch (SQLException e) {
                                sql.mysql_con.rollback();
                                logger.error(e.toString(), e);
                            } finally {
                                sql.mysql_con.setAutoCommit(true);
                            }
                        }
                    }
                    
                    clearDisTestDataEndData(m_Conn_Pool, BattGroupId,mTestData.testRecordCount );
                }
            } catch (SQLException e) {
                logger.error(e.toString(), e);
            } finally {
                sql.close_con();
                clearStoreDataBusyTag();
            }
        }
    }
    
    
    /**
     *    格式化刚结束的放电数据
     *        放电数据末尾的 清除  {测试电流<[平均电流*0.8]}=>清除
     * @param pool
     * @param BattGroupId
     * @param testRecordCount
     */
    public static void clearDisTestDataEndData(MysqlConnPool pool,int BattGroupId,int testRecordCount) {
        //1.判断当前这一笔数据是否是放电数据=>结果可分析出平均电流
        BattStatData battStatData = queryTestDataInf(pool,BattGroupId,testRecordCount);
        if(battStatData.battTestType_For_DataSave == BattStatData.BATTSTATE_DISCHARGE && battStatData.testTimeLong > 0) {
            //System.out.println("testCap:"+battStatData.testCap+"\ttestTimeLong:"+battStatData.testTimeLong);
            float avg_curr = (float)Math.abs(battStatData.testCap*3600/(battStatData.testTimeLong))*0.8f;            
            //System.out.println("AvgCurr:" + avg_curr + "总记录数:" + battStatData.recordNum);
            //2.查询放电过程中最后一笔测试电流 绝对值>[平均电流*0.8],的位置=>record_num
            int max_record_num = queryMaxTestRecordNum(pool,BattGroupId,testRecordCount,avg_curr);
            //System.out.println("需要删除的起始record_num:" + max_record_num);
            //3.删除记录中  > record_num 的数据结束
            if(max_record_num <= battStatData.recordNum) {
                deleteData(pool,BattGroupId, testRecordCount,max_record_num);
                //4.同步 tb_batt_testdata_inf 和 tb_batt_testdata_id中的record_num
                synchTestDataRecordNum(pool, BattGroupId, testRecordCount, max_record_num);
            }
        }
    }
    
    /**
     *     查询本次测试的信息
     * @param pool
     * @param BattGroupId
     * @param testRecordCount
     * @return
     */
    public static BattStatData queryTestDataInf(MysqlConnPool pool,int BattGroupId,int testRecordCount) {
        BattStatData battData = new BattStatData();
        String sql_str_sel = "SELECT * FROM " + Sql_Mysql.BattTestDataInf_Table + " WHERE BattGroupId = " + BattGroupId + " AND test_record_count = " + testRecordCount;
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool);
        res = sql.sqlMysqlQuery(sql_str_sel);
        try {
            if(res.next()) {
                battData.testCap = res.getFloat("test_cap");
                battData.testTimeLong = res.getInt("test_timelong");
                battData.recordNum = res.getInt("record_num");
                battData.battTestType_For_DataSave = (byte)res.getInt("test_type");
            }
        } catch (SQLException e) {
            sql.logger.error(e.toString(), e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(), e);
                }
            }
            sql.close_con();
        }
        return battData;
    }
    
    /**
     *    查询需要删除的数据
     * @param pool
     * @param BattGroupId
     * @param testRecordCount
     * @param avg_curr
     * @return
     */
    public static int queryMaxTestRecordNum(MysqlConnPool pool,int BattGroupId,int testRecordCount,float avg_curr) {
        int Max_Record_Num = 65535;
        String sql_str_sel =  " SELECT * "
                            + " FROM db_batt_testdata.tb_batttestdata_" + BattGroupId
                            + " WHERE  test_record_count = " + testRecordCount + " and abs(test_curr) >= " + avg_curr
                            + " ORDER BY record_num DESC";
        ResultSet res = null;
        Sql_Mysql sql = new Sql_Mysql(pool);
        res = sql.sqlMysqlQuery(sql_str_sel);
        try {
            if(res.next()) {
                Max_Record_Num = res.getInt("record_num");
            }
        } catch (SQLException e) {
            sql.logger.error(e.toString(), e);
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    sql.logger.error(e.toString(), e);
                }
            }
            sql.close_con();
        }
        return Max_Record_Num;
    }
    
    
    public String getUkeyID() {
        return ukeyID;
    }
    public void setUkeyID(String ukeyID) {
        this.ukeyID = ukeyID;
    }
    /**
     *     删除记录
     * @param pool
     * @param battGroupId
     * @param testRecordCount
     * @param num
     */
    public static void deleteData(MysqlConnPool pool,int battGroupId, int testRecordCount, int record_num) {
        createBattTestData_Clear_Table(pool, battGroupId);
        ArrayList<String> al_str = new ArrayList<>();
        
        String sql_str_ins = "INSERT INTO " + Sql_Mysql.BattTestData_Clear_Table + battGroupId
                + "(BattGroupId,test_record_count,test_type,data_new,data_available,record_num,test_starttime,record_time,test_timelong,online_vol,group_vol,test_curr,test_cap,mon_num,mon_vol,mon_tmp) "
                + "(SELECT BattGroupId,test_record_count,test_type,data_new,data_available,record_num,test_starttime,record_time,test_timelong,online_vol,group_vol,test_curr,test_cap,mon_num,mon_vol,mon_tmp from db_batt_testdata.tb_batttestdata_" + battGroupId
                     + " WHERE test_record_count = " + testRecordCount
                     + " and record_num > " + record_num 
                + ")";    ;
        
        String sql_str_del = " delete FROM db_batt_testdata.tb_batttestdata_"+battGroupId+
                         " WHERE test_record_count = " +testRecordCount+
                         " and record_num > " + record_num;
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            al_str.add(sql_str_ins);
            al_str.add(sql_str_del);
            sql.mysql_con.setAutoCommit(false);
            for(int n=0; n<al_str.size(); n++) {
                sql.sqlMysqlExecute(al_str.get(n));
            }
            sql.mysql_con.commit();
            //System.out.println("清除成功。。。");
            //sql.sqlMysqlExecute(sql_str_del);
        } catch (SQLException e) {
            try {
                sql.mysql_con.rollback();
            } catch (SQLException e1) {
                sql.logger.error(e.toString(), e);
            }
            sql.logger.error(e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    
    /**
     *     删除记录
     * @param pool
     * @param battGroupId
     * @param testRecordCount
     * @param num
     */
    public static void synchTestDataRecordNum(MysqlConnPool pool,int battGroupId, int testRecordCount, int record_num) {
        String sql_str = "  update "+Sql_Mysql.BattTestDataInf_Table+
                          "  Set record_num = " + record_num
                         + " WHERE test_record_count = " + testRecordCount
                         + " and BattGroupId = " + battGroupId;        
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            sql.logger.error(e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
   
    /**
     *     创建数据缓存清空表
     * @param pool
     * @param battGroupId
     */
    public static void createBattTestData_Clear_Table(MysqlConnPool pool,int battGroupId) {
        String sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.BattTestData_Clear_Table + battGroupId + " (" + 
                "  `num` bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  `BattGroupId` int(11) NOT NULL DEFAULT '0'," + 
                "  `test_record_count` int(11) NOT NULL DEFAULT '0'," + 
                "  `test_type` int(11) NOT NULL DEFAULT '0'," + 
                "  `data_new` tinyint(1) NOT NULL DEFAULT '0'," + 
                "  `data_available` tinyint(1) NOT NULL DEFAULT '0'," + 
                "  `record_num` int(11) NOT NULL DEFAULT '0'," + 
                "  `test_starttime` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `record_time` datetime NOT NULL DEFAULT '1982-01-01 00:00:00'," + 
                "  `test_timelong` int(11) NOT NULL DEFAULT '0'," + 
                "  `online_vol` float NOT NULL DEFAULT '0'," + 
                "  `group_vol` float NOT NULL DEFAULT '0'," + 
                "  `test_curr` float NOT NULL DEFAULT '0'," + 
                "  `test_cap` float NOT NULL DEFAULT '0'," + 
                "  `mon_num` int(11) NOT NULL DEFAULT '0'," + 
                "  `mon_vol` float NOT NULL DEFAULT '0'," + 
                "  `mon_tmp` float NOT NULL DEFAULT '0'," + 
                "  PRIMARY KEY (`num`)," + 
                "  KEY `index_test_record_count` (`test_record_count`)" + 
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
}