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