whyclxw
2020-12-29 1f73540edbda5eca9da22ecd84bf134bd5523203
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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();
    }
}