| | |
| | | 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.sqlite_DaoHelper.SqliteHelper;
|
| | | 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();
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | 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){ |
| | | Log.e(TAG, "run: 开始记录历史数据。。。。。" ); |
| | | 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(); |
| | | |
| | | } |
| | | 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 || !isRecording){ |
| | | lastRecordTime = new Date(); |
| | | //放电结束或者记录时间到 |
| | | //记录当前的充放电记录 |
| | | insertBattTestData(dbHelper,battData); |
| | | } |
| | | if(!isRecording && testTimelong < minTestTimeLong){ |
| | | //测试时长小于1分钟的充放电记录删除 |
| | | deleteInvalidTestData(dbHelper,battData); |
| | | } |
| | | battData.state.record_num++; |
| | | isStartRecord = false; |
| | | }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(3000); |
| | | } 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,record_num,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+","+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+","+(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,record_num,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+","+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+","+(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(); |
| | | // } |
| | | // } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除历史指定电池组的指定充放电记录 |
| | | * @param dbHelper |
| | | * @param battData |
| | | */ |
| | | public static void deleteInvalidTestData(DBHelper dbHelper,BattDataThread battData){ |
| | | Concentrator_State state = battData.state; |
| | | //删除tb_batttestdata_inf表中的记录 |
| | | String sql_delbatttDataInf_str = "DELETE FROM tb_batttestdata_inf WHERE BattGroupId = "+battData.battIndex+" AND test_record_count = "+state.test_record_count; |
| | | //删除tb_batttestdata_id表中的记录 |
| | | String sql_delbatttDataId_str = "DELETE FROM tb_batttestdata_"+battData.battIndex+" WHERE test_record_count= "+state.test_record_count; |
| | | SQLiteDatabase db = null; |
| | | try { |
| | | db = dbHelper.getWritableDatabase(); |
| | | db.beginTransaction(); |
| | | db.execSQL(sql_delbatttDataInf_str); |
| | | db.execSQL(sql_delbatttDataId_str); |
| | | db.setTransactionSuccessful(); |
| | | db.endTransaction(); |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Log.e(TAG, "deleteInvalidTestData:删除测试时长小于1分钟的放电记录 "); |
| | | } |
| | | } |