-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsageControl.java
More file actions
124 lines (106 loc) · 2.44 KB
/
Copy pathUsageControl.java
File metadata and controls
124 lines (106 loc) · 2.44 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
/**
*Class: UsageControl.java
*Project: Usage Visualizer
*Author: Jason Van Kerkhoven
*Date of Update: 26/10/2017
*Version: 1.0.1
*
*Purpose: Main runnable class for Usage Visualizer.
* Synchronizes everything, and manages updating the graph.
* Handles any user inputs from UI.
*
*
*Update Log v1.0.1
* - run method rewritten to use DatabaseReader v1.1.0
* v1.0.0
* - null
*/
package ctrl;
//import external libraries
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.List;
import org.sqlite.SQLiteException;
//import packages
import ui.UsageView;
import datatypes.UsageSeries;
public class UsageControl implements Runnable
{
//declaring static constants
private static final String TITLE = "Usage Visualizer v1.0.1";
private static final String GRAPH_TITLE = "Reported Power Usage";
private static final String X_AXIS = "Date";
private static final String Y_AXIS = "Power Usage (kWh)";
//declaring local instance variables
private DatabaseReader db;
private UsageView ui;
private long updatePeriod;
//generic constructor
public UsageControl(String dbPath, long updatePeriod) throws FileNotFoundException, SQLException
{
db = new DatabaseReader(dbPath);
ui = new UsageView(false, TITLE);
this.updatePeriod = updatePeriod;
}
@Override
//exactly what it says on the tin
public void run()
{
try
{
//initialize db
db.open();
db.initialize();
db.close();
//enable gui and setup
ui.updateLineTab(db.getCollection());
ui.setVisible(true);
//main loop
while(true)
{
//check for database updates
db.open();
if (db.updateSeries())
{
//update graph
ui.updateLineTab(db.getCollection());
}
db.close();
try {
Thread.sleep(updatePeriod);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
//main
public static void main(String[] args)
{
if (args.length == 2)
{
try
{
UsageControl ctrl = new UsageControl(args[0], Integer.parseInt(args[1])*1000);
ctrl.run();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
else
{
System.out.println("ERROR: Must call program with arg1=path/database.db arg2=updatePeriod");
}
}
}