Statistics
| Revision:

root / hci / trunk / EneraptorSim3 / EneraptorSim.java @ 83

History | View | Annotate | Download (3.46 KB)

1
import java.sql.*;
2
import java.io.*;
3
import java.util.*;
4

    
5
public class EneraptorSim {
6

    
7
        public static int state = 0;
8

    
9
        public static void main(String[] args) throws Exception {
10

    
11
                DatabaseFiller dbf = new DatabaseFiller();
12
                dbf.start();
13

    
14
                System.out.println("\nEneraptor DMS is turned OFF.");
15
                Scanner in = new Scanner(System.in);
16
                
17
                String input = "";
18
                
19
                while(!input.equals("turn on")) {
20
                        System.out.print("> ");
21
                        input = in.nextLine();
22
                }
23
                
24
                state = 1;
25
                
26
                while(true) { }
27
                
28
        }
29
        
30
        public static class DatabaseFiller extends Thread {
31
        
32
                public void run() {
33
                
34
                        try {
35
                                doWhatYouMust();
36
                        } catch (Exception e) {
37
                                System.exit(1);
38
                        }
39
                
40
                }
41
                        
42
                public void doWhatYouMust() throws Exception {        
43
                        
44
                        Class.forName("org.postgresql.Driver");
45

    
46
                        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/eneraptor-db-dev", "eneraptor-hci", "eneraptor-hci-2010");
47

    
48
                        // CONFIGURE
49
                        
50
                        double val = 24;
51
                        double thrust = 0.1;
52
                        int step = 0;
53
                        
54
                        while(true) {
55
                        
56
                                java.util.Date now = new java.util.Date();
57
                                
58
                                if(EneraptorSim.state < 3) {
59
                                
60
                                        double a = Math.random();
61
                                        if(a < 0.05)
62
                                                val += 0.1;
63
                                        else if(a > 0.95)
64
                                                val -= 0.1;
65
                                                
66
                                } else if (EneraptorSim.state == 3) {
67
                                        
68
                                        val = val+thrust;
69
                                        thrust += 0.01;
70
                                        
71
                                } else if (EneraptorSim.state == 4) {
72
                                        
73
                                        val = val+thrust;
74
                                        thrust -= 0.05;
75
                                
76
                                } else if (EneraptorSim.state == 5) {
77
                                
78
                                        val = val+thrust;
79
                                        thrust += 0.05;
80
                                
81
                                }
82
                                
83
                                
84
                                Statement st = conn.createStatement();
85
                                String stm = "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + now + "', 'thermometer01', '" + val + "');";
86
                                st.execute(stm);
87

    
88
                                if(EneraptorSim.state == 1) {
89
                                
90
                                        stm = "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + now + "', 'airConditioner01', 'AIR CONDITIONER: TURN OFF');";
91
                                        st.execute(stm);
92
                                        
93
                                        EneraptorSim.state = 2;
94
                                        
95
                                } else if(EneraptorSim.state == 2) {
96
                                
97
                                        stm = "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + now + "', 'windowOpener01', 'WINDOW OPENER: OPEN');";
98
                                        st.execute(stm);
99
                                        
100
                                        EneraptorSim.state = 3;
101
                                
102
                                } else if(EneraptorSim.state == 3) {
103
                                
104
                                        if(val > 26) EneraptorSim.state = 4;
105
                                
106
                                } else if(EneraptorSim.state == 4) {
107
                                
108
                                        if(val < 20 && thrust < -1) {
109
                                        
110
                                        
111
                                                EneraptorSim.state = 5;
112
                                                stm = "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + now + "', 'windowOpener01', 'WINDOW OPENER: CLOSE');";
113
                                                st.execute(stm);
114
                                        
115
                                        }
116
                                
117
                                }  else if(EneraptorSim.state == 5) {
118
                                
119
                                        if(val > 15 && thrust > 0.5) {
120
                                        
121
                                                EneraptorSim.state = 4;
122
                                                stm = "INSERT INTO logged_data(id, \"version\", date_recieved, device_id, reported_data) VALUES (nextval('hibernate_sequence'), 0, '" + now + "', 'windowOpener01', 'WINDOW OPENER: OPEN');";
123
                                                st.execute(stm);
124
                                        
125
                                        }
126
                                
127
                                }
128
                                
129
                                
130
                                st.close();
131
                                step++;
132
                                
133
                                try {
134
                                        Thread.currentThread().sleep(1000);
135
                                }
136
                                catch (InterruptedException e) {
137
                                        e.printStackTrace();
138
                                }
139
                        }
140
                        
141
                }
142
        }
143
        
144
        
145
}