综合管理平台数据库数据模拟程序
Administrator
2021-03-25 335a3b0a6a6f27d0f7097b27c6614c66f5946676
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
package com.thread.devs;
 
import java.util.Calendar;
import java.util.Date;
 
import com.mode.Device_Inf;
import com.mode.Manage_Util;
import com.mode.Motor_State;
import com.sql.MysqlConnPool;
 
public class Manage_Simul_Motor_Thread implements Runnable{
    public MysqlConnPool pool;
    public Device_Inf device;
    public Motor_State state;
    
    public Manage_Simul_Motor_Thread(MysqlConnPool pool,Device_Inf device){
        this.device = device;
        this.pool = pool;
        state = new Motor_State(device.getDevice_id());
    }
    
    @Override
    public void run() {
        RecordRealData real = new RecordRealData(pool, device);
        new Thread(real).start();
        
        Manage_Simul_Motor_Thread_SQL.insertOrUpdateMotor_State_Table(pool, state);
        while(true) {
            try {
                createRandomData(state);
                
                Manage_Simul_Motor_Thread_SQL.updateMotor_State_Table(pool, state);
                
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public void createRandomData(Motor_State state) {
        state.record_time = new Date();//'¼Ç¼ʱ¼ä',
        state.motor_curr = state.motor_curr + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»úµçÁ÷',
        state.motor_power = state.motor_power + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»ú¹¦ÂÊ',
        state.motor_speed = state.motor_speed + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»úתËÙ',
        state.motor_vol = state.motor_vol + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»úµçѹ',
        state.motor_out_tmp = state.motor_out_tmp + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»ú³ö¿ÚζÈ',
        state.motor_into_tmp = state.motor_into_tmp + Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»ú½ø¿ÚζÈ',
        state.motor_torque = state.motor_torque+ Manage_Util.CreateIntRandom()*Manage_Util.CreateFloat0To1();//'µç»úת¾Ø',
        state.motor_state = Manage_Util.CreateSwitchRanDom();//'µç»ú״̬',
    }
    
    public class RecordRealData implements Runnable{
        public MysqlConnPool pool;
        public Device_Inf device;
        
        
        public RecordRealData(MysqlConnPool pool,Device_Inf device) {
            this.pool = pool;
            this.device = device;
        }
 
        @Override
        public void run() {
            Date last = new Date(0,0,0);
            while(true) {
                Date now = new Date();
                try {
                    Manage_Simul_Motor_Thread_SQL.createTb_Motor_State_RealData_Table(pool, state.device_id, now);
                    Manage_Simul_Motor_Thread_SQL.createTb_Motor_State_RealData_Table(pool, state.device_id, tomorrow(now));
                    
                    if((now.getTime()-last.getTime())>=1000){
                        Manage_Simul_Motor_Thread_SQL.insertMotor_State_RealData_Table(pool, state);
                        
                        last = now;
                    }                
                    Thread.sleep(200);
                } 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();
    }
}