whyclj
2020-10-15 95c0c2e94d390650d3954c30c6a7c805fe9c714c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.dev.base.data;
 
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 Dynamicload_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<Dynamicload_inf> dinfs;
    
    public Dynamicload_RealDataRecord_Thread(MysqlConnPool pool,List<Dynamicload_inf> dinfs){
        this.pool = pool;
        this.dinfs = dinfs;
        threads = new ArrayList<>();
    }
    
    @Override
    public void run() {
        System.out.println("Dynamicload_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<dinfs.size();i++) {
                    boolean isExist = false;
                    Dynamicload_inf dinf = dinfs.get(i);
                    for(int k=0;k<threads.size();k++) {
                        if(threads.get(k).dinf.dev_id == dinf.dev_id) {
                            isExist = true;
                            break;
                        }
                    }
                    if(!isExist) {
                        TestData_Record_Thread thread = new TestData_Record_Thread(pool, dinf);
                        threads.add(thread);
                        new Thread(thread).start();
                    }
                    
                    //¹¹Ôì½ñÌìºÍÃ÷ÌìµÄÀúʷʵʱÊý¾Ý±í
                    Dynamicload_Task_SQL.createTb_Dynamicload_realdata(pool, dinf.dev_id, now);
                    Dynamicload_Task_SQL.createTb_Dynamicload_realdata(pool, dinf.dev_id, tomorrow(now));
                
                }
                Date del_time = getDateBefore(now,MAX_SAVE_DAY_COUNT);
                Dynamicload_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 Dynamicload_inf dinf;
        
        public TestData_Record_Thread(MysqlConnPool pool,Dynamicload_inf dinf) {
            this.pool = pool;
            this.dinf = dinf;
        }
        
        @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(dinf.conn_state == 1) {
                            //ͨѶÕý³£
                            Dynamicload_Task_SQL.insertTb_dynamicload_realdata(pool, dinf, 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();
    }
}