Massive refactoring, new Sensor/Event class structure, controller instantiation disabled as it needs some more work
Former-commit-id: 0d2c15d63ea44c45c018fec59877a374122dacd4
This commit is contained in:
parent
fc05f82f31
commit
588ac41956
20 changed files with 232 additions and 155 deletions
|
|
@ -1,8 +1,7 @@
|
|||
package se.koc.hal;
|
||||
|
||||
import se.koc.hal.intf.HalSensorController;
|
||||
import se.koc.hal.struct.HalEvent;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.plugin.PluginData;
|
||||
import zutil.plugin.PluginManager;
|
||||
|
|
@ -26,14 +25,14 @@ public class ControllerManager {
|
|||
|
||||
|
||||
|
||||
public void register(HalSensor sensor) throws IllegalAccessException, InstantiationException {
|
||||
public void register(Sensor sensor) throws IllegalAccessException, InstantiationException {
|
||||
Class<? extends HalSensorController> c = sensor.getController();
|
||||
HalSensorController controller;
|
||||
if (controllerMap.containsKey(c))
|
||||
controller = controllerMap.get(c);
|
||||
else {
|
||||
// Instantiate controller
|
||||
logger.fine("Instantiating controller: " + c.getName());
|
||||
logger.fine("Instantiating new controller: " + c.getName());
|
||||
controller = c.newInstance();
|
||||
controllerMap.put(c, controller);
|
||||
}
|
||||
|
|
@ -41,7 +40,7 @@ public class ControllerManager {
|
|||
controller.register(sensor);
|
||||
}
|
||||
|
||||
public void deregister(HalSensor sensor){
|
||||
public void deregister(Sensor sensor){
|
||||
Class<? extends HalSensorController> c = sensor.getController();
|
||||
HalSensorController controller;
|
||||
if (controllerMap.containsKey(c)) {
|
||||
|
|
@ -70,7 +69,7 @@ public class ControllerManager {
|
|||
Iterator<PluginData> it = pluginManager.iterator();
|
||||
while (it.hasNext()){
|
||||
PluginData plugin = it.next();
|
||||
Iterator<Class<?>> pluginIt = plugin.getClassIterator(HalSensor.class);
|
||||
Iterator<Class<?>> pluginIt = plugin.getClassIterator(Sensor.class);
|
||||
while (pluginIt.hasNext()){
|
||||
manager.availableSensors.add(pluginIt.next());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import se.koc.hal.intf.HalHttpPage;
|
|||
import se.koc.hal.page.PCConfigureHttpPage;
|
||||
import se.koc.hal.page.PCHeatMapHttpPage;
|
||||
import se.koc.hal.page.PCOverviewHttpPage;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.log.CompactLogFormatter;
|
||||
|
|
@ -45,11 +45,11 @@ public class PowerChallenge {
|
|||
DBConnection db = HalContext.getDB();
|
||||
|
||||
// Init sensors and controllers
|
||||
ControllerManager.initialize();
|
||||
for(HalSensor sensor : HalSensor.getLocalSensors(db)){
|
||||
//ControllerManager.getInstance().register(sensor);
|
||||
/* ControllerManager.initialize();
|
||||
for(Sensor sensor : Sensor.getLocalSensors(db)){
|
||||
ControllerManager.getInstance().register(sensor);
|
||||
}
|
||||
|
||||
*/
|
||||
// init daemons
|
||||
daemons = new HalDaemon[]{
|
||||
new DataAggregatorDaemon(),
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ package se.koc.hal.deamon;
|
|||
|
||||
import se.koc.hal.HalContext;
|
||||
import se.koc.hal.intf.HalDaemon;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.HalSensor.AggregationMethod;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import se.koc.hal.intf.HalSensor.AggregationMethod;
|
||||
import se.koc.hal.struct.PowerConsumptionSensor;
|
||||
import se.koc.hal.util.TimeUtility;
|
||||
import zutil.db.DBConnection;
|
||||
|
|
@ -31,8 +31,8 @@ public class DataAggregatorDaemon implements HalDaemon {
|
|||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
List<HalSensor> sensorList = HalSensor.getLocalSensors(HalContext.getDB());
|
||||
for(HalSensor sensor : sensorList){
|
||||
List<Sensor> sensorList = Sensor.getLocalSensors(HalContext.getDB());
|
||||
for(Sensor sensor : sensorList){
|
||||
logger.fine("Aggregating sensor_id: " + sensor.getId());
|
||||
aggregateSensor(sensor);
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ public class DataAggregatorDaemon implements HalDaemon {
|
|||
}
|
||||
}
|
||||
|
||||
public void aggregateSensor(HalSensor sensor) {
|
||||
public void aggregateSensor(Sensor sensor) {
|
||||
//if(sensor instanceof PowerConsumptionSensor){
|
||||
logger.fine("The sensor is of type: " + PowerConsumptionSensor.class.getSimpleName());
|
||||
logger.fine("aggregating raw data to five minute periods");
|
||||
|
|
@ -61,7 +61,7 @@ public class DataAggregatorDaemon implements HalDaemon {
|
|||
* @param sensor The sensor for to aggregate data
|
||||
* @param toPeriodSizeInMs The period length in ms to aggregate to
|
||||
*/
|
||||
private void aggregateRawData(HalSensor sensor, long toPeriodSizeInMs, int expectedSampleCount){
|
||||
private void aggregateRawData(Sensor sensor, long toPeriodSizeInMs, int expectedSampleCount){
|
||||
long sensorId = sensor.getId();
|
||||
AggregationMethod aggrMethod = sensor.getAggregationMethod();
|
||||
DBConnection db = HalContext.getDB();
|
||||
|
|
@ -98,7 +98,7 @@ public class DataAggregatorDaemon implements HalDaemon {
|
|||
* @param fromPeriodSizeInMs The period length in ms to aggregate from
|
||||
* @param toPeriodSizeInMs The period length in ms to aggregate to
|
||||
*/
|
||||
private void aggrigateAggregatedData(HalSensor sensor, long fromPeriodSizeInMs, long toPeriodSizeInMs){
|
||||
private void aggrigateAggregatedData(Sensor sensor, long fromPeriodSizeInMs, long toPeriodSizeInMs){
|
||||
long sensorId = sensor.getId();
|
||||
AggregationMethod aggrMethod = sensor.getAggregationMethod();
|
||||
int expectedSampleCount = (int)Math.ceil((double)toPeriodSizeInMs / (double)fromPeriodSizeInMs);
|
||||
|
|
@ -158,7 +158,7 @@ public class DataAggregatorDaemon implements HalDaemon {
|
|||
int sum = 0;
|
||||
float confidenceSum = 0;
|
||||
int samples = 0;
|
||||
long highestSequenceId = HalSensor.getHighestSequenceId(sensorId);
|
||||
long highestSequenceId = Sensor.getHighestSequenceId(sensorId);
|
||||
PreparedStatement preparedInsertStmt = HalContext.getDB().getPreparedStatement(
|
||||
"INSERT INTO sensor_data_aggr(sensor_id, sequence_id, timestamp_start, timestamp_end, data, confidence) VALUES(?, ?, ?, ?, ?, ?)");
|
||||
while(result.next()){
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package se.koc.hal.deamon;
|
|||
|
||||
import se.koc.hal.HalContext;
|
||||
import se.koc.hal.intf.HalDaemon;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import se.koc.hal.struct.PowerConsumptionSensor;
|
||||
import se.koc.hal.util.TimeUtility;
|
||||
import zutil.db.DBConnection;
|
||||
|
|
@ -30,8 +30,8 @@ public class DataCleanupDaemon implements HalDaemon {
|
|||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
List<HalSensor> sensorList = HalSensor.getSensors(HalContext.getDB());
|
||||
for(HalSensor sensor : sensorList){
|
||||
List<Sensor> sensorList = Sensor.getSensors(HalContext.getDB());
|
||||
for(Sensor sensor : sensorList){
|
||||
logger.fine("Deleting old data for sensor id: " + sensor.getId());
|
||||
cleanupSensor(sensor);
|
||||
}
|
||||
|
|
@ -41,7 +41,7 @@ public class DataCleanupDaemon implements HalDaemon {
|
|||
}
|
||||
}
|
||||
|
||||
public void cleanupSensor(HalSensor sensor) {
|
||||
public void cleanupSensor(Sensor sensor) {
|
||||
//if(sensor instanceof PowerConsumptionSensor){
|
||||
logger.fine("The sensor is of type: " + PowerConsumptionSensor.class.getSimpleName());
|
||||
if(sensor.getUser().isExternal()){
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import se.koc.hal.HalContext;
|
|||
import se.koc.hal.deamon.DataSynchronizationDaemon.SensorDataDTO;
|
||||
import se.koc.hal.deamon.DataSynchronizationDaemon.SensorDataListDTO;
|
||||
import se.koc.hal.intf.HalDaemon;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import se.koc.hal.struct.User;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.log.LogUtil;
|
||||
|
|
@ -47,12 +47,12 @@ public class DataSynchronizationClient implements HalDaemon {
|
|||
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
|
||||
|
||||
List<HalSensor> sensors = HalSensor.getSensors(db, user);
|
||||
for(HalSensor sensor : sensors){
|
||||
List<Sensor> sensors = Sensor.getSensors(db, user);
|
||||
for(Sensor sensor : sensors){
|
||||
if(sensor.isSynced()) {
|
||||
PeerDataReqDTO req = new PeerDataReqDTO();
|
||||
req.sensorId = sensor.getExternalId();
|
||||
req.offsetSequenceId = HalSensor.getHighestSequenceId(sensor.getId());
|
||||
req.offsetSequenceId = Sensor.getHighestSequenceId(sensor.getId());
|
||||
out.writeObject(req);
|
||||
|
||||
SensorDataListDTO dataList = (SensorDataListDTO) in.readObject();
|
||||
|
|
|
|||
11
src/se/koc/hal/intf/HalEvent.java
Executable file
11
src/se/koc/hal/intf/HalEvent.java
Executable file
|
|
@ -0,0 +1,11 @@
|
|||
package se.koc.hal.intf;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-23.
|
||||
*/
|
||||
public interface HalEvent {
|
||||
|
||||
public Class<? extends HalEventController> getController();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package se.koc.hal.intf;
|
||||
|
||||
import se.koc.hal.struct.HalEvent;
|
||||
import se.koc.hal.struct.Event;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-15.
|
||||
|
|
@ -9,18 +9,18 @@ public interface HalEventController {
|
|||
/**
|
||||
* Will register an event type to be handled by this controller
|
||||
*/
|
||||
public void register(HalEvent event);
|
||||
public void register(Event event);
|
||||
|
||||
/**
|
||||
* Deregisters an event from this controller, the controller
|
||||
* will no longer handle that type of event
|
||||
*/
|
||||
public void deregister(HalEvent event);
|
||||
public void deregister(Event event);
|
||||
|
||||
/**
|
||||
* @param event transmit this event if possible.
|
||||
*/
|
||||
public void send(HalEvent event);
|
||||
public void send(Event event);
|
||||
|
||||
/**
|
||||
* @return the number of registered objects
|
||||
|
|
|
|||
18
src/se/koc/hal/intf/HalSensor.java
Executable file
18
src/se/koc/hal/intf/HalSensor.java
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
package se.koc.hal.intf;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-23.
|
||||
*/
|
||||
public interface HalSensor {
|
||||
enum AggregationMethod{
|
||||
SUM,
|
||||
AVERAGE
|
||||
}
|
||||
|
||||
|
||||
public AggregationMethod getAggregationMethod();
|
||||
|
||||
public Class<? extends HalSensorController> getController();
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package se.koc.hal.intf;
|
||||
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-15.
|
||||
|
|
@ -10,13 +10,13 @@ public interface HalSensorController {
|
|||
/**
|
||||
* Will register a sensor type to be handled by this controller
|
||||
*/
|
||||
public void register(HalSensor sensor);
|
||||
public void register(Sensor sensor);
|
||||
|
||||
/**
|
||||
* Deregisters a sensor from this controller, the controller
|
||||
* will no longer handle that type of sensor
|
||||
*/
|
||||
public void deregister(HalSensor sensor);
|
||||
public void deregister(Sensor sensor);
|
||||
|
||||
/**
|
||||
* @return the number of registered objects
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package se.koc.hal.page;
|
|||
|
||||
import se.koc.hal.HalContext;
|
||||
import se.koc.hal.intf.HalHttpPage;
|
||||
import se.koc.hal.struct.HalSensor;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import se.koc.hal.struct.User;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.io.file.FileUtil;
|
||||
|
|
@ -30,7 +30,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
if(request.containsKey("action")){
|
||||
String action = request.get("action");
|
||||
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
|
||||
HalSensor sensor;
|
||||
Sensor sensor;
|
||||
User user;
|
||||
switch(action) {
|
||||
// Local User
|
||||
|
|
@ -42,7 +42,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
|
||||
// Local Sensors
|
||||
case "create_local_sensor":
|
||||
sensor = new HalSensor();
|
||||
sensor = new Sensor();
|
||||
sensor.setName(request.get("name"));
|
||||
sensor.setType(request.get("type"));
|
||||
sensor.setConfig(request.get("config"));
|
||||
|
|
@ -50,7 +50,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
sensor.setSynced(true);
|
||||
sensor.save(db);
|
||||
case "modify_local_sensor":
|
||||
sensor = HalSensor.getSensor(db, id);
|
||||
sensor = Sensor.getSensor(db, id);
|
||||
if(sensor != null){
|
||||
sensor.setName(request.get("name"));
|
||||
sensor.setType(request.get("type"));
|
||||
|
|
@ -59,7 +59,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
}
|
||||
break;
|
||||
case "remove_local_sensor":
|
||||
sensor = HalSensor.getSensor(db, id);
|
||||
sensor = Sensor.getSensor(db, id);
|
||||
if(sensor != null)
|
||||
sensor.delete(db);
|
||||
break;
|
||||
|
|
@ -88,7 +88,7 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
|
||||
// External Sensors
|
||||
case "modify_external_sensor":
|
||||
sensor = HalSensor.getSensor(db, id);
|
||||
sensor = Sensor.getSensor(db, id);
|
||||
if(sensor != null){
|
||||
sensor.setSynced(Boolean.parseBoolean(request.get("sync")));
|
||||
sensor.save(db);
|
||||
|
|
@ -100,9 +100,9 @@ public class PCConfigureHttpPage extends HalHttpPage {
|
|||
// Output
|
||||
Templator tmpl = new Templator(FileUtil.find("web-resource/configure.tmpl"));
|
||||
tmpl.set("user", localUser);
|
||||
tmpl.set("localSensor", HalSensor.getLocalSensors(db));
|
||||
tmpl.set("localSensor", Sensor.getLocalSensors(db));
|
||||
tmpl.set("extUsers", User.getExternalUsers(db));
|
||||
tmpl.set("extSensor", HalSensor.getExternalSensors(db));
|
||||
tmpl.set("extSensor", Sensor.getExternalSensors(db));
|
||||
|
||||
return tmpl;
|
||||
|
||||
|
|
|
|||
|
|
@ -23,12 +23,15 @@
|
|||
package se.koc.hal.plugin.tellstick;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import se.koc.hal.intf.HalSensorController;
|
||||
import se.koc.hal.struct.Sensor;
|
||||
import zutil.log.InputStreamLogger;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.log.OutputStreamLogger;
|
||||
import zutil.struct.TimedHashSet;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -36,7 +39,7 @@ import java.util.logging.Logger;
|
|||
* This version of the TwoWaySerialComm example makes use of the
|
||||
* SerialPortEventListener to avoid polling.
|
||||
*/
|
||||
public class TellstickSerialComm extends Thread{
|
||||
public class TellstickSerialComm implements Runnable, HalSensorController {
|
||||
private static final long TRANSMISSION_UNIQUENESS_TTL = 300; // milliseconds
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static TellstickSerialComm instance;
|
||||
|
|
@ -50,8 +53,9 @@ public class TellstickSerialComm extends Thread{
|
|||
private TellstickChangeListener listener;
|
||||
|
||||
|
||||
public TellstickSerialComm(){
|
||||
public TellstickSerialComm() throws Exception {
|
||||
set = new TimedHashSet(TRANSMISSION_UNIQUENESS_TTL);
|
||||
connect("COM6");
|
||||
}
|
||||
|
||||
public void connect(String portName) throws Exception {
|
||||
|
|
@ -64,14 +68,29 @@ public class TellstickSerialComm extends Thread{
|
|||
|
||||
in = new BufferedReader(new InputStreamReader(new InputStreamLogger(serial.getInputStream()), "UTF-8"));
|
||||
out = new BufferedWriter(new OutputStreamWriter(new OutputStreamLogger(serial.getOutputStream()), "UTF-8"));
|
||||
this.start();
|
||||
Executors.newSingleThreadExecutor().submit(this);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if(serial != null) {
|
||||
try {
|
||||
serial.closePort();
|
||||
in.close();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
serial = null;
|
||||
in = null;
|
||||
out = null;
|
||||
}
|
||||
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
String data;
|
||||
while ((data = in.readLine()) != null) {
|
||||
while (in != null && (data = in.readLine()) != null) {
|
||||
if ((data.startsWith("+S") || data.startsWith("+T"))) {
|
||||
synchronized (this) {
|
||||
this.notifyAll();
|
||||
|
|
@ -117,15 +136,17 @@ public class TellstickSerialComm extends Thread{
|
|||
}
|
||||
|
||||
|
||||
public static TellstickSerialComm getInstance(){
|
||||
if(instance == null){
|
||||
try {
|
||||
instance = new TellstickSerialComm();
|
||||
instance.connect("COM6");
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
|
||||
@Override
|
||||
public void register(Sensor sensor) {
|
||||
|
||||
}
|
||||
@Override
|
||||
public void deregister(Sensor sensor) {
|
||||
|
||||
}
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,15 @@
|
|||
package se.koc.hal.plugin.tellstick.protocols;
|
||||
|
||||
import se.koc.hal.intf.HalSensor;
|
||||
import se.koc.hal.intf.HalSensorController;
|
||||
import se.koc.hal.plugin.tellstick.TellstickProtocol;
|
||||
import se.koc.hal.plugin.tellstick.TellstickSerialComm;
|
||||
import se.koc.hal.struct.PowerConsumptionSensor;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-11-19.
|
||||
*/
|
||||
public class Oregon0x1A2D implements TellstickProtocol {
|
||||
public class Oregon0x1A2D implements TellstickProtocol, PowerConsumptionSensor {
|
||||
|
||||
double temperature = 0;
|
||||
double humidity = 0;
|
||||
|
|
@ -74,4 +78,16 @@ public class Oregon0x1A2D implements TellstickProtocol {
|
|||
public double getHumidity(){
|
||||
return humidity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public AggregationMethod getAggregationMethod() {
|
||||
return AggregationMethod.SUM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalSensorController> getController() {
|
||||
return TellstickSerialComm.class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,10 @@
|
|||
|
||||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.intf.HalEvent;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-05-07.
|
||||
*/
|
||||
public class DimmerEvent extends HalEvent{
|
||||
public abstract class DimmerEvent implements HalEvent {
|
||||
}
|
||||
|
|
|
|||
56
src/se/koc/hal/struct/Event.java
Executable file
56
src/se/koc/hal/struct/Event.java
Executable file
|
|
@ -0,0 +1,56 @@
|
|||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.intf.HalEvent;
|
||||
import se.koc.hal.intf.HalEventController;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.bean.DBBean;
|
||||
import zutil.db.bean.DBBeanSQLResultHandler;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-15.
|
||||
*/
|
||||
@DBBean.DBTable("event")
|
||||
public class Event extends DBBean{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
// Event specific data
|
||||
private String name;
|
||||
private String type;
|
||||
private String config;
|
||||
// Event specific data
|
||||
private transient HalEvent eventData;
|
||||
|
||||
// User configuration
|
||||
private User user;
|
||||
|
||||
|
||||
|
||||
public static List<Event> getEvents(DBConnection db) throws SQLException {
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM event" );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Event.class, db) );
|
||||
}
|
||||
|
||||
private HalEvent getEventData(){
|
||||
if(eventData == null) {
|
||||
try {
|
||||
Class c = Class.forName(type);
|
||||
eventData = (HalEvent) c.newInstance();
|
||||
} catch (Exception e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
return eventData;
|
||||
}
|
||||
|
||||
|
||||
public Class<? extends HalEventController> getController(){
|
||||
return getEventData().getController();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
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.
|
||||
*/
|
||||
@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 Class<? extends HalEventController> getController(){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.intf.HalSensor;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-03.
|
||||
*/
|
||||
public class PowerConsumptionSensor extends HalSensor {
|
||||
public interface PowerConsumptionSensor extends HalSensor {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,31 @@
|
|||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.HalContext;
|
||||
import se.koc.hal.intf.HalEvent;
|
||||
import se.koc.hal.intf.HalSensor;
|
||||
import se.koc.hal.intf.HalSensorController;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.bean.DBBean;
|
||||
import zutil.db.bean.DBBeanSQLResultHandler;
|
||||
import zutil.db.handler.SimpleSQLResult;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@DBBean.DBTable("sensor")
|
||||
public class HalSensor extends DBBean{
|
||||
public enum AggregationMethod{
|
||||
SUM,
|
||||
AVERAGE
|
||||
}
|
||||
public class Sensor extends DBBean{
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
|
||||
// Sensor specific data
|
||||
// Sensor specific data
|
||||
private String name;
|
||||
private String type;
|
||||
private String config;
|
||||
// Sensor specific data
|
||||
private transient HalSensor sensorData;
|
||||
|
||||
// User configuration
|
||||
@DBColumn("user_id")
|
||||
|
|
@ -33,40 +36,50 @@ public class HalSensor extends DBBean{
|
|||
|
||||
|
||||
|
||||
public static List<HalSensor> getExternalSensors(DBConnection db) throws SQLException{
|
||||
public static List<Sensor> getExternalSensors(DBConnection db) throws SQLException{
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT sensor.* FROM sensor,user WHERE user.external == 1 AND user.id == sensor.user_id" );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(HalSensor.class, db) );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Sensor.class, db) );
|
||||
}
|
||||
|
||||
public static List<HalSensor> getLocalSensors(DBConnection db) throws SQLException{
|
||||
public static List<Sensor> getLocalSensors(DBConnection db) throws SQLException{
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT sensor.* FROM sensor,user WHERE user.external == 0 AND user.id == sensor.user_id" );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(HalSensor.class, db) );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Sensor.class, db) );
|
||||
}
|
||||
|
||||
public static List<HalSensor> getSensors(DBConnection db, User user) throws SQLException{
|
||||
public static List<Sensor> getSensors(DBConnection db, User user) throws SQLException{
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM sensor WHERE user_id == ?" );
|
||||
stmt.setLong(1, user.getId());
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(HalSensor.class, db) );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Sensor.class, db) );
|
||||
}
|
||||
|
||||
public static List<HalSensor> getSensors(DBConnection db) throws SQLException{
|
||||
public static List<Sensor> getSensors(DBConnection db) throws SQLException{
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM sensor" );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(HalSensor.class, db) );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Sensor.class, db) );
|
||||
}
|
||||
|
||||
public static HalSensor getSensor(DBConnection db, int id) throws SQLException{
|
||||
return DBBean.load(db, HalSensor.class, id);
|
||||
public static Sensor getSensor(DBConnection db, int id) throws SQLException{
|
||||
return DBBean.load(db, Sensor.class, id);
|
||||
}
|
||||
|
||||
|
||||
public static long getHighestSequenceId(long sensorId) throws SQLException{
|
||||
PreparedStatement stmt = HalContext.getDB().getPreparedStatement("SELECT MAX(sequence_id) FROM sensor_data_aggr WHERE sensor_id == ?");
|
||||
stmt.setLong(1, sensorId);
|
||||
Integer id = DBConnection.exec(stmt, new SimpleSQLResult<Integer>());
|
||||
return (id != null ? id+1 : 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private HalSensor getSensorData(){
|
||||
if(sensorData == null) {
|
||||
try {
|
||||
Class c = Class.forName(type);
|
||||
sensorData = (HalSensor) c.newInstance();
|
||||
} catch (Exception e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
return sensorData;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
|
@ -107,11 +120,11 @@ public class HalSensor extends DBBean{
|
|||
}
|
||||
|
||||
|
||||
public AggregationMethod getAggregationMethod(){
|
||||
return AggregationMethod.SUM;
|
||||
public HalSensor.AggregationMethod getAggregationMethod(){
|
||||
return getSensorData().getAggregationMethod();
|
||||
}
|
||||
|
||||
public Class<? extends HalSensorController> getController(){
|
||||
return null;
|
||||
return getSensorData().getController();
|
||||
}
|
||||
}
|
||||
|
|
@ -22,44 +22,16 @@
|
|||
|
||||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.intf.HalEvent;
|
||||
import se.koc.hal.plugin.tellstick.TellstickSerialComm;
|
||||
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-05-07.
|
||||
*/
|
||||
public class SwitchEvent extends HalEvent{
|
||||
private String name;
|
||||
private NexaSelfLearning nexa;
|
||||
public interface SwitchEvent extends HalEvent {
|
||||
|
||||
|
||||
public SwitchEvent(String name, NexaSelfLearning nexa){
|
||||
this.name = name;
|
||||
this.nexa = nexa;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public boolean isOn(){
|
||||
return nexa.isEnabled();
|
||||
}
|
||||
public void turnOn(){
|
||||
nexa.setEnable(true);
|
||||
TellstickSerialComm.getInstance().write(nexa);
|
||||
}
|
||||
public void turnOff(){
|
||||
nexa.setEnable(false);
|
||||
TellstickSerialComm.getInstance().write(nexa);
|
||||
}
|
||||
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof String)
|
||||
return name.equals(obj);
|
||||
if(obj instanceof NexaSelfLearning)
|
||||
return nexa.equals(obj);
|
||||
return this == obj;
|
||||
}
|
||||
public boolean isOn();
|
||||
public void turnOn();
|
||||
public void turnOff();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
package se.koc.hal.struct;
|
||||
|
||||
import se.koc.hal.intf.HalSensor;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-12-03.
|
||||
*/
|
||||
public class TemperatureSensor extends HalSensor {
|
||||
public interface TemperatureSensor extends HalSensor {
|
||||
|
||||
public double getTemperature();
|
||||
|
||||
public double getHumidity();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,8 +43,6 @@ public class TelstickSerialCommTest {
|
|||
}
|
||||
});
|
||||
logger.info("Connecting to com port...");
|
||||
//comm.connect("COM5");
|
||||
comm.setDaemon(false);
|
||||
comm.connect("/dev/ttyUSB1");
|
||||
|
||||
logger.info("Up and Running");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue