package com.data.comm;
|
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
import com.data.Motor_inf;
|
import com.sql.MysqlConnPool;
|
|
public class Rectifier_power_RealRecord_Thread extends Thread{
|
public static final int TESTDATA_RECORD_INTERVAL = 1; //¼Ç¼Êý¾Ý¼ä¸ô
|
public static final int MAX_SAVE_DAY_COUNT = 3*366;
|
|
private MysqlConnPool pool;
|
private Logger logger = null;
|
private List<Motor_inf> motors;
|
|
public Rectifier_power_RealRecord_Thread(MysqlConnPool pool, List<Motor_inf> motors) {
|
this.pool = pool;
|
logger = LogManager.getLogger(this.getClass());
|
this.motors = motors;
|
}
|
|
@Override
|
public void run() {
|
logger.info(" Rectifier_power_RealRecord_Thread Start ...");
|
|
Date now = null;
|
|
for(int i=0;i<motors.size();i++) {
|
RecordThread thread = new RecordThread(motors.get(i),pool);
|
new Thread(thread).start();
|
}
|
while(true) {
|
try {
|
now = new Date();
|
for(int i = 0 ; i < motors.size() ; i++) {
|
//´´½¨4MW´ó¹¦ÂʵçÔ´ÀúʷʵʱÊý¾Ý±í
|
Rectifier_power_SQL.createTb_rectifier_power_RealData(pool, motors.get(i).getDev_id(), now);
|
|
Rectifier_power_SQL.createTb_rectifier_power_RealData(pool, motors.get(i).getDev_id(), tomorrow(now));
|
|
|
|
}
|
Date deltime = getDateBefore(now,MAX_SAVE_DAY_COUNT);
|
//ɾ³ý´´½¨Ê±¼äÔÚÖ¸¶¨Ê±¼ä֮ǰ´´½¨µÄÀúʷʵʱÊý¾Ý±í
|
Rectifier_power_SQL.deleteHistoryData(pool, deltime);
|
|
|
Thread.sleep(5000);
|
} catch (Exception e) {
|
logger.error(e.toString(),e);
|
}
|
}
|
}
|
class RecordThread implements Runnable{
|
public Motor_inf motor_inf;
|
public MysqlConnPool pool;
|
public RecordThread(Motor_inf motor_inf, MysqlConnPool pool) {
|
this.motor_inf = motor_inf;
|
this.pool = pool;
|
}
|
|
@Override
|
public void run() {
|
Date last = new Date(0);
|
Date now = null;
|
while(true) {
|
try {
|
now = new Date();
|
Thread.sleep(100);
|
/*if(motor_inf.conn_state == 0) {
|
//ͨѶÖжÏ
|
continue;
|
}*/
|
if(now.getTime()-last.getTime() >= TESTDATA_RECORD_INTERVAL*1000) {
|
Rectifier_power_SQL.insertTb_rectifier_power_RealData(pool, motor_inf.rPower, now);
|
last = now;
|
}
|
} catch (Exception e) {
|
logger.error(e.toString(),e);
|
}
|
}
|
}
|
}
|
|
/**
|
* ·µ»ØÃ÷ÌìÈÕÆÚ
|
* @param today
|
* @return
|
*/
|
public Date tomorrow(Date today) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(today);
|
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
|
return calendar.getTime();
|
}
|
|
/**
|
* »ñȡָ¶¨Ê±¼ä֮ǰ¶àÉÙÌìµÄʱ¼ä
|
* @param d
|
* @param day
|
* @return
|
*/
|
public static Date getDateBefore(Date d,int day){
|
Calendar now =Calendar.getInstance();
|
now.setTime(d);
|
now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
|
return now.getTime();
|
}
|
}
|