Refactored remaining code (RPi Plugin)
This commit is contained in:
parent
3a8bb12bab
commit
d692c30bd9
10 changed files with 62 additions and 24 deletions
|
|
@ -1,5 +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;
|
||||
|
|
@ -89,9 +90,9 @@ public class RPiController implements HalSensorController {
|
|||
}
|
||||
}
|
||||
|
||||
public void sendDataReport(HalSensorConfig sensorConfig){
|
||||
public void sendDataReport(HalSensorConfig sensorConfig, HalDeviceData sensorData){
|
||||
if(sensorListener != null){
|
||||
sensorListener.reportReceived(sensorConfig);
|
||||
sensorListener.reportReceived(sensorConfig, sensorData);
|
||||
}else{
|
||||
logger.log(Level.WARNING, "Could not report data. No registered listener");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,8 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
private int gpioPin = -1;
|
||||
|
||||
|
||||
public RPiPowerConsumptionSensor(){
|
||||
//need to be empty for the framework to create an instance
|
||||
}
|
||||
|
||||
public RPiPowerConsumptionSensor(int gpioPin) {
|
||||
public RPiPowerConsumptionSensor(){ } //need to be empty for the framework to create an instance
|
||||
public RPiPowerConsumptionSensor(int gpioPin) {
|
||||
this.gpioPin = gpioPin;
|
||||
}
|
||||
|
||||
|
|
@ -38,9 +35,9 @@ public class RPiPowerConsumptionSensor implements HalSensorConfig {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof RPiPowerConsumptionSensor))
|
||||
return false;
|
||||
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
||||
if(obj instanceof RPiPowerConsumptionSensor)
|
||||
return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin;
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getGpioPin() {
|
||||
|
|
|
|||
|
|
@ -8,10 +8,15 @@ import zutil.ui.Configurator;
|
|||
public class RPiTemperatureSensor implements HalSensorConfig {
|
||||
|
||||
@Configurator.Configurable("1-Wire Address")
|
||||
private String w1Address = null;
|
||||
private String w1Address;
|
||||
|
||||
|
||||
|
||||
public RPiTemperatureSensor() { }
|
||||
public RPiTemperatureSensor(String w1Address) {
|
||||
this.w1Address = w1Address;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getDataInterval() {
|
||||
|
|
@ -30,8 +35,8 @@ public class RPiTemperatureSensor implements HalSensorConfig {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if(obj instanceof RPiTemperatureSensor)
|
||||
return obj == this;
|
||||
if(obj instanceof RPiTemperatureSensor && w1Address != null)
|
||||
return this.get1WAddress().equals(((RPiTemperatureSensor) obj).w1Address);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import com.pi4j.temperature.TemperatureScale;
|
|||
import se.hal.plugin.raspberry.RPiController;
|
||||
import se.hal.plugin.raspberry.RPiSensor;
|
||||
import se.hal.plugin.raspberry.RPiTemperatureSensor;
|
||||
import se.hal.struct.devicedata.TemperatureSensorData;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
|
|
@ -54,7 +55,11 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
for(TemperatureSensor device : w1Mater.getDevices(TemperatureSensor.class)){
|
||||
if(device.getName().equals(w1Address)){
|
||||
controller.sendDataReport(
|
||||
new RPiTemperatureSensor(System.currentTimeMillis(), device.getTemperature(TemperatureScale.CELSIUS)));
|
||||
new RPiTemperatureSensor(w1Address),
|
||||
new TemperatureSensorData(
|
||||
System.currentTimeMillis(),
|
||||
device.getTemperature(TemperatureScale.CELSIUS)
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import se.hal.plugin.raspberry.RPiController;
|
|||
import se.hal.plugin.raspberry.RPiPowerConsumptionSensor;
|
||||
import se.hal.plugin.raspberry.RPiSensor;
|
||||
import se.hal.plugin.raspberry.RPiUtility;
|
||||
import se.hal.struct.devicedata.PowerConsumptionSensorData;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
|
@ -132,7 +133,11 @@ public class RPiInteruptPulseFlankCounter implements Runnable, GpioPinListenerDi
|
|||
@Override
|
||||
public void run() {
|
||||
logger.log(Level.INFO, "Reporting data. timestamp_end="+timestamp_end+", data="+data);
|
||||
controller.sendDataReport(new RPiPowerConsumptionSensor(gpioPin, timestamp_end, data));
|
||||
controller.sendDataReport(
|
||||
new RPiPowerConsumptionSensor(gpioPin),
|
||||
new PowerConsumptionSensorData(
|
||||
timestamp_end, data
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -213,22 +213,24 @@ public class TellstickSerialComm implements Runnable,
|
|||
public void register(HalEventConfig event) {
|
||||
if(event instanceof TellstickDevice)
|
||||
registeredDevices.add((TellstickDevice) event);
|
||||
else throw new IllegalArgumentException(
|
||||
"Device configuration is not an instance of "+TellstickDevice.class+": "+event.getClass());
|
||||
}
|
||||
@Override
|
||||
public void register(HalSensorConfig sensor) {
|
||||
if(sensor instanceof TellstickDevice)
|
||||
registeredDevices.add((TellstickDevice) sensor);
|
||||
else throw new IllegalArgumentException(
|
||||
"Device configuration is not an instance of "+TellstickDevice.class+": "+sensor.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregister(HalEventConfig event) {
|
||||
if(event instanceof TellstickDevice)
|
||||
registeredDevices.remove(event);
|
||||
registeredDevices.remove(event);
|
||||
}
|
||||
@Override
|
||||
public void deregister(HalSensorConfig sensor) {
|
||||
if(sensor instanceof TellstickDevice)
|
||||
registeredDevices.remove(sensor);
|
||||
registeredDevices.remove(sensor);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -32,6 +32,12 @@ public class DimmerEventData extends HalEventData {
|
|||
private double dimmValue;
|
||||
|
||||
|
||||
public DimmerEventData() { }
|
||||
public DimmerEventData(double dimmValue) {
|
||||
this.dimmValue = dimmValue;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public double getData() {
|
||||
return dimmValue;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,18 @@ public class PowerConsumptionSensorData extends HalSensorData {
|
|||
private double wattHours;
|
||||
|
||||
|
||||
|
||||
public PowerConsumptionSensorData() { }
|
||||
public PowerConsumptionSensorData(double wattHours) {
|
||||
this.wattHours = wattHours;
|
||||
}
|
||||
public PowerConsumptionSensorData(long timestamp, double wattHours) {
|
||||
this(wattHours);
|
||||
super.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setConsumption(double wattHours){
|
||||
this.wattHours = wattHours;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,15 @@ public class TemperatureSensorData extends HalSensorData {
|
|||
public TemperatureSensorData(double temperature){
|
||||
this.temperature = temperature;
|
||||
}
|
||||
public TemperatureSensorData(long timestamp, double temperature){
|
||||
this(temperature);
|
||||
super.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param data the temperature to set in degrees C
|
||||
*/
|
||||
public void setData(double data){
|
||||
public void setTemperature(double data){
|
||||
this.temperature = data;
|
||||
}
|
||||
|
||||
|
|
@ -29,4 +33,5 @@ public class TemperatureSensorData extends HalSensorData {
|
|||
public double getData() {
|
||||
return temperature;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ public class TelstickSerialCommTest {
|
|||
//############ Normal TCs
|
||||
|
||||
@Test
|
||||
public void unregisteredEvent(){
|
||||
public void receiveUnregisteredEvent(){
|
||||
// Setup
|
||||
TellstickSerialComm tellstick = new TellstickSerialComm();
|
||||
final ArrayList<HalEventConfig> list = new ArrayList<>();
|
||||
|
|
@ -59,7 +59,7 @@ public class TelstickSerialCommTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void event(){
|
||||
public void receiveEvent(){
|
||||
// Setup
|
||||
TellstickSerialComm tellstick = new TellstickSerialComm();
|
||||
final ArrayList<HalEventConfig> list = new ArrayList<>();
|
||||
|
|
@ -81,6 +81,7 @@ public class TelstickSerialCommTest {
|
|||
|
||||
|
||||
|
||||
|
||||
private static class TestEvent extends TellstickProtocol implements HalEventConfig,TellstickDevice {
|
||||
public int testData;
|
||||
|
||||
|
|
@ -105,6 +106,5 @@ public class TelstickSerialCommTest {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object obj) {return testData == ((TestEvent)obj).testData;}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue