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.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
@ -48,6 +49,7 @@ public class TellstickSerialComm implements Runnable,
|
||||||
HalSensorController, HalEventController, HalAutoScannableController {
|
HalSensorController, HalEventController, HalAutoScannableController {
|
||||||
private static final long TRANSMISSION_UNIQUENESS_TTL = 1000; // milliseconds
|
private static final long TRANSMISSION_UNIQUENESS_TTL = 1000; // milliseconds
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
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 SerialPort serial;
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
|
|
@ -83,6 +85,10 @@ public class TellstickSerialComm implements Runnable,
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initialize(String portName) throws Exception {
|
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 +")");
|
logger.info("Connecting to com port... ("+ portName +")");
|
||||||
serial = SerialPort.getCommPort(portName);
|
serial = SerialPort.getCommPort(portName);
|
||||||
serial.setBaudRate(9600);
|
serial.setBaudRate(9600);
|
||||||
|
|
@ -110,6 +116,7 @@ public class TellstickSerialComm implements Runnable,
|
||||||
serial = null;
|
serial = null;
|
||||||
in = null;
|
in = null;
|
||||||
out = 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());
|
"Device config is not an instance of "+TellstickDevice.class+": "+sensor.getClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TellstickDevice> getRegisteredDevices(){
|
||||||
|
return registeredDevices;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deregister(HalEventConfig event) {
|
public void deregister(HalEventConfig event) {
|
||||||
registeredDevices.remove(event);
|
registeredDevices.remove(event);
|
||||||
|
|
@ -245,4 +256,8 @@ public class TellstickSerialComm implements Runnable,
|
||||||
sensorListener = listener;
|
sensorListener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static TellstickSerialComm getInstance(){
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -33,12 +33,24 @@ public class Oregon0x1A2D implements HalSensorConfig,TellstickDevice {
|
||||||
private OregonSensorType sensorType;
|
private OregonSensorType sensorType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Oregon0x1A2D() { }
|
public Oregon0x1A2D() { }
|
||||||
public Oregon0x1A2D(int address) {
|
public Oregon0x1A2D(int address) {
|
||||||
this.address = address;
|
this.address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public long getDataInterval() {
|
||||||
|
return interval;
|
||||||
|
}
|
||||||
|
public OregonSensorType getSensorType() {
|
||||||
|
return sensorType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj){
|
public boolean equals(Object obj){
|
||||||
if(! (obj instanceof Oregon0x1A2D))
|
if(! (obj instanceof Oregon0x1A2D))
|
||||||
|
|
@ -52,12 +64,6 @@ public class Oregon0x1A2D implements HalSensorConfig,TellstickDevice {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getDataInterval() {
|
|
||||||
return interval;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AggregationMethod getAggregationMethod() {
|
public AggregationMethod getAggregationMethod() {
|
||||||
if (sensorType == OregonSensorType.POWER)
|
if (sensorType == OregonSensorType.POWER)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,13 @@
|
||||||
package se.hal.plugin.tellstick.protocol;
|
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.TellstickProtocol;
|
||||||
|
import se.hal.plugin.tellstick.TellstickSerialComm;
|
||||||
import se.hal.plugin.tellstick.device.Oregon0x1A2D;
|
import se.hal.plugin.tellstick.device.Oregon0x1A2D;
|
||||||
import se.hal.struct.devicedata.HumiditySensorData;
|
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 se.hal.struct.devicedata.TemperatureSensorData;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
|
|
@ -59,14 +64,30 @@ public class Oregon0x1A2DProtocol extends TellstickProtocol {
|
||||||
|
|
||||||
// Create return objects
|
// Create return objects
|
||||||
ArrayList<TellstickDecodedEntry> list = new ArrayList<>();
|
ArrayList<TellstickDecodedEntry> list = new ArrayList<>();
|
||||||
Oregon0x1A2D device = new Oregon0x1A2D(address);
|
for (Oregon0x1A2D device : getSensorByAddress(address)) {
|
||||||
list.add(new TellstickDecodedEntry(
|
HalSensorData dataObj;
|
||||||
device, new TemperatureSensorData(temperature)
|
switch (device.getSensorType()){
|
||||||
));
|
case HUMIDITY:
|
||||||
list.add(new TellstickDecodedEntry(
|
dataObj = new HumiditySensorData(humidity); break;
|
||||||
device, new HumiditySensorData(humidity)
|
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;
|
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