whyclj
2020-05-19 6fbf87a8e692b8eaed6ade2ce867f3a9fa12a8e2
src/com/dec/fbs9100/BattRealDataPro_Thread.java
@@ -1,133 +1,134 @@
package com.dec.fbs9100;
import java.sql.SQLException;
import java.util.Date;
import com.base.AppConfig;
import com.base.Com;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
import com.battdata_rt.BattStatData;
/**
 * 电池组历史实时数据记录线程
 * @author DELL
 *
 */
public class BattRealDataPro_Thread extends Thread{
   public MysqlConnPool con_pool;
   public AppConfig cfg;
   public BattData_RT_Array data;
   public static int MinRecordTimeLong = 60;         //每次记录的最短时间间隔
   public BattRealDataPro_Thread(MysqlConnPool con_pool, AppConfig cfg, BattData_RT_Array data) {
      this.cfg = cfg;
      this.con_pool = con_pool;
      this.data = data;
   }
   @Override
   public void run() {
      System.out.println("BattRealDataPro_Thread Start at " + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ));
      Date lastTime = new Date();
      //线程启动前休眠60秒                     //用于甄别还未通讯上的设备
      for(int i =0;i<60;i++) {
         try {
            sleep(1000);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
      System.out.println("BattRealDataPro_Thread Start  Record......");
      while(true) {
         Date nowTime = new Date();
         long timelong =(nowTime.getTime()-lastTime.getTime())/1000;
         try {
            if(timelong < MinRecordTimeLong) {
               continue;
            }
            boolean isRecorded = false;
            for(int i=0;i<data.getItemCount();i++) {
               BattData_RT battData = data.getItem(i);
               if(battData.getBattState() == BattStatData.BATTSTATE_NULL) {
                  continue;            //消除未连接过的电池组
               }
               isRecorded = true;
               //new Thread() {
               //   public void run() {
                     RecordBattReadData(con_pool, battData);                     //记录电池组的放电记录
               //   }
               //}.start();
               sleep(50);
            }
            if(isRecorded) {
               lastTime = nowTime;
            }
            sleep(300);
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }
   /**
    * 记录电池组历史实时记录
    * @param pool
    * @param battData
    */
   public void RecordBattReadData(MysqlConnPool pool,BattData_RT battData) {
      //创建指定电池组的电池组历史实时数据记录表
      FBS9100_Task_Thread_SQL.CreateTb_BattRealDataTable(con_pool, battData.BattGroupId);
      String sql_str_delete = " DELETE FROM "+ Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId +" WHERE recrod_time < DATE_SUB(NOW(),INTERVAL 1 YEAR) ";            //删除一年前的历史记录
      String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId+"(BattGroupId,recrod_time,group_vol,online_vol,group_curr,group_tmp,batt_state,batt_test_type,batt_test_starttime,batt_test_tlong,batt_test_cap,mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res,mon_JH_curr)  VALUES" ;
      Date dt_test_Start = new Date();
      Date dt_record = new Date();
      dt_test_Start.setTime(battData.getTestStartTime());
      dt_record.setTime(battData.getTestRecordTime());
      for(int i=0;i<battData.MonCount;i++) {
         if(i > 0){
            sql_str += ",";
         }
         sql_str+="("
               + battData.BattGroupId+","
               + "'" + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ) + "',"
               + battData.getGroupVol() +","
               + battData.getOnlineVol() +","
               + battData.getGroupCurr() +","
               + battData.getGroupTmp() +","
               + battData.getBattState() +","
               + battData.getBattTestType() +","
               + "'" + Com.getDateTimeFormat(dt_test_Start, Com.DTF_YMDhms) + "',"
               + battData.getTestTimeLong() +","
               + battData.getTestCap() +","
               + (i+1) +","
               + battData.al_MonVol.get(i).monVol +","
               + battData.al_MonVol.get(i).monTmp +","
               + battData.al_MonVol.get(i).monRes +","
               + battData.al_MonVol.get(i).monSer +","
               + battData.al_MonVol.get(i).connRes +","
               + battData.al_MonVol.get(i).mon_JH_curr
               +")";
      }
      Sql_Mysql sql = new Sql_Mysql(con_pool.getConn());
      try {
         sql.sqlMysqlExecute(sql_str);
         //System.out.println("开始记录电池组数据");
         sql.sqlMysqlExecute(sql_str_delete);
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
}
package com.dec.fbs9100;
import java.sql.SQLException;
import java.util.Date;
import com.base.AppConfig;
import com.base.Com;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
import com.battdata_rt.BattStatData;
/**
 * 电池组历史实时数据记录线程
 * @author DELL
 *
 */
public class BattRealDataPro_Thread extends Thread{
   public MysqlConnPool con_pool;
   public AppConfig cfg;
   public BattData_RT_Array data;
   public static int MinRecordTimeLong = 60;         //每次记录的最短时间间隔
   public BattRealDataPro_Thread(MysqlConnPool con_pool, AppConfig cfg, BattData_RT_Array data) {
      this.cfg = cfg;
      this.con_pool = con_pool;
      this.data = data;
   }
   @Override
   public void run() {
      System.out.println("BattRealDataPro_Thread Start at " + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ));
      Date lastTime = new Date();
      //线程启动前休眠60秒                     //用于甄别还未通讯上的设备
      for(int i =0;i<60;i++) {
         try {
            sleep(1000);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
      System.out.println("BattRealDataPro_Thread Start  Record......");
      Date nowTime = null;
      while(true) {
         nowTime = new Date();
         long timelong =(nowTime.getTime()-lastTime.getTime())/1000;
         try {
            if(timelong < MinRecordTimeLong) {
               continue;
            }
            boolean isRecorded = false;
            for(int i=0;i < data.getItemCount();i++) {
               BattData_RT battData = data.getItem(i);
               if(battData.getBattState() == BattStatData.BATTSTATE_NULL) {
                  continue;            //消除未连接过的电池组
               }
               isRecorded = true;
               //new Thread() {
               //   public void run() {
                     RecordBattReadData(con_pool, battData);                     //记录电池组的放电记录
               //   }
               //}.start();
               sleep(50);
            }
            if(isRecorded) {
               lastTime = nowTime;
            }
            sleep(500);
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
   }
   /**
    * 记录电池组历史实时记录
    * @param pool
    * @param battData
    */
   public void RecordBattReadData(MysqlConnPool pool,BattData_RT battData) {
      //创建指定电池组的电池组历史实时数据记录表
      FBS9100_Task_Thread_SQL.CreateTb_BattRealDataTable(con_pool, battData.BattGroupId);
      String sql_str_delete = " DELETE FROM "+ Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId +" WHERE recrod_time < DATE_SUB(NOW(),INTERVAL 1 YEAR) ";            //删除一年前的历史记录
      String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId+"(BattGroupId,recrod_time,group_vol,online_vol,group_curr,group_tmp,batt_state,batt_test_type,batt_test_starttime,batt_test_tlong,batt_test_cap,mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res,mon_JH_curr)  VALUES" ;
      Date dt_test_Start = new Date();
      Date dt_record = new Date();
      dt_test_Start.setTime(battData.getTestStartTime());
      dt_record.setTime(battData.getTestRecordTime());
      for(int i=0;i<battData.MonCount;i++) {
         if(i > 0){
            sql_str += ",";
         }
         sql_str+="("
               + battData.BattGroupId+","
               + "'" + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ) + "',"
               + battData.getGroupVol() +","
               + battData.getOnlineVol() +","
               + battData.getGroupCurr() +","
               + battData.getGroupTmp() +","
               + battData.getBattState() +","
               + battData.getBattTestType() +","
               + "'" + Com.getDateTimeFormat(dt_test_Start, Com.DTF_YMDhms) + "',"
               + battData.getTestTimeLong() +","
               + battData.getTestCap() +","
               + (i+1) +","
               + battData.al_MonVol.get(i).monVol +","
               + battData.al_MonVol.get(i).monTmp +","
               + battData.al_MonVol.get(i).monRes +","
               + battData.al_MonVol.get(i).monSer +","
               + battData.al_MonVol.get(i).connRes +","
               + battData.al_MonVol.get(i).mon_JH_curr
               +")";
      }
      Sql_Mysql sql = new Sql_Mysql(con_pool.getConn());
      try {
         sql.sqlMysqlExecute(sql_str);
         //System.out.println("开始记录电池组数据");
         sql.sqlMysqlExecute(sql_str_delete);            //删除当前电池组一年前的历史数据
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         sql.close_con();
      }
   }
}