Some more refactoring and initial implementation of sensor Interface
Former-commit-id: 8e05cf50495dd54ba95d3a4ed10f0374cb2c428a
This commit is contained in:
parent
70f9bb1681
commit
56106a178d
6 changed files with 128 additions and 18 deletions
|
|
@ -13,6 +13,7 @@ import java.util.logging.Logger;
|
||||||
import se.koc.hal.HalContext;
|
import se.koc.hal.HalContext;
|
||||||
import se.koc.hal.intf.HalDaemon;
|
import se.koc.hal.intf.HalDaemon;
|
||||||
import se.koc.hal.struct.HalSensor;
|
import se.koc.hal.struct.HalSensor;
|
||||||
|
import se.koc.hal.struct.HalSensor.AggregationMethod;
|
||||||
import se.koc.hal.util.TimeUtility;
|
import se.koc.hal.util.TimeUtility;
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
import zutil.db.SQLResultHandler;
|
import zutil.db.SQLResultHandler;
|
||||||
|
|
@ -22,11 +23,6 @@ import zutil.log.LogUtil;
|
||||||
public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
private enum AggregationMethod{
|
|
||||||
SUM,
|
|
||||||
AVG
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initiate(Timer timer){
|
public void initiate(Timer timer){
|
||||||
timer.schedule(this, 0, TimeUtility.FIVE_MINUTES_IN_MS);
|
timer.schedule(this, 0, TimeUtility.FIVE_MINUTES_IN_MS);
|
||||||
}
|
}
|
||||||
|
|
@ -49,13 +45,13 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
||||||
logger.fine("The sensor is of type: " + sensor.getType());
|
logger.fine("The sensor is of type: " + sensor.getType());
|
||||||
if(sensor.getType().equals("PowerMeter")){
|
if(sensor.getType().equals("PowerMeter")){
|
||||||
logger.fine("aggregating raw data to five minute periods");
|
logger.fine("aggregating raw data to five minute periods");
|
||||||
aggregateRawData(sensor.getId(), TimeUtility.FIVE_MINUTES_IN_MS, 5, AggregationMethod.SUM);
|
aggregateRawData(sensor.getId(), TimeUtility.FIVE_MINUTES_IN_MS, 5, sensor.getAggregationMethod());
|
||||||
logger.fine("aggregating five minute periods into hour periods");
|
logger.fine("aggregating five minute periods into hour periods");
|
||||||
aggrigateAggregatedData(sensor.getId(), TimeUtility.FIVE_MINUTES_IN_MS, TimeUtility.HOUR_IN_MS, 12, AggregationMethod.SUM);
|
aggrigateAggregatedData(sensor.getId(), TimeUtility.FIVE_MINUTES_IN_MS, TimeUtility.HOUR_IN_MS, 12, sensor.getAggregationMethod());
|
||||||
logger.fine("aggregating one hour periods into one day periods");
|
logger.fine("aggregating one hour periods into one day periods");
|
||||||
aggrigateAggregatedData(sensor.getId(), TimeUtility.HOUR_IN_MS, TimeUtility.DAY_IN_MS, 24, AggregationMethod.SUM);
|
aggrigateAggregatedData(sensor.getId(), TimeUtility.HOUR_IN_MS, TimeUtility.DAY_IN_MS, 24, sensor.getAggregationMethod());
|
||||||
}else{
|
}else{
|
||||||
logger.fine("The sensor type is not supported by the aggregation deamon. Ignoring");
|
logger.fine("The sensor type is not supported by the aggregation daemon. Ignoring");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,7 +166,7 @@ public class DataAggregatorDaemon extends TimerTask implements HalDaemon {
|
||||||
float data = -1;
|
float data = -1;
|
||||||
switch(aggrMethod){
|
switch(aggrMethod){
|
||||||
case SUM: data = sum; break;
|
case SUM: data = sum; break;
|
||||||
case AVG: data = sum/samples; break;
|
case AVERAGE: data = sum/samples; break;
|
||||||
}
|
}
|
||||||
logger.finer("Calculated day period: " + currentPeriodTimestamp + ", data: " + sum + ", confidence: " + aggrConfidence + ", samples: " + samples + ", aggrMethod: " + aggrMethod);
|
logger.finer("Calculated day period: " + currentPeriodTimestamp + ", data: " + sum + ", confidence: " + aggrConfidence + ", samples: " + samples + ", aggrMethod: " + aggrMethod);
|
||||||
preparedInsertStmt.setInt(1, result.getInt("sensor_id"));
|
preparedInsertStmt.setInt(1, result.getInt("sensor_id"));
|
||||||
|
|
|
||||||
36
src/se/koc/hal/intf/HalEventController.java
Executable file
36
src/se/koc/hal/intf/HalEventController.java
Executable file
|
|
@ -0,0 +1,36 @@
|
||||||
|
package se.koc.hal.intf;
|
||||||
|
|
||||||
|
import se.koc.hal.struct.HalEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ziver on 2015-12-15.
|
||||||
|
*/
|
||||||
|
public interface HalEventController {
|
||||||
|
/**
|
||||||
|
* Will register an event type to be handled by this controller
|
||||||
|
*/
|
||||||
|
public void register(HalEvent event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deregisters an event from this controller, the controller
|
||||||
|
* will no longer handle that type of event
|
||||||
|
*/
|
||||||
|
public void deregister(HalEvent event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param event transmit this event if possible.
|
||||||
|
*/
|
||||||
|
public void send(HalEvent event);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the number of registered objects
|
||||||
|
*/
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close any resources associated with this controller.
|
||||||
|
* This method could be called multiple times, first time
|
||||||
|
* should be handled as normal any subsequent calls should be ignored.
|
||||||
|
*/
|
||||||
|
public void close();
|
||||||
|
}
|
||||||
32
src/se/koc/hal/intf/HalSensorController.java
Executable file
32
src/se/koc/hal/intf/HalSensorController.java
Executable file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package se.koc.hal.intf;
|
||||||
|
|
||||||
|
import se.koc.hal.struct.HalSensor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ziver on 2015-12-15.
|
||||||
|
*/
|
||||||
|
public interface HalSensorController {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will register a sensor type to be handled by this controller
|
||||||
|
*/
|
||||||
|
public void register(HalSensor sensor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deregisters a sensor from this controller, the controller
|
||||||
|
* will no longer handle that type of sensor
|
||||||
|
*/
|
||||||
|
public void deregister(HalSensor sensor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the number of registered objects
|
||||||
|
*/
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close any resources associated with this controller.
|
||||||
|
* This method could be called multiple times, first time
|
||||||
|
* should be handled as normal any subsequent calls should be ignored.
|
||||||
|
*/
|
||||||
|
public void close();
|
||||||
|
}
|
||||||
|
|
@ -46,7 +46,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
||||||
sensor.setName(request.get("name"));
|
sensor.setName(request.get("name"));
|
||||||
sensor.setType(request.get("type"));
|
sensor.setType(request.get("type"));
|
||||||
sensor.setConfig(request.get("config"));
|
sensor.setConfig(request.get("config"));
|
||||||
sensor.setUserId(localUser);
|
sensor.setUser(localUser);
|
||||||
sensor.setSynced(true);
|
sensor.setSynced(true);
|
||||||
sensor.save(db);
|
sensor.save(db);
|
||||||
case "modify_local_sensor":
|
case "modify_local_sensor":
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,37 @@
|
||||||
package se.koc.hal.struct;
|
package se.koc.hal.struct;
|
||||||
|
|
||||||
|
import se.koc.hal.intf.HalEventController;
|
||||||
|
import zutil.db.DBConnection;
|
||||||
|
import zutil.db.bean.DBBean;
|
||||||
|
import zutil.db.bean.DBBeanSQLResultHandler;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Ziver on 2015-12-15.
|
* Created by Ziver on 2015-12-15.
|
||||||
*/
|
*/
|
||||||
public class HalEvent {
|
@DBBean.DBTable("event")
|
||||||
|
public class HalEvent extends DBBean{
|
||||||
|
// Event specific data
|
||||||
|
private String name;
|
||||||
|
private String type;
|
||||||
|
private String config;
|
||||||
|
|
||||||
|
// User configuration
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static List<HalEvent> getEvents(DBConnection db) throws SQLException {
|
||||||
|
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM event" );
|
||||||
|
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(HalEvent.class, db) );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public HalEventController getController(){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import se.koc.hal.HalContext;
|
import se.koc.hal.HalContext;
|
||||||
|
import se.koc.hal.intf.HalSensorController;
|
||||||
import zutil.db.DBConnection;
|
import zutil.db.DBConnection;
|
||||||
import zutil.db.bean.DBBean;
|
import zutil.db.bean.DBBean;
|
||||||
import zutil.db.bean.DBBeanSQLResultHandler;
|
import zutil.db.bean.DBBeanSQLResultHandler;
|
||||||
|
|
@ -12,13 +13,19 @@ import zutil.db.handler.SimpleSQLResult;
|
||||||
|
|
||||||
@DBBean.DBTable("sensor")
|
@DBBean.DBTable("sensor")
|
||||||
public class HalSensor extends DBBean{
|
public class HalSensor extends DBBean{
|
||||||
|
public enum AggregationMethod{
|
||||||
|
SUM,
|
||||||
|
AVERAGE
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sensor specific data
|
// Sensor specific data
|
||||||
private String name;
|
private String name;
|
||||||
private String type;
|
private String type;
|
||||||
private String config;
|
private String config;
|
||||||
|
|
||||||
// User configuration
|
// User configuration
|
||||||
private long user_id;
|
private User user;
|
||||||
private long external_id = -1;
|
private long external_id = -1;
|
||||||
/** local sensor= if sensor should be public. external sensor= if sensor should be requested from host **/
|
/** local sensor= if sensor should be public. external sensor= if sensor should be requested from host **/
|
||||||
private boolean sync = false;
|
private boolean sync = false;
|
||||||
|
|
@ -79,11 +86,11 @@ public class HalSensor extends DBBean{
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getUserId() {
|
public User getUser() {
|
||||||
return user_id;
|
return user;
|
||||||
}
|
}
|
||||||
public void setUserId(User user) {
|
public void setUser(User user) {
|
||||||
this.user_id = user.getId();
|
this.user = user;
|
||||||
}
|
}
|
||||||
public long getExternalId() {
|
public long getExternalId() {
|
||||||
return external_id;
|
return external_id;
|
||||||
|
|
@ -97,4 +104,13 @@ public class HalSensor extends DBBean{
|
||||||
public void setSynced(boolean synced) {
|
public void setSynced(boolean synced) {
|
||||||
this.sync = synced;
|
this.sync = synced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public AggregationMethod getAggregationMethod(){
|
||||||
|
return AggregationMethod.SUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HalSensorController getController(){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue