package com.watersystem.monitor;
|
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
import com.base.Com;
|
import com.sql.MysqlConnPool;
|
|
public class WaterSystem_RealDataRecord_Thread implements Runnable{
|
public static final int TESTDATA_RECORD_INTERVAL = 1; //¼Ç¼Êý¾Ý¼ä¸ô
|
public static final int MAX_SAVE_DAY_COUNT = 180;
|
|
|
public MysqlConnPool pool;
|
public List<TestData_Record_Thread> threads;
|
public List<Water_inf> winfs;
|
|
public WaterSystem_RealDataRecord_Thread(MysqlConnPool pool,List<Water_inf> winfs){
|
this.pool = pool;
|
this.winfs = winfs;
|
threads = new ArrayList<>();
|
}
|
|
@Override
|
public void run() {
|
System.out.println("WaterSystem_RealDataRecord_Thread Start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
Date now = null;
|
Date last = new Date(0);
|
while(true) {
|
try {
|
now = new Date();
|
|
for(int i=0;i<winfs.size();i++) {
|
boolean isExist = false;
|
Water_inf winf = winfs.get(i);
|
for(int k=0;k<threads.size();k++) {
|
if(threads.get(k).winf.water_id == winf.water_id) {
|
isExist = true;
|
break;
|
}
|
}
|
if(!isExist) {
|
TestData_Record_Thread thread = new TestData_Record_Thread(pool, winf);
|
threads.add(thread);
|
new Thread(thread).start();
|
}
|
|
//¹¹Ôì½ñÌìºÍÃ÷ÌìµÄÀúʷʵʱÊý¾Ý±í
|
Water_Task_SQL.createTb_Water_realdata(pool,winf.water_id, now);
|
Water_Task_SQL.createTb_Water_realdata(pool,winf.water_id, tomorrow(now));
|
|
}
|
Date del_time = getDateBefore(now,MAX_SAVE_DAY_COUNT);
|
Water_Task_SQL.deleteHistoryData(pool, del_time);
|
//System.out.println(Com.getDateTimeFormat(del_time, Com.DTF_YMDhms));
|
Thread.sleep(3000);
|
} catch (Exception e) {
|
try {
|
Thread.sleep(5000);
|
} catch (InterruptedException e1) {
|
e1.printStackTrace();
|
}
|
e.printStackTrace();
|
}
|
}
|
|
}
|
|
class TestData_Record_Thread implements Runnable{
|
public MysqlConnPool pool;
|
public Water_inf winf;
|
|
public TestData_Record_Thread(MysqlConnPool pool,Water_inf winf) {
|
this.pool = pool;
|
this.winf = winf;
|
}
|
|
@Override
|
public void run() {
|
Date last = new Date(0); //ÉÏÒ»´Î²âÊÔµÄʱ¼ä
|
Date now = null; //µ±Ç°Ê±¼ä
|
|
while(true) {
|
try {
|
now = new Date();
|
long timelong = (now.getTime()-last.getTime())/1000;
|
//System.out.println(timelong);
|
if(timelong >= TESTDATA_RECORD_INTERVAL) {
|
if(winf.conn_state == 1) {
|
//ͨѶÕý³£
|
Water_Task_SQL.insertTb_water_realdata(pool, winf, now);
|
last = now;
|
}
|
}
|
Thread.sleep(10);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
/**
|
* ·µ»ØÃ÷ÌìÈÕÆÚ
|
* @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();
|
}
|
}
|