From e08f1b447724b1911430a0c848daeabeca52c37e Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 5 Jan 2016 02:11:03 +0100 Subject: [PATCH] Bug fixes Former-commit-id: e2dd5aecc8d9fd521d97bff04a9336f6c5008aad --- Hal.iml | 4 +- hal.conf.example | 3 ++ src/se/koc/hal/ControllerManager.java | 10 ++-- .../hal/plugin/tellstick/TellstickParser.java | 10 ++-- .../plugin/tellstick/TellstickSerialComm.java | 47 ++++++++++++++----- .../tellstick/protocols/NexaSelfLearning.java | 2 +- .../tellstick/protocols/Oregon0x1A2D.java | 13 +++++ 7 files changed, 65 insertions(+), 24 deletions(-) create mode 100755 hal.conf.example diff --git a/Hal.iml b/Hal.iml index 45384d36..75f455bc 100755 --- a/Hal.iml +++ b/Hal.iml @@ -30,7 +30,9 @@ - + + + diff --git a/hal.conf.example b/hal.conf.example new file mode 100755 index 00000000..7d8cb831 --- /dev/null +++ b/hal.conf.example @@ -0,0 +1,3 @@ +http_port=8080 +sync_port=6666 +tellstick.com_port=COM5 \ No newline at end of file diff --git a/src/se/koc/hal/ControllerManager.java b/src/se/koc/hal/ControllerManager.java index c6074a1a..f37720e2 100755 --- a/src/se/koc/hal/ControllerManager.java +++ b/src/se/koc/hal/ControllerManager.java @@ -50,6 +50,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor /////////////////////////////// SENSORS /////////////////////////////////// public void register(Sensor sensor) throws IllegalAccessException, InstantiationException { + logger.info("Registering new sensor(id: "+ sensor.getId() +"): "+ sensor.getSensorData().getClass()); Class c = sensor.getController(); HalSensorController controller = getControllerInstance(c); @@ -59,6 +60,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor } public void deregister(Sensor sensor){ + logger.info("Deregistering sensor(id: "+ sensor.getId() +"): "+ sensor.getSensorData().getClass()); Class c = sensor.getController(); HalSensorController controller = (HalSensorController) controllerMap.get(c);; if (controller != null) { @@ -91,7 +93,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor if (sensor != null) { PreparedStatement stmt = - db.getPreparedStatement("INSERT INTO sensor_data_raw (timestamp, event_id, data) VALUES(?, ?, ?)"); + db.getPreparedStatement("INSERT INTO sensor_data_raw (timestamp, sensor_id, data) VALUES(?, ?, ?)"); stmt.setLong(1, sensorData.getTimestamp()); stmt.setLong(2, sensor.getId()); stmt.setDouble(3, sensorData.getData()); @@ -112,6 +114,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor //////////////////////////////// EVENTS /////////////////////////////////// public void register(Event event) throws IllegalAccessException, InstantiationException { + logger.info("Registering new event(id: "+ event.getId() +"): "+ event.getEventData().getClass()); Class c = event.getController(); HalEventController controller = getControllerInstance(c); @@ -121,6 +124,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor } public void deregister(Event event){ + logger.info("Deregistering event(id: "+ event.getId() +"): "+ event.getEventData().getClass()); Class c = event.getController(); HalEventController controller = (HalEventController) controllerMap.get(c); if (controller != null) { @@ -179,7 +183,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor controller = controllerMap.get(c); else { // Instantiate controller - logger.fine("Instantiating new controller: " + c.getName()); + logger.info("Instantiating new controller: " + c.getName()); try { controller = c.newInstance(); if(controller instanceof HalSensorController) { @@ -209,7 +213,7 @@ public class ControllerManager implements HalSensorReportListener, HalEventRepor if(size < 0){ // Remove controller as it has no more registered sensors - logger.fine("Closing controller as it has no more registered sensors: "+controller.getClass().getName()); + logger.info("Closing controller as it has no more registered objects: "+controller.getClass().getName()); controllerMap.remove(controller.getClass()); if(controller instanceof HalSensorController) diff --git a/src/se/koc/hal/plugin/tellstick/TellstickParser.java b/src/se/koc/hal/plugin/tellstick/TellstickParser.java index 21d65442..a6ecb1a8 100755 --- a/src/se/koc/hal/plugin/tellstick/TellstickParser.java +++ b/src/se/koc/hal/plugin/tellstick/TellstickParser.java @@ -44,8 +44,6 @@ public class TellstickParser { } - private TellstickProtocol previus; - public TellstickProtocol decode(String data) { if (data.startsWith("+W")) { data = data.substring(2); @@ -62,12 +60,10 @@ public class TellstickParser { try { TellstickProtocol protocol = protClass.newInstance(); String binData = map.get("data"); + protocol.decode(Converter.hexToByte(binData)); - if(!protocol.equals(previus)) { - previus = protocol; - logger.finest("Decoded: " + protocol); - return protocol; - } + logger.finest("Decoded: " + protocol); + return protocol; } catch (Exception e) { logger.log(Level.WARNING, null, e); } diff --git a/src/se/koc/hal/plugin/tellstick/TellstickSerialComm.java b/src/se/koc/hal/plugin/tellstick/TellstickSerialComm.java index d6519525..f294ddac 100755 --- a/src/se/koc/hal/plugin/tellstick/TellstickSerialComm.java +++ b/src/se/koc/hal/plugin/tellstick/TellstickSerialComm.java @@ -47,8 +47,8 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv private static final Logger logger = LogUtil.getLogger(); private SerialPort serial; - private BufferedReader in; - private BufferedWriter out; + private InputStream in; + private OutputStream out; private TimedHashSet set; // To check for retransmissions private TellstickParser parser; @@ -84,8 +84,8 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv serial.setComPortTimeouts( SerialPort.TIMEOUT_READ_BLOCKING, 0, 0); - in = new BufferedReader(new InputStreamReader(new InputStreamLogger(serial.getInputStream()), "UTF-8")); - out = new BufferedWriter(new OutputStreamWriter(new OutputStreamLogger(serial.getOutputStream()), "UTF-8")); + in = new InputStreamLogger(serial.getInputStream()); + out = new OutputStreamLogger(serial.getOutputStream()); Executors.newSingleThreadExecutor().execute(this); } @@ -109,7 +109,8 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv public void run() { try { String data; - while (in != null && (data = in.readLine()) != null) { + + while (in != null && (data = readLine()) != null) { if ((data.startsWith("+S") || data.startsWith("+T"))) { synchronized (this) { this.notifyAll(); @@ -118,14 +119,16 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv else { if(!set.contains(data)) { TellstickProtocol protocol = parser.decode(data); - if(protocol.getTimestamp() < 0) - protocol.setTimestamp(System.currentTimeMillis()); - set.add(data); + if(protocol != null) { + if (protocol.getTimestamp() < 0) + protocol.setTimestamp(System.currentTimeMillis()); + set.add(data); - if (sensorListener != null && protocol instanceof HalSensor) - sensorListener.reportReceived((HalSensor)protocol); - else if (eventListener != null && protocol instanceof HalEvent) - eventListener.reportReceived((HalEvent)protocol); + if (sensorListener != null && protocol instanceof HalSensor) + sensorListener.reportReceived((HalSensor) protocol); + else if (eventListener != null && protocol instanceof HalEvent) + eventListener.reportReceived((HalEvent) protocol); + } } } } @@ -134,6 +137,26 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEv } } + /** + * There seems to be an issue with read(...) methods only read() is working + */ + private String readLine() throws IOException { + StringBuilder str = new StringBuilder(50); + char c = 0; + while((c = (char)in.read()) >= 0){ + switch(c) { + case '\n': + case '\r': + if(str.length() > 0) + return str.toString(); + break; + default: + str.append(c); + } + } + return str.toString(); + } + @Override public void send(HalEvent event) { diff --git a/src/se/koc/hal/plugin/tellstick/protocols/NexaSelfLearning.java b/src/se/koc/hal/plugin/tellstick/protocols/NexaSelfLearning.java index 49a217e8..ed601fea 100755 --- a/src/se/koc/hal/plugin/tellstick/protocols/NexaSelfLearning.java +++ b/src/se/koc/hal/plugin/tellstick/protocols/NexaSelfLearning.java @@ -154,7 +154,7 @@ public class NexaSelfLearning extends TellstickProtocol implements SwitchEvent { public String toString(){ - return "class:command;protocol:arctech;model:selflearning;" + + return "protocol:arctech;model:selflearning;" + "house:"+house+ ";group:"+group+ ";unit:"+unit+ diff --git a/src/se/koc/hal/plugin/tellstick/protocols/Oregon0x1A2D.java b/src/se/koc/hal/plugin/tellstick/protocols/Oregon0x1A2D.java index e2f95058..790cebd2 100755 --- a/src/se/koc/hal/plugin/tellstick/protocols/Oregon0x1A2D.java +++ b/src/se/koc/hal/plugin/tellstick/protocols/Oregon0x1A2D.java @@ -71,6 +71,19 @@ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionS } + public boolean equals(Object obj){ + if(! (obj instanceof Oregon0x1A2D)) + return false; + return ((Oregon0x1A2D)obj).address == this.address; + } + + public String toString(){ + return "protocol:oregon;model:0x1A2D;" + + "address:"+address+ + ";temperature:"+temperature+ + ";humidity:"+humidity; + } + public double getTemperature(){ return temperature;