whyclj
2019-11-23 d4c7dbd51fa93b0dae3d55f68518a12bb20ed79c
platforms/android/app/src/main/java/com/socket/BattTestDataRecordThread.java
@@ -7,6 +7,7 @@
import com.concentrator.Concentrator_State;
import com.fgkj.dao.DBHelper;
import com.sqlite_DaoHelper.SqliteHelper;
import com.util.Com;
import java.util.Date;
@@ -39,24 +40,67 @@
        Log.e(TAG,"run:BattTestDataRecordThread start at " + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms));
        int nowTestType = Concentrator_State.WORKSTATE_FLOAT;           //当前记录的测试类型(当前电池状态)
        int nowTestType = BattDataThread.TESTTYPE_NULL;                 //当前记录的测试类型
        int nowworkState = Concentrator_State.WORKSTATE_FLOAT;          //当前电池组的状态
        Date lastRecordTime = new Date();                               //上一次记录的时间
        boolean isStartRecord = false;                                  //是否是新的放电记录
        int record_num = 0;                                             //记录笔数
        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(2000);
                sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
@@ -90,10 +134,72 @@
    }
    //插入历史数据
    public static void insertBattTestDatainf(DBHelper dbHelper,Concentrator_State state){
        String sql_str = "";
    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();
                }
            }
        }
    }
}