package com.dev.fbs9009;
|
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.Date;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import com.base.Com;
|
import com.battdata_rt.BattData_RT;
|
import com.battdata_rt.BattData_RT_Array;
|
import com.battdata_rt.BattStatData;
|
import com.config.AppConfig;
|
import com.sql.MysqlConnPool;
|
import com.sql.Sql_Mysql;
|
|
public class BattResStorePro_Thread extends Thread {
|
|
private BattData_RT_Array m_Data;
|
private MysqlConnPool m_Conn_Pool;
|
private Logger logger;
|
//private int battResStoreDelayForTestManual = 1200;
|
//private int mSaveBattResDayInterval = 7;
|
|
//private Main_Com_Data m_Main_Com_Data = null;
|
|
public BattResStorePro_Thread(MysqlConnPool pool, AppConfig cfg, BattData_RT_Array data)
|
{
|
m_Conn_Pool = pool;
|
m_Data = data;
|
logger = LogManager.getLogger(getClass());
|
|
//battResStoreDelayForTestManual = cfg.getBattResStoreDelayForTestManual();
|
//mSaveBattResDayInterval = cfg.getBattResStoreDayInterval();
|
|
//m_Main_Com_Data = Com_Data;
|
}
|
|
/**
|
* ´´½¨¼°Ìí¼ÓÊý¾Ýµ½tb_battresdata_id±í£»Ìí¼ÓÊý¾Ý£¬ÒÔ¼°¸üÐÂtb_battresdata_infÊý¾Ý±í£¬²¢ÇÒÏò±íÖÐÌí¼ÓÊý¾Ý
|
* @param conn_pool
|
* @param data_rt
|
* @param test_type
|
*/
|
public static void insertDataToResTable(MysqlConnPool conn_pool, BattData_RT data_rt, int test_type)
|
{
|
int mon_count = data_rt.MonCount;
|
if(mon_count < 1)
|
return;
|
|
Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
|
String sql_str = null;
|
try {
|
sql.sqlMysqlUseDB(Sql_Mysql.DB_BATT_TESTDATA);
|
|
boolean result = true;
|
Date rc_time = new Date();
|
|
String tb_name = "tb_BattResData_" + data_rt.BattGroupId;
|
if(false == sql.sqlMysqlCheckIfTableExist(tb_name))
|
{
|
sql_str = "CREATE TABLE IF NOT EXISTS " + tb_name + " "
|
+ " ( `num` BIGINT NOT NULL AUTO_INCREMENT, "
|
+ "`BattGroupId` INT NOT NULL DEFAULT 0, "
|
+ "`test_record_count` INT NOT NULL DEFAULT 0, "
|
+ "`test_type` INT NOT NULL DEFAULT 0, "
|
+ "`data_new` BOOLEAN NOT NULL DEFAULT false, "
|
+ "`data_available` BOOLEAN NOT NULL DEFAULT false, "
|
+ "`test_starttime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', "
|
+ "`group_vol` FLOAT NOT NULL DEFAULT 0, "
|
+ "`test_curr` FLOAT NOT NULL DEFAULT 0, "
|
+ "`mon_num` INT NOT NULL DEFAULT 0, "
|
+ "`mon_vol` FLOAT NOT NULL DEFAULT 0, "
|
+ "`mon_tmp` FLOAT NOT NULL DEFAULT 0, "
|
+ "`mon_res` FLOAT NOT NULL DEFAULT 0, "
|
+ "`mon_ser` FLOAT NOT NULL DEFAULT 0, "
|
+ "`conn_res` FLOAT NOT NULL DEFAULT 0, "
|
+ "INDEX index_test_record_count (`test_record_count`), "
|
+ "PRIMARY KEY (`num`));";
|
//System.out.println(sql_str);
|
sql.sqlMysqlExecute(sql_str);
|
}
|
|
int mtest_record_count = sql.getBattTestRecordCountNew(data_rt.BattGroupId, Sql_Mysql.BattResDataInf_Table);
|
|
for(int c=0; c<3; c++)
|
{
|
try {
|
sql_str = "INSERT INTO " + tb_name + " "
|
+ " (BattGroupId, "
|
+ "test_record_count, "
|
+ "test_type, "
|
+ "data_new, "
|
+ "data_available, "
|
+ "test_starttime, "
|
+ "group_vol, "
|
+ "test_curr, "
|
+ "mon_num, "
|
+ "mon_vol,"
|
+ "mon_tmp,"
|
+ "mon_res,"
|
+ "mon_ser,"
|
+ "conn_res)"
|
+ " VALUES ";
|
try {
|
for(int n=0; n<mon_count; n++) {
|
String tmp_sql_str = "("
|
+ data_rt.BattGroupId + ", "
|
+ mtest_record_count + ", "
|
+ test_type + ", "
|
+ true + ", "
|
+ true + ", "
|
+ "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
|
+ data_rt.mTestData.groupVol + ", "
|
+ data_rt.mTestData.testCurr + ", "
|
+ ((n+1)*10 + 1) + ", "
|
+ data_rt.al_MonVol.get(n).monVol + ","
|
+ data_rt.al_MonVol.get(n).monTmp + ","
|
+ data_rt.al_MonVol.get(n).monRes + ","
|
+ data_rt.al_MonVol.get(n).monSer + ","
|
+ data_rt.al_MonVol.get(n).connRes
|
+ ")";
|
if(n > 0) {
|
sql_str += ", ";
|
}
|
sql_str += tmp_sql_str;
|
}
|
} catch (Exception e) {
|
System.err.println("BattResStorePro_Thread.insertDataToResTable():" + e.getMessage());
|
}
|
|
String sql_str2 = "INSERT INTO " + Sql_Mysql.BattResDataInf_Table
|
+ " (BattGroupId, "
|
+ "test_record_count, "
|
+ "test_type, "
|
+ "data_new, "
|
+ "data_available, "
|
+ "test_starttime, "
|
+ "test_devtype, "
|
+ "record_time, "
|
+ "group_vol, "
|
+ "test_curr, "
|
+ "upload_usr_id) "
|
+ " VALUES "
|
+ "("+ data_rt.BattGroupId + ", "
|
+ mtest_record_count + ", "
|
+ test_type + ", "
|
+ true + ", "
|
+ true + ", "
|
+ "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
|
+ 0 + ", "
|
+ "'" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "', "
|
+ 0 + ", "
|
+ 0 + ", "
|
+ 0 + ") ";
|
if(1 == mtest_record_count) {
|
sql_str2 = "UPDATE " + Sql_Mysql.BattResDataInf_Table + " SET "
|
+ "test_type=" + test_type + ", "
|
+ "data_new=" + true + ", "
|
+ "data_available=" + true + ", "
|
+ "test_starttime='" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
|
+ "test_devtype=" + 0 + ", "
|
+ "record_time='" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "', "
|
+ "group_vol=" + 0 + ", "
|
+ "test_curr=" + 0 + ", "
|
+ "upload_usr_id=" + 0 + " "
|
+ " WHERE BattGroupId=" + data_rt.BattGroupId
|
+ " AND test_record_count=" + mtest_record_count;
|
}
|
//System.out.println(sql_str);
|
//System.out.println(sql_str2);
|
sql.mysql_con.setAutoCommit(false);
|
sql.sqlMysqlExecute(sql_str);
|
sql.sqlMysqlExecute(sql_str2);
|
sql.mysql_con.commit();
|
} catch (SQLException e) {
|
sql.mysql_con.rollback();
|
//System.out.println(sql_str);
|
e.printStackTrace();
|
result = false;
|
} finally {
|
sql.mysql_con.setAutoCommit(true);
|
if(true == result)
|
{
|
break;
|
}
|
else
|
{
|
try {
|
Thread.sleep(500);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
} catch (SQLException e) {
|
System.out.println(e.getMessage());
|
}
|
finally
|
{
|
sql.close_con();
|
}
|
}
|
//-----------------------------------------------------------------------------//
|
@Override
|
public void run() {
|
|
logger.info(this.getName() + " - BattResStorePro_Thread Started ...");
|
|
try {
|
for(int s=0; s<6; s++)
|
{
|
sleep(1000);
|
}
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
|
while(true) {
|
try {
|
sleep(2000);
|
|
for(int n=0; n<m_Data.getItemCount(); n++) {
|
BattData_RT rt_data = m_Data.getItem(n);
|
if(((rt_data.FBSDeviceId/100000) == 4019)) {
|
get_MonData_From_BattData_RT_RamDB_Table(m_Conn_Pool, rt_data);
|
get_ResTestState_From_LD9State_RT_RamDB_Table(m_Conn_Pool, rt_data);
|
}
|
|
if(0x00 == rt_data.mTestData.batt_res_test_state) {
|
try {
|
if(rt_data.al_MonVol.get(0).monRes > 0) {
|
if(rt_data.mTestData.batt_res_test_cnt_last != rt_data.mTestData.batt_res_test_cnt) {
|
rt_data.mTestData.batt_res_test_cnt_last = rt_data.mTestData.batt_res_test_cnt;
|
if(rt_data.mTestData.batt_res_test_cnt > 0) {
|
rt_data.setStoreBattRes_DataEnStat(true);
|
}
|
}
|
}
|
} catch (Exception e) {
|
logger.error("BattResStorePro_Thread.run():" + e.getMessage());
|
}
|
}
|
|
if(true == rt_data.getStoreBattRes_DataEnStat()) {
|
logger.info(this.getName() + " - BattResStorePro_Thread: "
|
+ "dev_id:" + rt_data.FBSDeviceId + " bg_id:" + rt_data.BattGroupId
|
+ " Insert Batt Res Data Into DB - "
|
+ Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
|
insertDataToResTable(m_Conn_Pool, rt_data, BattStatData.BATTDATA_RES);
|
rt_data.setStoreBattRes_DataEnStat(false);
|
}
|
}
|
//------------------------------------------------------------------------//
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
/**
|
* ´ÓÊý¾Ý¿âÖвéѯµ±Ç°µç³Ø×éµÄµ¥ÌåÊý¾Ý
|
* @param conn_pool
|
* @param bd_rt
|
*/
|
public static void get_MonData_From_BattData_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt)
|
{
|
Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
|
try {
|
String sql_str = "SELECT mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res FROM " + Sql_Mysql.BattRtData_Table
|
+ " WHERE BattGroupId=" + bd_rt.BattGroupId
|
+ " ORDER BY mon_num ASC";
|
//System.out.println(sql_str);
|
ResultSet res = sql.sqlMysqlQuery(sql_str);
|
try {
|
while(res.next()) {
|
int mon_index = res.getInt("mon_num");
|
if(mon_index > 0) {
|
mon_index -= 1;
|
}
|
if(mon_index < bd_rt.al_MonVol.size()) {
|
bd_rt.al_MonVol.get(mon_index).monVol = res.getFloat("mon_vol");
|
bd_rt.al_MonVol.get(mon_index).monTmp = res.getFloat("mon_tmp");
|
bd_rt.al_MonVol.get(mon_index).monRes = res.getFloat("mon_res");
|
bd_rt.al_MonVol.get(mon_index).monSer = res.getFloat("mon_ser");
|
bd_rt.al_MonVol.get(mon_index).connRes = res.getFloat("mon_conn_res");
|
}
|
}
|
} catch (Exception e) {
|
System.err.println("InsertDataToMysql_Task_SQL.get_MonData_From_BattData_RT_RamDB_Table():" + e.getMessage());
|
}
|
res.close();
|
} catch (SQLException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
|
|
/**
|
* ²éѯÉ豸µÄÄÚ×è²âÊÔ״̬¼°ÄÚ×è²âÊÔ´ÎÊý
|
* @param conn_pool
|
* @param bd_rt
|
*/
|
public static void get_ResTestState_From_LD9State_RT_RamDB_Table(MysqlConnPool conn_pool, BattData_RT bd_rt)
|
{
|
Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
|
ResultSet res = null;
|
try {
|
String sql_str = "SELECT systemState,resTestCount FROM " + Sql_Mysql.LD9_ConcentratorState_Table
|
+ " WHERE dev_id=" + bd_rt.FBSDeviceId;
|
//System.out.println(sql_str);
|
res = sql.sqlMysqlQuery(sql_str);
|
if(res.next()) {
|
bd_rt.mTestData.batt_res_test_state = res.getInt("systemState");
|
bd_rt.mTestData.batt_res_test_cnt = res.getInt("resTestCount");
|
}
|
|
} catch (SQLException e) {
|
if(res != null) {
|
try {
|
res.close();
|
} catch (SQLException e1) {
|
e1.printStackTrace();
|
}
|
}
|
e.printStackTrace();
|
} finally {
|
|
sql.close_con();
|
}
|
}
|
}
|