-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDriver.java
345 lines (306 loc) · 10.8 KB
/
Driver.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import java.sql.*;
import java.io.*;
import java.util.Scanner;
import java.util.Calendar;
public class Driver {
public static void main(String[] args) throws IOException {
//open the scanner so that we can read in the file to be inserted into the database;
Scanner pv = new Scanner (new BufferedReader(new FileReader(new File( args[0]))));
Scanner BMSR1_temp = new Scanner (new BufferedReader(new FileReader(new File(args[1]))));
Scanner BMSR1_humid = new Scanner (new BufferedReader(new FileReader(new File(args[2]))));
Scanner BMSR1_heat = new Scanner (new BufferedReader(new FileReader(new File(args[3]))));
Scanner BMSR2_temp = new Scanner (new BufferedReader(new FileReader(new File(args[4]))));
Scanner BMSR2_humid = new Scanner (new BufferedReader(new FileReader(new File(args[5]))));
Scanner BMSR2_heat = new Scanner (new BufferedReader(new FileReader(new File(args[6]))));
// variables that are used by jdbc to connect to our database;
String url = "jdbc:mysql://localhost:8889/CSV_DB" ;
String username = "root";
String password = "root";
//store the current year.
int year = Calendar.getInstance().get(Calendar.YEAR);
try{
//establish connection to database.
Connection myConn = DriverManager.getConnection(url , username , password);
System.out.println("Beginning Insertion into Database");
System.out.print("-----------------------\n");
insertToDB_PV(myConn,pv,args[0]);
insertToDB_BMS_R1(myConn,BMSR1_temp,BMSR1_humid,BMSR1_heat,year);
insertToDB_BMS_R2(myConn,BMSR2_temp,BMSR2_humid,BMSR2_heat,year);
System.out.println("Insertion into the Database Complete");
System.out.print("-----------------------\n");
System.out.println("Beginning Printing from BMS Database");
System.out.print("-----------------------\n");
printFromDB_BMS_Combined(myConn, 1);
printFromDB_BMS_Combined(myConn, 2);
printFromDB_PV(myConn);
System.out.println("Printing from BMS Database Complete");
System.out.print("-----------------------\n");
}
catch (Exception exc){
exc.printStackTrace();
}
//close all scanners and file writers.
BMSR1_heat.close();
BMSR1_humid.close();
BMSR1_temp.close();
BMSR2_heat.close();
BMSR2_humid.close();
BMSR2_temp.close();
pv.close();
}
//used for inserting into the PV table in the database
static void insertToDB_PV(Connection myConn,Scanner in1, String filename){
try{
String line,value1,value2,date,timeStamp;
String insertDB = "INSERT INTO PV"
+"(TimeStamp, Date, PAC, Temperature) VALUES"
+ "(?,?,?,?)";
PreparedStatement myStmt = myConn.prepareStatement(insertDB);
date = date_parse(filename);
while(in1.hasNextLine()){
line = in1.nextLine();
String[] results = line.split(",");
timeStamp = results[0];
value1 = results[3];
value2 = results[2];
myStmt.setString(1, timeStamp);
myStmt.setString(2, date);
myStmt.setString(3, value1);
myStmt.setString(4, value2);
myStmt.executeUpdate();
}
}catch(Exception x){
x.printStackTrace();
}
}
//used for inserting into the BMS-R1 table in the database
static void insertToDB_BMS_R1(Connection myConn,Scanner in1,Scanner in2,Scanner in3,int year){
try{
String line1,line2,line3, date, timeStamp;
String value1,value2,value3;
String insertDB = "INSERT INTO BMSR1"
+"(TimeStamp, Date, Temperature, RelativeHumidity, CO_2, SensibleHeat) VALUES"
+ "(?,?,?,?,?,?)";
//String updateDB = "update BMSR1";
PreparedStatement myStmt = myConn.prepareStatement(insertDB);
line1 = in1.nextLine();
line2 = in2.nextLine();
line3 = in3.nextLine();
while(in1.hasNextLine()){
line1 = in1.nextLine();
line2 = in2.nextLine();
line3 = in3.nextLine();
String[] results1 = line1.split(",");
String[] results2 = line2.split(",");
String[] results3 = line3.split(",");
results1[5] = time_adjustment(results1[5]);
date = time_parse(results1[2]) + "-" + results1[3] + "-" + String.valueOf(year) ;
timeStamp = results1[5] + ":" + time_parse(results1[6]);
value1 = results1[10];
value2 = results2[10];
value3 = results3[10];
myStmt.setString(1,timeStamp);
myStmt.setString(2,date);
myStmt.setString(3,value1);
myStmt.setString(4,value2);
myStmt.setString(5, "0.0");
myStmt.setString(6,value3);
myStmt.executeUpdate();
}
}catch(Exception r1){
r1.printStackTrace();
}
}
//used for inserting into the BMS-R2 table in the database
static void insertToDB_BMS_R2(Connection myConn, Scanner in1, Scanner in2, Scanner in3, int year){
try{
String line1,line2,line3, date, timeStamp;
String value1,value2,value3;
String insertDB = "INSERT INTO BMSR2"
+"(TimeStamp, Date, Temperature, RelativeHumidity, CO_2, SensibleHeat) VALUES"
+ "(?,?,?,?,?,?)";
PreparedStatement myStmt = myConn.prepareStatement(insertDB);
line1 = in1.nextLine();
line2 = in2.nextLine();
line3 = in3.nextLine();
while(in1.hasNextLine()){
line1 = in1.nextLine();
line2 = in2.nextLine();
line3 = in3.nextLine();
String[] results1 = line1.split(",");
String[] results2 = line2.split(",");
String[] results3 = line3.split(",");
results1[5] = time_adjustment(results1[5]);
date = results1[2] + "-" + results1[3] + "-" + String.valueOf(year) ;
timeStamp = results1[5] + ":" + time_parse(results1[6]);
value1 = results1[10];
value2 = results2[10];
value3 = results3[10];
myStmt.setString(1,timeStamp);
myStmt.setString(2,date);
myStmt.setString(3,value1);
myStmt.setString(4,value2);
myStmt.setString(5, "0.0");
myStmt.setString(6,value3);
myStmt.executeUpdate();
}
}catch(Exception r1){
r1.printStackTrace();
}
}
//used to print all data from the PV table into a file specified by the program
static void printFromDB_PV(Connection myConn){
//create a statement.
try{
Statement pvStmt = myConn.createStatement();
BufferedWriter out = new BufferedWriter (new FileWriter("dataC.csv"));
//make and SQL query for the PV table in the database.
ResultSet pvResults = pvStmt.executeQuery("select * from PV");
//write the columns of the database before the data is written out
out.write("TimeStamp"
+ "," + "Date"
+ "," + "Pac"
+ "," + "Temperature"
+ "\n");
//process the results from the pv table in the database
while(pvResults.next()){
out.write(pvResults.getString("TimeStamp")
+ "," + pvResults.getString("Date")
+ "," + pvResults.getString("Pac")
+ "," + pvResults.getString("Temperature")
+ "\n");
}
out.flush();
out.close();
}catch(Exception x){
x.printStackTrace();
}
}
static void printFromDB_BMS_Combined(Connection myConn, int roomNum){
try{
Statement pvStmt = myConn.createStatement();
ResultSet pvResults = null;
BufferedWriter out = null;
switch (roomNum){
case 1:
out = new BufferedWriter (new FileWriter("BMSOne.csv"));
break;
case 2:
out = new BufferedWriter (new FileWriter("BMSTwo.csv"));
break;
}
//pick query based on room number
switch (roomNum){
case 1:
pvResults = pvStmt.executeQuery("select * from BMSR1");
break;
case 2:
pvResults = pvStmt.executeQuery("select * from BMSR2");
break;
}
//write the column names
out.write("TimeStamp"
+ "," + "Date"
+ "," + "Temperature"
+ "," + "RelativeHumidity"
+ "," + "COtwo"
+ "," + "SensibleHeat"
+ "\n");
//process the data from database in BMS
while(pvResults.next()){
out.write(pvResults.getString("TimeStamp")
+ "," + pvResults.getString("Date")
+ "," + pvResults.getString("Temperature")
+ "," + pvResults.getString("RelativeHumidity")
+ "," + pvResults.getString("CO_2")
+ "," + pvResults.getString("SensibleHeat")
+ "\n");
}
out.flush();
out.close();
}catch(Exception x){
x.printStackTrace();
}
}
//used to print all data from the BMS table into a file specified by the program
static void printFromDB_BMS(Connection myConn, int roomNum){
try{
Statement bmsStmt = myConn.createStatement();
//create an array that represents each column in the database
String[] column = {"Temperature", "Relative-Humidity", "CO_2", "Sensible-Heat"};
String[] columnName = {"Temperature", "RelativeHumidity", "COtwo", "SensibleHeat"};
//create a file and write data for date, time, and one of the columns.
for(int i = 0; i < column.length; i++ ){
//select database based on roomNum
ResultSet bmsResults = bmsStmt.executeQuery("select * from bmsr" + roomNum);
//filen vbxcame is based off of room number and column name.
BufferedWriter out = null;
switch (roomNum){
case 1:
out = new BufferedWriter (new FileWriter("BMSOne" + columnName[i] + ".csv"));
break;
case 2:
out = new BufferedWriter (new FileWriter("BMSTwo" + columnName[i] + ".csv"));
break;
}
//write first row with names of columns
out.write("TimeStamp"
+ "," + "Date"
+ "," + columnName[i]
+ "\n");
//write data from database of timestamo, date, and the designated column
while(bmsResults.next()){
out.write(bmsResults.getString("TimeStamp")
+ "," + bmsResults.getString("Date")
+ "," + bmsResults.getString(column[i])
+ "\n");
}
out.flush();
out.close();
}
}catch(Exception x){
x.printStackTrace();
}
}
//used to adjust time to military time.
static String time_adjustment(String hour){
String hour1 = hour.replaceAll("\\s","");
int time = Integer.valueOf(hour1);
time = time - 1;
hour1 = String.valueOf(time);
return hour1;
}
//used to get rid of any excess white space and/or digits.
static String time_parse(String minute){
String[] space = minute.split("\\.");
String result = space[0].replaceAll("\\s","");
if(result.equals("0") || result.equals("5")){
String special = "0" + result;
return special;
}
return result;
}
//parses the date from the pv file name
static String date_parse(String date){
String[] something = date.split("\\.");
String[] something1 = something[0].split("_");
String[] something2 = something1[1].split("-");
String something3 = something2[1] + "-" + something2[2] + "-" + something2[0];
return something3;
}
//deletes all data from the databases.
static void delete(Connection myConn){
try{
Statement myStmt = myConn.createStatement();
String delete = "delete from BMSR1 where Date = ' 7-29-2015'";
String delete1 = "delete from BMSR2 where Date = ' 7-29-2015'";
String delete2 = "delete from PV where Date = '05-01.csv-2015'";
System.out.println("----------Deletion complete-----------");
myStmt.executeUpdate(delete);
myStmt.executeUpdate(delete1);
myStmt.executeUpdate(delete2);
}catch(Exception x){
x.printStackTrace();
}
}
}