OregonProtocol now supports multiple types of SensorData
This commit is contained in:
parent
d91d5c5b81
commit
fa9f7aed2b
3 changed files with 55 additions and 13 deletions
|
|
@ -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<TellstickDevice> 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<TellstickDecodedEntry> 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<Oregon0x1A2D> getSensorByAddress(int address){
|
||||
ArrayList<Oregon0x1A2D> list = new ArrayList<>();
|
||||
for (TellstickDevice device : TellstickSerialComm.getInstance().getRegisteredDevices()){
|
||||
if (device instanceof Oregon0x1A2D && ((Oregon0x1A2D) device).getAddress() == address)
|
||||
list.add((Oregon0x1A2D) device);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue