whyclj
2020-10-17 e163b7b3e4d5acc63cfda2fec2dd9034864ccf7a
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.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();
    }
}