81041
2019-11-27 7c0c5ab7ecded4e1573c2c4e12f117b780260f5d
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
package com.socket;
 
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
 
import com.concentrator.Concentrator_State;
import com.fgkj.dao.DBHelper;
import com.util.Com;
 
import java.util.Date;
 
/**
 * 历史充放电数据记录线程
 */
public class BattTestDataRecordThread extends Thread{
    public static final String TAG = "BattTestDataRecodThread";
 
    private static final int saveDataIntever = 10;      //记录历史数据的时间间隔
    private static final int minTestTimeLong = 60;      //最小的测试时长
 
    public DBHelper dbHelper;
    public BattDataThread battData;
 
    public boolean isRecording = false;
 
 
    public BattTestDataRecordThread(DBHelper dbHelper, BattDataThread battData){
        this.dbHelper = dbHelper;
        this.battData = battData;
 
        dbHelper.Create_batttestdata(dbHelper.getWritableDatabase(),battData.battIndex);            //创建电池组历史数据表tb_batttestdata_id
    }
 
 
    @Override
    public void run() {
        Log.e(TAG,"run:BattTestDataRecordThread start at " + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms));
 
 
        int nowTestType = BattDataThread.TESTTYPE_NULL;                 //当前记录的测试类型
        int nowworkState = Concentrator_State.WORKSTATE_FLOAT;          //当前电池组的状态
        Date lastRecordTime = new Date();                               //上一次记录的时间
        boolean isStartRecord = false;                                  //是否是新的放电记录
 
 
        while(true){
            try {
                if(isRecording){
                    //正在记录历史充放电数据
                    battData.state.record_time = new Date();
                    if(isStartRecord){
                        battData.state.testStartTime = new Date();                                                  //测试开始时间
                        battData.state.test_type = nowTestType;                                                     //设置当前的测试类型
                        battData.state.testTimelong = 0;
                        battData.state.test_record_count = queryMaxTestRecordCount(battData.battIndex,dbHelper);
                        battData.state.record_num = 1;
 
                        //刚开始记录的线程
                        insertBattTestDatainf(dbHelper,battData);
 
                        lastRecordTime = new Date();
                        isStartRecord = false;
                    }
                    int testTimelong = (int)((battData.state.record_time.getTime() - battData.state.testStartTime.getTime())/1000);             //测试时长
                    battData.state.testTimelong = testTimelong;
                    if(battData.state.battstate != nowworkState || !battData.isInstall || battData.loseTimeLong > 60*60){
                        //当前电池组的状态变化//当前电池组变成未安装//通讯超时(1小时)
                        isRecording = false;
                    }
                    int recordInterver = (int)Math.abs(new Date().getTime()-lastRecordTime.getTime())/1000;                     //距离上次记录数据的时长
                    if(recordInterver >= saveDataIntever || !isStartRecord){
                        lastRecordTime = new Date();
                        //放电结束或者记录时间到
                        //记录当前的充放电记录
                        insertBattTestData(dbHelper,battData);
                    }
                    battData.state.record_num++;
 
                }else{
                    //暂无记录充放电数据
                    if(battData.state.battstate == Concentrator_State.WORKSTATE_DISCH && battData.isInstall && battData.loseTimeLong <= 60*60){
                        //正在放电
                        isRecording = true;
                        isStartRecord = true;
                        nowTestType = BattDataThread.TESTTYPE_DISTEST;
                        nowworkState = Concentrator_State.WORKSTATE_DISCH;
                    }else if(battData.state.battstate == Concentrator_State.WORKSTATE_CHARG && battData.isInstall && battData.loseTimeLong <= 60*60){
                        //正在充电
                        isRecording = true;
                        isStartRecord = true;
                        nowTestType = BattDataThread.TESTTYPE_CHRTEST;
                        nowworkState = Concentrator_State.WORKSTATE_CHARG;
                    }else{
                        nowTestType = BattDataThread.TESTTYPE_NULL;
                        nowworkState = Concentrator_State.WORKSTATE_FLOAT;
                        isRecording = false;
                    }
 
                }
                sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
 
    /**
     *  查询数据库目前当前电池组的最大
     * @param battgroupid
     * @param dbHelper
     * @return
     */
    public static int queryMaxTestRecordCount(int battgroupid,DBHelper dbHelper){
        int max_test_record_count = 1;
        String sql_str = " SELECT max(test_record_count)as max_test_record_count FROM tb_batttestdata_inf WHERE BattGroupId =  " + battgroupid;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        try {
            Cursor rs = db.rawQuery(sql_str,null);
            if (rs.moveToNext()){
                max_test_record_count = rs.getInt(rs.getColumnIndex("max_test_record_count"))+1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            if(db != null){
                db.close();
            }
        }
        return max_test_record_count;
    }
 
    //插入历史数据
    public static void insertBattTestDatainf(DBHelper dbHelper,BattDataThread battData){
        Concentrator_State state = battData.state;
        String batt_inf_sql_str = "INSERT INTO tb_batttestdata_inf" +
                         "(BattGroupId,test_record_count,test_type,record_time_interval,record_num,test_starttime,record_time,test_timelong,group_vol,test_curr,test_cap,max_monnum,max_monvol,min_monnum,min_monvol) " +
                         " VALUES("+battData.battIndex+","+state.test_record_count+","+state.test_type+","+saveDataIntever+","+state.record_num+",'"+Com.getDateTimeFormat(state.testStartTime,Com.DTF_YMDhms)+"','"+Com.getDateTimeFormat(state.record_time,Com.DTF_YMDhms)+"',"+state.testTimelong+","+state.getGroupvol()+","+state.getGroupcurr()+","+0+","+state.max_monnum+","+state.max_monvol+","+state.min_monnum+","+state.min_monvol+") ";
 
        String batt_id_sql_str="INSERT INTO tb_batttestdata_"+battData.battIndex+"(BattGroupId,test_record_count,test_type,test_starttime,record_time,test_timelong,group_vol,test_curr,test_cap,mon_num,mon_vol,mon_tmp,mon_res) " +
                "VALUES";
        for(int i=0;i<state.monCount;i++){
            if(i != 0){
                batt_id_sql_str += ",";
            }
            batt_id_sql_str += "("+battData.battIndex+","+state.test_record_count+","+state.test_type+",'"+Com.getDateTimeFormat(state.testStartTime,Com.DTF_YMDhms)+"','"+Com.getDateTimeFormat(state.record_time,Com.DTF_YMDhms)+"',"+state.testTimelong+","+state.getGroupvol()+","+state.getGroupcurr()+","+0+","+(i+1)+","+state.mon_vols[i]+","+state.mon_tmps[i]+","+state.mon_ress[i]+")";
        }
        SQLiteDatabase db = null;
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();
            db.execSQL(batt_inf_sql_str);
            db.execSQL(batt_id_sql_str);
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(db != null){
                try {
                    db.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    //插入历史数据
    public static void insertBattTestData(DBHelper dbHelper,BattDataThread battData){
        Concentrator_State state = battData.state;
        String batt_inf_sql_str = " UPDATE tb_batttestdata_inf SET record_num ="+state.record_num+" ,record_time='"+Com.getDateTimeFormat(state.record_time,Com.DTF_YMDhms)+"',test_timelong="+state.testTimelong+",group_vol="+state.getGroupvol()+",test_curr="+state.getGroupcurr()+",test_cap=0,max_monnum="+state.max_monnum+",max_monvol="+state.max_monvol+",min_monnum="+state.min_monnum+",min_monvol="+state.min_monvol+" WHERE BattGroupId = "+battData.battIndex+" AND test_record_count = "+state.test_record_count;
 
        String batt_id_sql_str="INSERT INTO tb_batttestdata_"+battData.battIndex+"(BattGroupId,test_record_count,test_type,test_starttime,record_time,test_timelong,group_vol,test_curr,test_cap,mon_num,mon_vol,mon_tmp,mon_res) " +
                "VALUES";
        for(int i=0;i<state.monCount;i++){
            if(i != 0){
                batt_id_sql_str += ",";
            }
            batt_id_sql_str += "("+battData.battIndex+","+state.test_record_count+","+state.test_type+",'"+Com.getDateTimeFormat(state.testStartTime,Com.DTF_YMDhms)+"','"+Com.getDateTimeFormat(state.record_time,Com.DTF_YMDhms)+"',"+state.testTimelong+","+state.getGroupvol()+","+state.getGroupcurr()+","+0+","+(i+1)+","+state.mon_vols[i]+","+state.mon_tmps[i]+","+state.mon_ress[i]+")";
        }
        SQLiteDatabase db = null;
        try {
            db = dbHelper.getWritableDatabase();
            db.beginTransaction();
            db.execSQL(batt_inf_sql_str);
            db.execSQL(batt_id_sql_str);
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(db != null){
                try {
                    db.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}