From 253e3594833d0ee0050b684b1cb28b1eb0b36021 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Fri, 19 Aug 2016 16:25:18 +0200 Subject: [PATCH] Added Tellstic sensor test --- run.sh | 4 +- src/se/hal/ControllerManager.java | 10 +- src/se/hal/intf/HalEventReportListener.java | 2 +- src/se/hal/intf/HalSensorReportListener.java | 2 +- .../hal/plugin/raspberry/RPiController.java | 7 +- .../plugin/tellstick/TellstickSerialComm.java | 4 +- src/se/hal/struct/AbstractDevice.java | 3 +- ....java => TelstickSerialCommEventTest.java} | 6 +- .../TelstickSerialCommSensorTest.java | 102 ++++++++++++++++++ 9 files changed, 119 insertions(+), 21 deletions(-) rename test/se/hal/plugin/tellstick/{TelstickSerialCommTest.java => TelstickSerialCommEventTest.java} (94%) create mode 100755 test/se/hal/plugin/tellstick/TelstickSerialCommSensorTest.java diff --git a/run.sh b/run.sh index 906f44f8..1e235bc1 100755 --- a/run.sh +++ b/run.sh @@ -1,8 +1,6 @@ #!/bin/bash -#screen -S hal -L -d -m \ -# java -cp sqlite-jdbc-3.7.2.jar:jSerialComm-1.3.4.jar:hal.jar:. se.hal.plugin.tellstick.TelstickSerialCommTest -#ant clean +ant clean ant release screen -S hal -L -d -m ant run diff --git a/src/se/hal/ControllerManager.java b/src/se/hal/ControllerManager.java index 9e3662e7..8ccac549 100755 --- a/src/se/hal/ControllerManager.java +++ b/src/se/hal/ControllerManager.java @@ -102,7 +102,7 @@ public class ControllerManager implements HalSensorReportListener, } @Override - public void reportReceived(HalSensorConfig sensorConfig, HalDeviceData sensorData) { + public void reportReceived(HalSensorConfig sensorConfig, HalSensorData sensorData) { try{ DBConnection db = HalContext.getDB(); Sensor sensor = findSensor(sensorConfig, registeredSensors); @@ -124,8 +124,9 @@ public class ControllerManager implements HalSensorReportListener, sensor = new Sensor(); detectedSensors.add(sensor); } + sensor.setDeviceConfig(sensorConfig); } - sensor.setDeviceConfig(sensorConfig); // Set the latest data + sensor.setDeviceData(sensorData); }catch (SQLException e){ logger.log(Level.WARNING, "Unable to store sensor report", e); @@ -191,7 +192,7 @@ public class ControllerManager implements HalSensorReportListener, } @Override - public void reportReceived(HalEventConfig eventConfig, HalDeviceData eventData) { + public void reportReceived(HalEventConfig eventConfig, HalEventData eventData) { try { DBConnection db = HalContext.getDB(); Event event = findEvent(eventConfig, registeredEvents); @@ -213,8 +214,9 @@ public class ControllerManager implements HalSensorReportListener, event = new Event(); detectedEvents.add(event); } + event.setDeviceConfig(eventConfig); } - event.setDeviceConfig(eventConfig); // Set the latest data + event.setDeviceData(eventData); }catch (SQLException e){ logger.log(Level.WARNING, "Unable to store event report", e); diff --git a/src/se/hal/intf/HalEventReportListener.java b/src/se/hal/intf/HalEventReportListener.java index d46e7bf3..0f599e75 100755 --- a/src/se/hal/intf/HalEventReportListener.java +++ b/src/se/hal/intf/HalEventReportListener.java @@ -2,6 +2,6 @@ package se.hal.intf; public interface HalEventReportListener { - void reportReceived(HalEventConfig e, HalDeviceData d); + void reportReceived(HalEventConfig e, HalEventData d); } \ No newline at end of file diff --git a/src/se/hal/intf/HalSensorReportListener.java b/src/se/hal/intf/HalSensorReportListener.java index 7e48510b..97deef92 100755 --- a/src/se/hal/intf/HalSensorReportListener.java +++ b/src/se/hal/intf/HalSensorReportListener.java @@ -2,6 +2,6 @@ package se.hal.intf; public interface HalSensorReportListener { - void reportReceived(HalSensorConfig s, HalDeviceData d); + void reportReceived(HalSensorConfig s, HalSensorData d); } \ No newline at end of file diff --git a/src/se/hal/plugin/raspberry/RPiController.java b/src/se/hal/plugin/raspberry/RPiController.java index dc819523..55e589f7 100755 --- a/src/se/hal/plugin/raspberry/RPiController.java +++ b/src/se/hal/plugin/raspberry/RPiController.java @@ -1,9 +1,6 @@ package se.hal.plugin.raspberry; -import se.hal.intf.HalDeviceData; -import se.hal.intf.HalSensorController; -import se.hal.intf.HalSensorConfig; -import se.hal.intf.HalSensorReportListener; +import se.hal.intf.*; import se.hal.plugin.raspberry.hardware.RPiDS18B20; import se.hal.plugin.raspberry.hardware.RPiInteruptPulseFlankCounter; import zutil.log.LogUtil; @@ -90,7 +87,7 @@ public class RPiController implements HalSensorController { } } - public void sendDataReport(HalSensorConfig sensorConfig, HalDeviceData sensorData){ + public void sendDataReport(HalSensorConfig sensorConfig, HalSensorData sensorData){ if(sensorListener != null){ sensorListener.reportReceived(sensorConfig, sensorData); }else{ diff --git a/src/se/hal/plugin/tellstick/TellstickSerialComm.java b/src/se/hal/plugin/tellstick/TellstickSerialComm.java index b841c784..d908f8f6 100755 --- a/src/se/hal/plugin/tellstick/TellstickSerialComm.java +++ b/src/se/hal/plugin/tellstick/TellstickSerialComm.java @@ -175,9 +175,9 @@ public class TellstickSerialComm implements Runnable, } private void reportEvent(TellstickDevice tellstickDevice, HalDeviceData deviceData){ if (sensorListener != null && tellstickDevice instanceof HalSensorConfig) - sensorListener.reportReceived((HalSensorConfig) tellstickDevice, deviceData); + sensorListener.reportReceived((HalSensorConfig) tellstickDevice, (HalSensorData) deviceData); else if (eventListener != null && tellstickDevice instanceof HalEventConfig) - eventListener.reportReceived((HalEventConfig) tellstickDevice, deviceData); + eventListener.reportReceived((HalEventConfig) tellstickDevice, (HalEventData) deviceData); } @Override diff --git a/src/se/hal/struct/AbstractDevice.java b/src/se/hal/struct/AbstractDevice.java index ac6029c9..8dcfc574 100755 --- a/src/se/hal/struct/AbstractDevice.java +++ b/src/se/hal/struct/AbstractDevice.java @@ -59,7 +59,7 @@ public abstract class AbstractDevice extends DBBean { applyConfig(); } catch (Exception e) { - logger.log(Level.SEVERE, "Unable instantiate DeviceData: "+type, e); + logger.log(Level.SEVERE, "Unable instantiate DeviceConfig: "+type, e); } } return deviceConfig; @@ -74,7 +74,6 @@ public abstract class AbstractDevice extends DBBean { if(data != null) { deviceConfig = data; type = data.getClass().getName(); - applyConfig(); // TODO: this is a bit clunky, should probably be solved in another way } else { deviceConfig = null; type = null; diff --git a/test/se/hal/plugin/tellstick/TelstickSerialCommTest.java b/test/se/hal/plugin/tellstick/TelstickSerialCommEventTest.java similarity index 94% rename from test/se/hal/plugin/tellstick/TelstickSerialCommTest.java rename to test/se/hal/plugin/tellstick/TelstickSerialCommEventTest.java index bad82ee4..0bbc09ae 100755 --- a/test/se/hal/plugin/tellstick/TelstickSerialCommTest.java +++ b/test/se/hal/plugin/tellstick/TelstickSerialCommEventTest.java @@ -15,7 +15,7 @@ import static org.junit.Assert.assertEquals; /** * Created by Ziver on 2015-11-19. */ -public class TelstickSerialCommTest { +public class TelstickSerialCommEventTest { @Before public void init(){ @@ -47,7 +47,7 @@ public class TelstickSerialCommTest { final ArrayList list = new ArrayList<>(); tellstick.setListener(new HalEventReportListener() { @Override - public void reportReceived(HalEventConfig e, HalDeviceData d) { + public void reportReceived(HalEventConfig e, HalEventData d) { list.add(e); } }); @@ -65,7 +65,7 @@ public class TelstickSerialCommTest { final ArrayList list = new ArrayList<>(); tellstick.setListener(new HalEventReportListener() { @Override - public void reportReceived(HalEventConfig e, HalDeviceData d) { + public void reportReceived(HalEventConfig e, HalEventData d) { list.add(e); } }); diff --git a/test/se/hal/plugin/tellstick/TelstickSerialCommSensorTest.java b/test/se/hal/plugin/tellstick/TelstickSerialCommSensorTest.java new file mode 100755 index 00000000..1a5a53b4 --- /dev/null +++ b/test/se/hal/plugin/tellstick/TelstickSerialCommSensorTest.java @@ -0,0 +1,102 @@ +package se.hal.plugin.tellstick; + +import org.junit.Before; +import org.junit.Test; +import se.hal.intf.HalSensorConfig; +import se.hal.intf.HalSensorController; +import se.hal.intf.HalSensorData; +import se.hal.intf.HalSensorReportListener; +import se.hal.struct.devicedata.TemperatureSensorData; +import zutil.converter.Converter; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Ziver on 2015-11-19. + */ +public class TelstickSerialCommSensorTest { + + @Before + public void init(){ + TellstickParser.registerProtocol(TestSensor.class); + } + + + //############ Normal TCs + + @Test + public void receiveUnregisteredSensor(){ + // Setup + TellstickSerialComm tellstick = new TellstickSerialComm(); + final ArrayList list = new ArrayList<>(); + tellstick.setListener(new HalSensorReportListener() { + @Override + public void reportReceived(HalSensorConfig e, HalSensorData d) { + list.add(e); + } + }); + // Execution + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:2345;"); + assertEquals("Sensors first transmission", 0, list.size()); + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:2345;"); + assertEquals("Sensors Second transmission", 1, list.size()); + } + + @Test + public void receiveSensor(){ + // Setup + TellstickSerialComm tellstick = new TellstickSerialComm(); + final ArrayList list = new ArrayList<>(); + tellstick.setListener(new HalSensorReportListener() { + @Override + public void reportReceived(HalSensorConfig e, HalSensorData d) { + list.add(e); + } + }); + // Execution + TestSensor sensor = new TestSensor(); + sensor.testData = 0xAAAA; + tellstick.register(sensor); + tellstick.handleLine("+Wclass:sensor;protocol:test-prot;model:test-model;data:AAAA;"); + // Verification + assertEquals("Nr of received sensors", 1, list.size()); + assertEquals("Data", sensor.testData, ((TestSensor)list.get(0)).testData); + } + + + + + private static class TestSensor extends TellstickProtocol implements HalSensorConfig,TellstickDevice { + public int testData; + + public TestSensor(){ + super("test-prot", "test-model"); + } + + @Override + public List decode(byte[] data) { + testData = Converter.toInt(data); + + ArrayList list = new ArrayList<>(); + list.add(new TellstickDecodedEntry( + this, new TemperatureSensorData(testData) + )); + return list; + } + + + @Override + public boolean equals(Object obj) {return testData == ((TestSensor)obj).testData;} + + + @Override + public long getDataInterval() { return 0; } + @Override + public AggregationMethod getAggregationMethod() { return null; } + @Override + public Class getSensorController() { return null; } + } +}