forked from FRC4048/Scouting2016
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemWatcher.java
More file actions
323 lines (284 loc) · 9.28 KB
/
FileSystemWatcher.java
File metadata and controls
323 lines (284 loc) · 9.28 KB
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
/**************************************************************************************************
* Scouting0.8 : Database version
* @Author Lucas Varella
*
* This program is meant to handle input from dummy data collectors, and properly insert said info
* into the database. The GUI is limited: it merely indicates when a singular form has been
* processed, not if the program ever catches an exception. Thus, it is recommended to run this
* program from the safety of an IDE.
*
* The program utilizes a WatchService in order to monitor changes to the children of a specific
* folder. A single WatchKey waits for a change to a specified folder in an endless loop, meant to
* be terminated only by closing the program. Whenever the WatchKey register an event or group of
* events (for our purposes, it will most likely be a single event), the event is expected to be
* the creation of a file or modification of a file. As soon as the event is registered, the file
* is located and its contents are broken down into constituent forms, and forms are broken down
* into constituent items for storage into the database. Items are stored in the database one at
* a time. The connection object to the database resets every time the program saves an item.
*
* The text file must follow a specific format dictated by the tablet software.
*************************************************************************************************/
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import static java.nio.file.StandardWatchEventKinds.*;
public class FileSystemWatcher {
// SQL Database connection object
public static Connection conn;
// The current string to display in the JFrame
private static String dispString = "";
// The number of lines displayed in the JFrame
private static int lines;
private static JFrame frame;
private static JTextArea console;
public static void main(String[] args) throws IOException {
// Initiating the UI
frame = new JFrame();
// intiating the frame
frame.setTitle("Scouting File System Watcher");
frame.setLayout(null);
frame.setLocation(100,100);
frame.setSize(980,870);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
console = new JTextArea("");
console.setEditable(false);
console.setFocusable(true);
console.setLayout(null);
console.setLineWrap(true);
console.setSize(945, 805);
console.setLocation(10,10);
frame.add(console);
console.setVisible(true);
output("Ready");
// Done initializing UI
// the WatchService will continue to check for File System events until the program is closed.
// Look up WatchService for more info.
while (true)
{
WatchService watcher = FileSystems.getDefault().newWatchService();
// The main folder where changes will be monitored.
Path dir = Paths.get("E:/Users/" + System.getProperty("user.name") + "/Desktop/Shortcutz");
WatchKey key = null;
try
{
key = dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
key = watcher.take();
}
catch (IOException | InterruptedException x)
{
x.printStackTrace();
}
output("Reading a file...");
// This code will only be reached when the WatchService has found a change in the monitored folder.
// It will hold the main thread until it has found a change.
for (WatchEvent<?> event: key.pollEvents()) {
// The filename is the context of the event.
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Path filename = ev.context();
// Verify that the new file is a text file.
try {
// Resolve the filename against the directory.
// This will indicate whether a new file found is a text file.
// Checks for extraneous new files in the monitored folder.
// Look up resolving file names for more info.
Path child = dir.resolve(filename);
if (!Files.probeContentType(child).equals("text/plain")) {
String message = String.format("New file '%s'" + " is not a plain text file.%n", filename);
output(message);
}
} catch (IOException x) {
System.err.println(x);
continue;
}
// The tablets will always send all forms as a single line, to be contained in this string.
String content = "";
Scanner in = new Scanner(Paths.get("C:/Users/" + System.getProperty("user.name") + "/Desktop/Shortcutz/" + filename));
while (in.hasNext())
{
content += in.next();
}
// We do not know how many forms will be present in the file.
// The next loop will read all of the forms it finds onto this array.
String contnt[] = new String[150];
// A count of all forms present in this file.
int i = 0;
// flag
boolean done = false;
while (!done)
{
// double pipes delimit forms in the file.
int index = content.indexOf("||");
if (index == -1) done = true;
else {
contnt[i] = content.substring(0, index);
content = content.substring(index+2);
}
i++;
}
// We now know the number of forms we've read.
// This means we can get rid of all null elements of the array.
String forms[] = Arrays.copyOf(contnt, i-1);
// Now we will iterate through each item in each form
for (String form : forms)
{
if (!form.equals(""))
{
// Once again, we cannot be sure of the number of items present.
// All items will be read onto this string.
String items[] = new String[150];
done = false;
// A count of all items in the current form.
i = 0;
while (!done)
{
// Single pipes delimit items in the form.
int index = form.indexOf("|");
if (index == -1)
{
items[i] = form;
done = true;
}
else
{
items[i] = form.substring(0, index);
form = form.substring(index+1);
}
i++;
}
// We now know the number of items we've read.
// This means we can get rid of all null elements of the array.
String formItems[] = Arrays.copyOf(items, i);
try {
storeInDB(formItems);
conn.close();
} catch (SQLException ev1)
{
ev1.printStackTrace();
}
output("File read successfully.");
}
}
}
// Reset the key -- this step is critical if you want to
// receive further watch events. If the key is no longer valid,
// the directory is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid)
{
System.err.format("Directory inaccessible");
break;
}
}
} // End main
// method output() takes in a string to write it to the JFrame (the "console").
public static void output(String s)
{
dispString += s + "%n";
if(lines<48)
{
lines++;
}
else
{
int loc = dispString.indexOf("%n");
dispString = dispString.substring(loc+2);
}
console.setText(String.format(dispString));
} // End output
public static void storeInDB(String[] items) throws SQLException
{
int reportID = 0;
if (!getConnection())
{
output("DB broken!");
}
else
{
CallableStatement stmt = null;
stmt = conn.prepareCall("{call procInsertReport(?,?,?,?,?,?)}");
stmt.setInt(1, Integer.parseInt(items[4]));
stmt.setInt(2, Integer.parseInt(items[2]));
stmt.setInt(3, Integer.parseInt(items[0]));
stmt.setString(4, items[1]);
boolean bool = true;
int num = Integer.parseInt(items[3]);
if (num == 0) bool = false;
stmt.setBoolean(5, bool);
stmt.registerOutParameter(6, Types.INTEGER);
try
{
stmt.executeQuery();
reportID = stmt.getInt(6);
}
catch (SQLException e)
{
e.printStackTrace();
output("broken");
System.exit(0);
}
finally
{
if (stmt != null) { stmt.close(); }
}
int id = 0;
String val = "";
for (int i = 5; i < items.length; i++)
{
val = "";
String item[] = items[i].split(",");
id = Integer.parseInt(item[0]);
for (int j = 1; j < item.length; j++)
{
val += item[j];
}
stmt = null;
stmt = conn.prepareCall("{call procInsertRecord(?,?,?)}");
stmt.setString(1, val);
stmt.setInt(2, reportID);
stmt.setInt(3, id);
try
{
stmt.executeQuery();
}
catch (SQLException e)
{
e.printStackTrace();
output("broken");
System.exit(0);
}
}
if (stmt != null) { stmt.close(); }
}
} // End storeInDB
public static boolean getConnection()
{
boolean connected = false;
try
{
conn = DriverManager.getConnection("jdbc:mysql://LucasPC:3306/scouting?useSSL=false", "lucas", "lucas");
System.out.println("Connected to database");
connected = true;
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(0);
}
return connected;
} // End getConnection
}