whyclj
2020-09-22 f7bc10472ba2dcc3f28910d4221a796fb50861ff
修改历史实时数据记录线程按天记录
3个文件已修改
143 ■■■■ 已修改文件
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electric_4KW_HistoryData_Thread.java 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electrical_4KW_SocketClient_Thread.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electrical_Task_SQL.java 65 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electric_4KW_HistoryData_Thread.java
@@ -1,5 +1,6 @@
package com.electrical.FourKW;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@@ -9,7 +10,9 @@
import com.sql.MysqlConnPool;
public class Electric_4KW_HistoryData_Thread implements Runnable{
    public static final int SaveDataTimeInterval = 3;
    public static final int SaveDataTimeInterval = 10;                //记录间隔
    public static final int MAX_SAVE_DAY_COUNT = 30;                //保留数据天数
    
    public MysqlConnPool pool;
    public AppConfig config;
@@ -25,23 +28,25 @@
    
    @Override
    public void run() {
        System.out.println("Electric_4KW_HistoryData_Thread Start at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
        for(int i = 0;i<elects.size();i++) {
            Electric_Record_Thread thread = new Electric_Record_Thread(elects.get(i), pool, config);
            threads.put(elects.get(i).electric_id, thread);
            new Thread(thread).start();
        }
        Date now = new Date();
        System.out.println("Electric_4KW_HistoryData_Thread Start at " + Com.getDateTimeFormat(now, Com.DTF_YMDhms));
        while(true) {
            try {
                Thread.sleep(10000);
                for(int i = 0;i<elects.size();i++) {
                    Electric_Record_Thread thread = threads.get(elects.get(i).electric_id);
                    if(thread == null) {
                        thread = new Electric_Record_Thread(elects.get(i), pool, config);
                        threads.put(elects.get(i).electric_id, thread);
                        new Thread(thread).start();
                    }
                    }
                    now = new Date();
                    Electrical_Task_SQL.createTb_Electric_Readhist_DataTable(pool, elects.get(i).electric_id, now);
                    Electrical_Task_SQL.createTb_Electric_Readhist_DataTable(pool, elects.get(i).electric_id, tomorrow(now));
                }
                Thread.sleep(10000);
                Date del_time = getDateBefore(now,MAX_SAVE_DAY_COUNT);
                Electrical_Task_SQL.deleteHistoryData(pool, del_time);
            } catch (Exception e) {
                try {
                    Thread.sleep(5000);
@@ -58,32 +63,30 @@
        public Electric_inf elect;
        public MysqlConnPool pool;
        public AppConfig cfg;
        public Electric_Rt rt;
        
        public Electric_Record_Thread(Electric_inf elect,MysqlConnPool pool,AppConfig cfg) {
            this.elect = elect;
            this.pool = pool;
            this.cfg = cfg;
            this.rt = new Electric_Rt(elect.electric_id);
        }
        @Override
        public void run() {        
            //创建历史数据表
            Electrical_Task_SQL.createTb_Electric_Readhist_DataTable(pool,elect.electric_id);
            Date now = null;
            Date last = new Date(0);
            while(true) {
                try {
                    //System.out.println(elect.lose_conn+"##########");
                    Thread.sleep(SaveDataTimeInterval*1000);
                    if(elect.lose_conn) {
                        //断开连接的停止记录
                        continue;
                    now = new Date();
                    long timelong = (now.getTime() - last.getTime())/1000;
                    if(timelong >= SaveDataTimeInterval) {
                        if(!elect.lose_conn) {
                            Electrical_Task_SQL.recordHistoryData(pool, elect.rt, now);
                            last = now;
                        }
                    }
                    //获取当前状态值
                    Electrical_Task_SQL.queryElectric_RtById(rt,pool);
                    Electrical_Task_SQL.recordHistoryData(pool,rt);
                    Thread.sleep(500);
                } catch (Exception e) {
                    try {
                        Thread.sleep(3000);
@@ -95,4 +98,29 @@
            }
        }
    }
    /**
     *     返回明天日期
     * @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();
    }
}
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electrical_4KW_SocketClient_Thread.java
@@ -104,9 +104,11 @@
                Electrical_Task_SQL.insertOrUpdateDeviceConnectState(conn_pool, einf.electric_id, master);
                
                //判断当前设备是否断开连接
                if(master.getTotalerr() >3) {
                    einf.lose_conn = true;
                if(master.getConnectState() == 0) {
                    //通讯中断
                    einf.lose_conn = true;
                }else {
                    //通讯正常
                    einf.lose_conn = false;                    
                }
                
ElectricalSystem_MonitorServer_4KW/src/com/electrical/FourKW/Electrical_Task_SQL.java
@@ -808,8 +808,8 @@
     * @param pool
     * @param electric_id
     */
    public static void createTb_Electric_Readhist_DataTable(MysqlConnPool pool,int electric_id) {
        String sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Tb_Electric_Readhist_Data + electric_id + " (" +
    public static void createTb_Electric_Readhist_DataTable(MysqlConnPool pool,int electric_id,Date time) {
        String sql_str = "CREATE TABLE IF NOT EXISTS " + Sql_Mysql.Tb_Electric_Readhist_Data + electric_id+"_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D) + " (" +
                "  num bigint(20) NOT NULL AUTO_INCREMENT," + 
                "  electric_id int(11) NOT NULL DEFAULT '0' COMMENT '电源id'," + 
                "  record_time datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '记录时间'," + 
@@ -895,6 +895,24 @@
        }
    }
    
    /**
     *     记录历史实时数据
     * @param pool
     * @param rt
     */
    public static void recordHistoryData(MysqlConnPool pool, Electric_Rt rt,Date time) {
        String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Electric_Readhist_Data + rt.electric_id+ "_" + Com.getDateTimeFormat(time, Com.DTF_Y_M_D) + "(electric_id,record_time,dcvol,dccurr,controlangle) VALUES("+rt.electric_id+",'"+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+"',"+rt.dcvol+","+rt.dccurr+","+rt.controlangle+");";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    /**
     *     插入或更新当前设备的连接状态
     * @param pool
@@ -927,4 +945,47 @@
            }
        }
    }
    /**
     * 删除创建时间超时的历史实时记录表
     * @param conn_pool
     * @param deldate
     */
    public static void deleteHistoryData(MysqlConnPool conn_pool,Date deldate) {
        String sql_select_strs = " select TABLE_NAME,UPDATE_TIME,CREATE_TIME " +
                                 " from information_schema.tables " +
                                 " where table_schema='db_electricsystem' " +
                                 " AND TABLE_NAME like 'tb_electric_realhist_data_%' " +
                                 " AND CREATE_TIME <= '"+Com.getDateTimeFormat(deldate, Com.DTF_YMDhms)+"';" ;
        String sql_delete_strs = " DROP TABLE IF EXISTS ";
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        ResultSet res = null;
        int count = 0;
        res = sql.sqlMysqlQuery(sql_select_strs);
        try {
            while(res.next()) {
                if(count > 0) {
                    sql_delete_strs += ",";
                }
                sql_delete_strs += "db_electricsystem." + res.getString("TABLE_NAME");
                System.out.println("删除:"+res.getString("TABLE_NAME")+"\t at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                count++;
            }
            if(count >0) {
                sql.sqlMysqlExecute(sql_delete_strs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(res != null) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
}