From fa9f7aed2bf3a2691372b39421d344520453abfb Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Thu, 25 Aug 2016 16:49:42 +0200 Subject: [PATCH] OregonProtocol now supports multiple types of SensorData --- .../plugin/tellstick/TellstickSerialComm.java | 15 ++++++++ .../plugin/tellstick/device/Oregon0x1A2D.java | 18 ++++++---- .../protocol/Oregon0x1A2DProtocol.java | 35 +++++++++++++++---- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/se/hal/plugin/tellstick/TellstickSerialComm.java b/src/se/hal/plugin/tellstick/TellstickSerialComm.java index d908f8f6..76e36165 100755 --- a/src/se/hal/plugin/tellstick/TellstickSerialComm.java +++ b/src/se/hal/plugin/tellstick/TellstickSerialComm.java @@ -34,6 +34,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.concurrent.Executors; import java.util.logging.Level; @@ -48,6 +49,7 @@ public class TellstickSerialComm implements Runnable, HalSensorController, HalEventController, HalAutoScannableController { private static final long TRANSMISSION_UNIQUENESS_TTL = 1000; // milliseconds private static final Logger logger = LogUtil.getLogger(); + private static TellstickSerialComm instance; // Todo: Don't like this but it is the best I could come up with private SerialPort serial; private InputStream in; @@ -83,6 +85,10 @@ public class TellstickSerialComm implements Runnable, } public void initialize(String portName) throws Exception { + if (instance != null) + throw new IllegalStateException("There is a previous TellstickSerialComm instance, only one allowed"); + instance = this; + logger.info("Connecting to com port... ("+ portName +")"); serial = SerialPort.getCommPort(portName); serial.setBaudRate(9600); @@ -110,6 +116,7 @@ public class TellstickSerialComm implements Runnable, serial = null; in = null; out = null; + instance = null; } @@ -222,6 +229,10 @@ public class TellstickSerialComm implements Runnable, "Device config is not an instance of "+TellstickDevice.class+": "+sensor.getClass()); } + public List getRegisteredDevices(){ + return registeredDevices; + } + @Override public void deregister(HalEventConfig event) { registeredDevices.remove(event); @@ -245,4 +256,8 @@ public class TellstickSerialComm implements Runnable, sensorListener = listener; } + + public static TellstickSerialComm getInstance(){ + return instance; + } } \ No newline at end of file diff --git a/src/se/hal/plugin/tellstick/device/Oregon0x1A2D.java b/src/se/hal/plugin/tellstick/device/Oregon0x1A2D.java index 234783ed..f6331667 100755 --- a/src/se/hal/plugin/tellstick/device/Oregon0x1A2D.java +++ b/src/se/hal/plugin/tellstick/device/Oregon0x1A2D.java @@ -33,12 +33,24 @@ public class Oregon0x1A2D implements HalSensorConfig,TellstickDevice { private OregonSensorType sensorType; + public Oregon0x1A2D() { } public Oregon0x1A2D(int address) { this.address = address; } + public int getAddress() { + return address; + } + @Override + public long getDataInterval() { + return interval; + } + public OregonSensorType getSensorType() { + return sensorType; + } + @Override public boolean equals(Object obj){ if(! (obj instanceof Oregon0x1A2D)) @@ -52,12 +64,6 @@ public class Oregon0x1A2D implements HalSensorConfig,TellstickDevice { } - - @Override - public long getDataInterval() { - return interval; - } - @Override public AggregationMethod getAggregationMethod() { if (sensorType == OregonSensorType.POWER) diff --git a/src/se/hal/plugin/tellstick/protocol/Oregon0x1A2DProtocol.java b/src/se/hal/plugin/tellstick/protocol/Oregon0x1A2DProtocol.java index 19ff566f..3c89a0f8 100755 --- a/src/se/hal/plugin/tellstick/protocol/Oregon0x1A2DProtocol.java +++ b/src/se/hal/plugin/tellstick/protocol/Oregon0x1A2DProtocol.java @@ -1,8 +1,13 @@ package se.hal.plugin.tellstick.protocol; +import se.hal.intf.HalSensorData; +import se.hal.plugin.tellstick.TellstickDevice; import se.hal.plugin.tellstick.TellstickProtocol; +import se.hal.plugin.tellstick.TellstickSerialComm; import se.hal.plugin.tellstick.device.Oregon0x1A2D; import se.hal.struct.devicedata.HumiditySensorData; +import se.hal.struct.devicedata.LightSensorData; +import se.hal.struct.devicedata.PowerConsumptionSensorData; import se.hal.struct.devicedata.TemperatureSensorData; import zutil.log.LogUtil; @@ -59,14 +64,30 @@ public class Oregon0x1A2DProtocol extends TellstickProtocol { // Create return objects ArrayList list = new ArrayList<>(); - Oregon0x1A2D device = new Oregon0x1A2D(address); - list.add(new TellstickDecodedEntry( - device, new TemperatureSensorData(temperature) - )); - list.add(new TellstickDecodedEntry( - device, new HumiditySensorData(humidity) - )); + for (Oregon0x1A2D device : getSensorByAddress(address)) { + HalSensorData dataObj; + switch (device.getSensorType()){ + case HUMIDITY: + dataObj = new HumiditySensorData(humidity); break; + case LIGHT: + dataObj = new LightSensorData(temperature); break; + case POWER: + dataObj = new PowerConsumptionSensorData(temperature); break; + case TEMPERATURE: + default: + dataObj = new TemperatureSensorData(temperature); break; + } + list.add(new TellstickDecodedEntry(device, dataObj)); + } return list; } + private List getSensorByAddress(int address){ + ArrayList list = new ArrayList<>(); + for (TellstickDevice device : TellstickSerialComm.getInstance().getRegisteredDevices()){ + if (device instanceof Oregon0x1A2D && ((Oregon0x1A2D) device).getAddress() == address) + list.add((Oregon0x1A2D) device); + } + return list; + } }