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
package com.dec.fbs9100;
 
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.base.AppConfig;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
import com.battdata_rt.BattStatData;
 
public class BattDataTestPro_Thread extends Thread {
    
    private ExecutorService m_WorkThreadPool;
    private BattData_RT_Array m_Data;
    private int BattTestCountMax = 10;
    private int WorkThreadCountMax = 300;
    
    private BattTestState m_BattTestState;
    
    Logger logger = null;
    
    public BattDataTestPro_Thread(AppParam param, AppConfig cfg, BattData_RT_Array data)
    {
        logger = LogManager.getLogger(this.getClass());
        
        BattTestCountMax = param.getBattTestGroupCountMax(AppParam.AppParam_Discharge);
        WorkThreadCountMax = cfg.getWorkThreadCountMax();
        m_Data = data;
        
        m_WorkThreadPool = Executors.newFixedThreadPool(WorkThreadCountMax);
        m_BattTestState = new BattTestState(BattTestCountMax);
    }
    //-----------------------------------------------------------------------------//
    private class BattTestState{
        
        private int[] battgroup_working_num;
        
        public BattTestState(int size)
        {
            battgroup_working_num = new int[size];
            for(int n=0; n<BattTestCountMax; n++)
            {
                battgroup_working_num[n] = -1;
            }
        }
        
        public boolean putBattTesttingState( int index, boolean is_testting )
        {
            boolean res = false;
            
            int search_index = 0;
            for( search_index=0; search_index<battgroup_working_num.length; search_index++ )
            {
                if( index == battgroup_working_num[search_index] )
                {
                    if(false == is_testting)
                    {
                        battgroup_working_num[search_index] = -1;
                    }
                    else 
                    {
                        res = true;
                    }
                    break;
                }
            }
            
            if(search_index >= battgroup_working_num.length)
            {
                if(true == is_testting)
                {
                    for(int n=0; n<battgroup_working_num.length; n++)
                    {
                        if(battgroup_working_num[n] < 0)
                        {
                            battgroup_working_num[n] = index;
                            res = true;
                            break;
                        }
                    }
                }
            }
            
            return res;
        }
    }
    //-----------------------------------------------------------------------------//
    @Override
    public void run() {
        logger.info("BattDataTestPro_Thread Started By TimerTask ...");
        Timer timer = new Timer();
        MyBattTestTask myTask1 = new MyBattTestTask(); 
        timer.scheduleAtFixedRate(myTask1, 1000, 1000);    
        
        while(true)
        {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                logger.error(e.toString(), e);
            }
        }
    }
    
    class MyBattTestTask extends java.util.TimerTask{
        String info = "MyBattTestTask By TimerTask";
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try 
            {
                for(int n=0; n<m_Data.getItemCount(); n++)
                {
                    BattData_RT rt_data = m_Data.getItem(n);
                    
                    boolean need_to_store = false;
                    need_to_store = rt_data.checkIfDataNeedStore();
 
                    boolean batt_is_testing = false;
                    if((BattStatData.BATTSTATE_CHARGE == rt_data.getBattTestType())
                        || (BattStatData.BATTSTATE_DISCHARGE == rt_data.getBattTestType())
                        || (BattStatData.BATTSTATE_MONITOR == rt_data.getBattTestType())
                        || (true == need_to_store))
                    {
                        batt_is_testing = true;
                    }
                    
                    if(true == m_BattTestState.putBattTesttingState(n, batt_is_testing))
                    {
                        if(true == need_to_store)
                        {
                            m_WorkThreadPool.execute(rt_data.mSqlTask);
                        }
                    }
                    else
                    {
                        rt_data.clearStoreDataBusyTag();
                    }
                }
            } catch (Exception e) {
                logger.error(e.toString(), e);
            }
        }
        
        public String getInfo(){
            return info;     
        }     
        public void setInfo(String info){
            this.info = info;     
        }
    }     
}