diff --git a/src/se/hal/intf/HalEventData.java b/src/se/hal/intf/HalEventData.java index 8efaf588..dc0c068a 100755 --- a/src/se/hal/intf/HalEventData.java +++ b/src/se/hal/intf/HalEventData.java @@ -15,6 +15,6 @@ public interface HalEventData { * This method needs to be implemented. * NOTE: it should not compare data and timestamp, only static or unique data for the event type. */ - boolean equals(HalEventData obj); + boolean equals(Object obj); } diff --git a/src/se/hal/intf/HalSensorData.java b/src/se/hal/intf/HalSensorData.java index a2211783..5ac29504 100755 --- a/src/se/hal/intf/HalSensorData.java +++ b/src/se/hal/intf/HalSensorData.java @@ -35,5 +35,5 @@ public interface HalSensorData { * NOTE: it should only static or unique data for the sensor type. * This method is used to associate reported data with registered sensors */ - boolean equals(HalSensorData obj); + boolean equals(Object obj); } diff --git a/src/se/hal/plugin/nutups/NutUpsDevice.java b/src/se/hal/plugin/nutups/NutUpsDevice.java index e5b4cc36..74bc0eb4 100755 --- a/src/se/hal/plugin/nutups/NutUpsDevice.java +++ b/src/se/hal/plugin/nutups/NutUpsDevice.java @@ -42,7 +42,7 @@ public class NutUpsDevice implements PowerConsumptionSensorData{ } @Override - public boolean equals(HalSensorData obj){ + public boolean equals(Object obj){ if (obj instanceof NutUpsDevice) return deviceId != null && deviceId.equals(((NutUpsDevice)obj).deviceId); return false; diff --git a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java index df3c7dbe..e79dfe11 100755 --- a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java +++ b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java @@ -49,7 +49,7 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData { } @Override - public boolean equals(HalSensorData obj){ + public boolean equals(Object obj){ if(!(obj instanceof RPiPowerConsumptionSensor)) return false; return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin; diff --git a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java b/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java index 25861f4c..75d01f26 100755 --- a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java +++ b/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java @@ -48,7 +48,7 @@ public class RPiTemperatureSensor implements TemperatureSensorData { } @Override - public boolean equals(HalSensorData obj){ + public boolean equals(Object obj){ if(obj instanceof RPiTemperatureSensor) return obj == this; return false; diff --git a/src/se/hal/plugin/tellstick/TellstickParser.java b/src/se/hal/plugin/tellstick/TellstickParser.java index 1231a67c..0191c500 100755 --- a/src/se/hal/plugin/tellstick/TellstickParser.java +++ b/src/se/hal/plugin/tellstick/TellstickParser.java @@ -33,6 +33,8 @@ import java.util.logging.Logger; /** * Created by Ziver on 2015-02-18. + * + * Protocol Specification: http://developer.telldus.com/doxygen/TellStick.html */ public class TellstickParser { private static final Logger logger = LogUtil.getLogger(); @@ -43,6 +45,9 @@ public class TellstickParser { registerProtocol(Oregon0x1A2D.class); } + private int firmwareVersion = -1; + + public TellstickProtocol decode(String data) { if (data.startsWith("+W")) { @@ -72,6 +77,12 @@ public class TellstickParser { } } else if (data.startsWith("+S") || data.startsWith("+T")) { // This is confirmation of send commands + synchronized (this) { + this.notifyAll(); + } + } else if (data.startsWith("+V")) { + if (data.length() > 2) + firmwareVersion = Integer.parseInt(data.substring(2)); }else { logger.severe("Unknown prefix: " + data); } @@ -79,12 +90,24 @@ public class TellstickParser { return null; } + public void waitSendConformation(){ + try { + this.wait(); + } catch (InterruptedException e) { + logger.log(Level.SEVERE, null, e); + } + } + + + public int getFirmwareVersion() { + return firmwareVersion; + } public static void registerProtocol(Class protClass) { try { if (protocolMap == null) - protocolMap = new HashMap>(); + protocolMap = new HashMap<>(); TellstickProtocol tmp = protClass.newInstance(); protocolMap.put( tmp.getProtocolName() + "-" + tmp.getModelName(), diff --git a/src/se/hal/plugin/tellstick/TellstickSerialComm.java b/src/se/hal/plugin/tellstick/TellstickSerialComm.java index 2d5c0e5f..a69da5c2 100755 --- a/src/se/hal/plugin/tellstick/TellstickSerialComm.java +++ b/src/se/hal/plugin/tellstick/TellstickSerialComm.java @@ -53,7 +53,7 @@ public class TellstickSerialComm implements Runnable, private OutputStream out; private TimedHashSet set; // To check for duplicate transmissions - private TellstickParser parser; + protected TellstickParser parser; private HalSensorReportListener sensorListener; private HalEventReportListener eventListener; @@ -115,43 +115,8 @@ public class TellstickSerialComm implements Runnable, public void run() { try { String data; - while (in != null && (data = readLine()) != null) { - if ((data.startsWith("+S") || data.startsWith("+T"))) { - synchronized (this) { - this.notifyAll(); - } - } - else { - TellstickProtocol protocol = parser.decode(data); - if(protocol != null) { - if (protocol.getTimestamp() < 0) - protocol.setTimestamp(System.currentTimeMillis()); - - boolean registered = registeredDevices.contains(protocol); - if(registered && !set.contains(data) || // check for duplicates transmissions of registered devices - !registered && set.contains(data)) { // required duplicate transmissions before reporting unregistered devices - - //Check for registered device that are in the same group - if(protocol instanceof TellstickGroupProtocol) { - TellstickGroupProtocol groupProtocol = (TellstickGroupProtocol) protocol; - for (int i=0; i list = new ArrayList<>(); + tellstick.setListener(new HalEventReportListener() { + @Override + public void reportReceived(HalEventData e) { + list.add(e); + } + }); + // Execution + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:2345;"); + assertEquals("Events first transmission", 0, list.size()); + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:2345;"); + assertEquals("Events Second transmission", 1, list.size()); + } + + @Test + public void event(){ + // Setup + TellstickSerialComm tellstick = new TellstickSerialComm(); + final ArrayList list = new ArrayList<>(); + tellstick.setListener(new HalEventReportListener() { + @Override + public void reportReceived(HalEventData e) { + list.add(e); + } + }); + // Execution + TestEvent event = new TestEvent(); + event.testData = 0xAAAA; + tellstick.register(event); + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:AAAA;"); + // Verification + assertEquals("Nr of received events", 1, list.size()); + assertEquals("Data", event.testData, ((TestEvent)list.get(0)).testData); + } + + + + private static class TestEvent extends TellstickProtocol implements HalEventData{ + public int testData; + + public TestEvent(){ + super("test-prot", "test-model"); } + + @Override + public void decode(byte[] data) { + testData = Converter.toInt(data); + } + + + @Override + public String encode() {return null;} + @Override + public double getData() {return 0;} + @Override + public boolean equals(Object obj) {return testData == ((TestEvent)obj).testData;} } }