Changed DB access to a getter
Former-commit-id: 0eb743932c1bcd1e4fe9eba1d8485a54ae0b9db6
This commit is contained in:
parent
8cd81766a0
commit
fdc9183c0c
10 changed files with 25 additions and 17 deletions
1
.gitignore
vendored
Normal file → Executable file
1
.gitignore
vendored
Normal file → Executable file
|
|
@ -1,2 +1,3 @@
|
|||
/hal.db
|
||||
/build/
|
||||
/lib/Zutil.jar
|
||||
|
|
|
|||
BIN
hal-default.db
Executable file
BIN
hal-default.db
Executable file
Binary file not shown.
BIN
hal.db
BIN
hal.db
Binary file not shown.
|
|
@ -10,7 +10,7 @@ public class HalContext {
|
|||
private static final String CONF_FILE = "hal.conf";
|
||||
|
||||
// Variables
|
||||
public static DBConnection db;
|
||||
private static DBConnection db;
|
||||
|
||||
public static Properties conf;
|
||||
private static Properties defaultConf;
|
||||
|
|
@ -39,4 +39,9 @@ public class HalContext {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static DBConnection getDB(){
|
||||
return db;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
List<Sensor> sensorList = Sensor.getLocalSensors(HalContext.db);
|
||||
List<Sensor> sensorList = Sensor.getLocalSensors(HalContext.getDB());
|
||||
for(Sensor sensor : sensorList){
|
||||
logger.fine("Aggregating sensor_id: " + sensor.getId());
|
||||
aggregateSensor(sensor.getId());
|
||||
|
|
@ -48,7 +48,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
}
|
||||
|
||||
public void aggregateSensor(long sensorId) {
|
||||
DBConnection db = HalContext.db;
|
||||
DBConnection db = HalContext.getDB();
|
||||
try {
|
||||
Long maxDBTimestamp = db.exec("SELECT MAX(timestamp_end) FROM sensor_data_aggr WHERE sensor_id == "+sensorId, new SimpleSQLHandler<Long>());
|
||||
if(maxDBTimestamp == null)
|
||||
|
|
@ -124,7 +124,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
if(currentPeriodTimestamp != 0 && periodTimestamp != currentPeriodTimestamp){
|
||||
float confidence = Math.min(count / 5f, 1.0f);
|
||||
logger.finer("Calculated minute period: "+ currentPeriodTimestamp +" sum: "+ sum +" confidence: "+ confidence);
|
||||
HalContext.db.exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
HalContext.getDB().exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
result.getInt("sensor_id"),
|
||||
Sensor.getHighestSequenceId(result.getInt("sensor_id")) + 1,
|
||||
currentPeriodTimestamp,
|
||||
|
|
@ -156,7 +156,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
if(currentPeriodTimestamp != 0 && periodTimestamp != currentPeriodTimestamp){
|
||||
float aggrConfidence = confidenceSum / 12f;
|
||||
logger.finer("Calculated hour period: "+ currentPeriodTimestamp +" sum: "+ sum +" confidence: "+ aggrConfidence);
|
||||
HalContext.db.exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
HalContext.getDB().exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
result.getInt("sensor_id"),
|
||||
Sensor.getHighestSequenceId(result.getInt("sensor_id")) + 1,
|
||||
currentPeriodTimestamp,
|
||||
|
|
@ -173,7 +173,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
confidenceSum += result.getFloat("confidence");
|
||||
|
||||
//TODO: SHould not be here!
|
||||
HalContext.db.exec("DELETE FROM sensor_data_aggr "
|
||||
HalContext.getDB().exec("DELETE FROM sensor_data_aggr "
|
||||
+ "WHERE sensor_id == "+ result.getInt("sensor_id") +" AND sequence_id == "+ result.getInt("sequence_id"));
|
||||
}
|
||||
return null;
|
||||
|
|
@ -193,7 +193,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
if(currentPeriodTimestamp != 0 && periodTimestamp != currentPeriodTimestamp){
|
||||
float aggrConfidence = confidenceSum / 24f;
|
||||
logger.finer("Calculated day period: "+ currentPeriodTimestamp +" sum: "+ sum +" confidence: "+ aggrConfidence+ " samples: " + samples);
|
||||
HalContext.db.exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
HalContext.getDB().exec(String.format(Locale.US, "INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(%d, %d, %d, %d, %d, %f)",
|
||||
result.getInt("sensor_id"),
|
||||
Sensor.getHighestSequenceId(result.getInt("sensor_id")) + 1,
|
||||
currentPeriodTimestamp,
|
||||
|
|
@ -212,7 +212,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
|||
samples++;
|
||||
|
||||
//TODO: SHould not be here!
|
||||
HalContext.db.exec("DELETE FROM sensor_data_aggr "
|
||||
HalContext.getDB().exec("DELETE FROM sensor_data_aggr "
|
||||
+ "WHERE sensor_id == "+ result.getInt("sensor_id") +" AND sequence_id == "+ result.getInt("sequence_id"));
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ public class DataSynchronizationClient extends TimerTask implements HalDaemon{
|
|||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
DBConnection db = HalContext.db;
|
||||
DBConnection db = HalContext.getDB();
|
||||
List<User> users = User.getExternalUsers(db);
|
||||
for(User user : users){
|
||||
if(user.getHostname() == null){
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ public class DataSynchronizationDaemon extends ThreadedTCPNetworkServer implemen
|
|||
if(obj instanceof PeerDataReqDTO){
|
||||
PeerDataReqDTO req = (PeerDataReqDTO) obj;
|
||||
|
||||
SensorDataListDTO list = HalContext.db.exec("SELECT * FROM sensor_data_aggr WHERE sensor_id == "+ req.sensorId +" AND sequence_id > "+ req.offsetSequenceId,
|
||||
SensorDataListDTO list = HalContext.getDB().exec("SELECT * FROM sensor_data_aggr WHERE sensor_id == "+ req.sensorId +" AND sequence_id > "+ req.offsetSequenceId,
|
||||
new SQLResultHandler<SensorDataListDTO>() {
|
||||
@Override
|
||||
public SensorDataListDTO handleQueryResult(Statement stmt, ResultSet result) throws SQLException {
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public class PCConfigureHttpPage implements HttpPage {
|
|||
Map<String, String> request) throws IOException {
|
||||
|
||||
try {
|
||||
DBConnection db = HalContext.db;
|
||||
DBConnection db = HalContext.getDB();
|
||||
|
||||
Templator tmpl = new Templator(FileUtil.find("web-resource/configure.html"));
|
||||
tmpl.set("user", User.getLocalUser(db));
|
||||
|
|
|
|||
8
src/se/koc/hal/page/PCOverviewHttpPage.java
Normal file → Executable file
8
src/se/koc/hal/page/PCOverviewHttpPage.java
Normal file → Executable file
|
|
@ -9,6 +9,7 @@ import java.util.Map;
|
|||
|
||||
import se.koc.hal.HalContext;
|
||||
import se.koc.hal.deamon.DataAggregatorDaemon;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.SQLResultHandler;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.net.http.HttpHeaderParser;
|
||||
|
|
@ -22,7 +23,8 @@ public class PCOverviewHttpPage implements HttpPage {
|
|||
public void respond(HttpPrintStream out, HttpHeaderParser client_info, Map<String, Object> session, Map<String, String> cookie, Map<String, String> request) throws IOException {
|
||||
|
||||
try {
|
||||
ArrayList<PowerData> minDataList = HalContext.db.exec(
|
||||
DBConnection db = HalContext.getDB();
|
||||
ArrayList<PowerData> minDataList = db.exec(
|
||||
"SELECT user.username as username, "
|
||||
+ "sensor_data_aggr.timestamp_start as timestamp_start, "
|
||||
+ "sensor_data_aggr.timestamp_end as timestamp_end , "
|
||||
|
|
@ -36,7 +38,7 @@ public class PCOverviewHttpPage implements HttpPage {
|
|||
+ "AND timestamp_start > " + (System.currentTimeMillis() - DataAggregatorDaemon.DAY_IN_MS)
|
||||
+ "ORDER BY timestamp_start ASC",
|
||||
new SQLPowerDataBuilder());
|
||||
ArrayList<PowerData> hourDataList = HalContext.db.exec(
|
||||
ArrayList<PowerData> hourDataList = db.exec(
|
||||
"SELECT user.username as username, "
|
||||
+ "sensor_data_aggr.timestamp_start as timestamp_start, "
|
||||
+ "sensor_data_aggr.timestamp_end as timestamp_end , "
|
||||
|
|
@ -50,7 +52,7 @@ public class PCOverviewHttpPage implements HttpPage {
|
|||
+ "AND timestamp_start > " + (System.currentTimeMillis() - 3*DataAggregatorDaemon.DAY_IN_MS)
|
||||
+ "ORDER BY timestamp_start ASC",
|
||||
new SQLPowerDataBuilder());
|
||||
ArrayList<PowerData> dayDataList = HalContext.db.exec(
|
||||
ArrayList<PowerData> dayDataList = db.exec(
|
||||
"SELECT user.username as username, "
|
||||
+ "sensor_data_aggr.timestamp_start as timestamp_start, "
|
||||
+ "sensor_data_aggr.timestamp_end as timestamp_end , "
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public class Sensor extends DBBean{
|
|||
|
||||
|
||||
public static long getHighestSequenceId(long sensorId) throws SQLException{
|
||||
Integer id = HalContext.db.exec("SELECT MAX(sequence_id) FROM sensor_data_aggr WHERE sensor_id == "+ sensorId, new SimpleSQLHandler<Integer>());
|
||||
Integer id = HalContext.getDB().exec("SELECT MAX(sequence_id) FROM sensor_data_aggr WHERE sensor_id == "+ sensorId, new SimpleSQLHandler<Integer>());
|
||||
return (id != null ? id+1 : 1);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue