Split switch event data into multiple classes

This commit is contained in:
Ziver Koc 2021-01-02 04:25:04 +01:00
parent 4499824965
commit 6214c01157
23 changed files with 259 additions and 96 deletions

View file

@ -40,7 +40,7 @@ public class DeviceTraitFactory {
public static DeviceTrait[] getTraits(Sensor sensor) {
switch (sensor.getDeviceData().getClass().getName()) {
case "se.hal.struct.devicedata.DimmerEventData":
case "se.hal.struct.devicedata.SwitchEventData":
case "se.hal.struct.devicedata.OnOffEventData":
return new DeviceTrait[]{};
case "se.hal.struct.devicedata.PowerConsumptionSensorData":

View file

@ -149,7 +149,7 @@ public enum DeviceType {
public static DeviceType getType(Sensor sensor) {
switch (sensor.getDeviceData().getClass().getName()) {
case "se.hal.struct.devicedata.DimmerEventData":
case "se.hal.struct.devicedata.SwitchEventData":
case "se.hal.struct.devicedata.OnOffEventData":
return LIGHT;
case "se.hal.struct.devicedata.PowerConsumptionSensorData":

View file

@ -1,16 +1,14 @@
package se.hal.plugin.dummy;
import se.hal.intf.*;
import se.hal.struct.devicedata.HumiditySensorData;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.TemperatureSensorData;
import se.hal.struct.devicedata.OnOffEventData;
public class DummySwitchEvent implements DummyDevice, HalEventConfig {
@Override
public HalDeviceData generateData() {
return new SwitchEventData(
return new OnOffEventData(
(int) (Math.random() * 10) < 5,
System.currentTimeMillis()
);
@ -24,6 +22,6 @@ public class DummySwitchEvent implements DummyDevice, HalEventConfig {
@Override
public Class<? extends HalEventData> getEventDataClass() {
return SwitchEventData.class;
return OnOffEventData.class;
}
}

View file

@ -2,7 +2,8 @@ package se.hal.plugin.netscan;
import se.hal.HalContext;
import se.hal.intf.*;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.AvailabilityEventData;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.InetUtil;
import zutil.log.LogUtil;
import zutil.net.InetScanner;
@ -27,7 +28,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
private ScheduledExecutorService executor;
private HalEventReportListener listener;
/** A register and a cache of previous state **/
private HashMap<NetworkDevice,SwitchEventData> devices = new HashMap<>();
private HashMap<NetworkDevice, AvailabilityEventData> devices = new HashMap<>();
@ -61,9 +62,9 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
@Override
public void run() {
try(MultiCommandExecutor executor = new MultiCommandExecutor()){
for (Map.Entry<NetworkDevice,SwitchEventData> entry : devices.entrySet()) {
for (Map.Entry<NetworkDevice, AvailabilityEventData> entry : devices.entrySet()) {
NetworkDevice device = entry.getKey();
SwitchEventData prevData = entry.getValue();
AvailabilityEventData prevData = entry.getValue();
if (listener != null) {
// We ping two times to increase reliability
boolean ping = false;
@ -72,10 +73,10 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
ping |= InetScanner.isReachable(device.getHost(), executor);
// Should we report?
if (prevData == null || prevData.isOn() != ping) {
SwitchEventData newData = new SwitchEventData(ping, System.currentTimeMillis());
if (prevData == null || prevData.isAvailable() != ping) {
AvailabilityEventData newData = new AvailabilityEventData(ping, System.currentTimeMillis());
entry.setValue(newData);
logger.fine("IP "+device.getHost() +" state has changed to "+ newData.isOn());
logger.fine("IP " + device.getHost() + " state has changed to " + newData);
listener.reportReceived(device, newData);
}
}
@ -90,7 +91,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
if (listener != null)
listener.reportReceived(
new NetworkDevice(ip.getHostAddress()),
new SwitchEventData(true, System.currentTimeMillis()));
new OnOffEventData(true, System.currentTimeMillis()));
}

View file

@ -3,7 +3,7 @@ package se.hal.plugin.netscan;
import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController;
import se.hal.intf.HalEventData;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.ui.Configurator;
public class NetworkDevice implements HalEventConfig {
@ -40,6 +40,6 @@ public class NetworkDevice implements HalEventConfig {
}
@Override
public Class<? extends HalEventData> getEventDataClass() {
return SwitchEventData.class;
return OnOffEventData.class;
}
}

View file

@ -25,14 +25,11 @@ package se.hal.plugin.tellstick.device;
import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController;
import se.hal.intf.HalEventData;
import se.hal.intf.HalSensorData;
import se.hal.plugin.tellstick.TellstickDevice;
import se.hal.plugin.tellstick.TellstickDeviceGroup;
import se.hal.plugin.tellstick.TellstickSerialComm;
import se.hal.plugin.tellstick.protocol.NexaSelfLearningProtocol;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.TemperatureSensorData;
import zutil.parser.binary.BinaryStruct;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.ui.Configurator;
/**
@ -109,7 +106,7 @@ public class NexaSelfLearning implements HalEventConfig,TellstickDevice,Tellstic
}
@Override
public Class<? extends HalEventData> getEventDataClass() {
return SwitchEventData.class;
return OnOffEventData.class;
}
@Override

View file

@ -26,11 +26,9 @@ import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController;
import se.hal.intf.HalEventData;
import se.hal.plugin.tellstick.TellstickDevice;
import se.hal.plugin.tellstick.TellstickDeviceGroup;
import se.hal.plugin.tellstick.TellstickSerialComm;
import se.hal.plugin.tellstick.protocol.NexaSelfLearningProtocol;
import se.hal.struct.devicedata.DimmerEventData;
import se.hal.struct.devicedata.SwitchEventData;
import zutil.ui.Configurator;
/**

View file

@ -24,14 +24,13 @@ package se.hal.plugin.tellstick.protocol;
import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventData;
import se.hal.plugin.tellstick.TellstickSerialComm;
import se.hal.plugin.tellstick.cmd.TellstickCmd;
import se.hal.plugin.tellstick.cmd.TellstickCmdExtendedSend;
import se.hal.plugin.tellstick.TellstickProtocol;
import se.hal.plugin.tellstick.device.NexaSelfLearning;
import se.hal.plugin.tellstick.device.NexaSelfLearningDimmer;
import se.hal.struct.devicedata.DimmerEventData;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.ByteUtil;
import zutil.log.LogUtil;
import zutil.parser.binary.BinaryStruct;
@ -84,8 +83,8 @@ public class NexaSelfLearningProtocol extends TellstickProtocol {
logger.severe("Device config is not instance of NexaSelfLearning: "+deviceConfig.getClass());
return null;
}
if ( ! (deviceData instanceof SwitchEventData || deviceData instanceof DimmerEventData)){
logger.severe("Device data is not an instance of SwitchEventData or DimmerEventData: "+deviceData.getClass());
if ( ! (deviceData instanceof OnOffEventData || deviceData instanceof DimmerEventData)){
logger.severe("Device data is not an instance of OnOffEventData or DimmerEventData: "+deviceData.getClass());
return null;
}
@ -102,7 +101,7 @@ public class NexaSelfLearningProtocol extends TellstickProtocol {
struct.house = ((NexaSelfLearning) deviceConfig).getHouse();
struct.group = ((NexaSelfLearning) deviceConfig).getGroup();
struct.unit = ((NexaSelfLearning) deviceConfig).getUnit();
struct.enable = ((SwitchEventData) deviceData).isOn();
struct.enable = ((OnOffEventData) deviceData).isOn();
}
@ -163,7 +162,7 @@ public class NexaSelfLearningProtocol extends TellstickProtocol {
*/
list.add(new TellstickDecodedEntry(
new NexaSelfLearning(struct.house, struct.group, struct.unit),
new SwitchEventData(struct.enable, System.currentTimeMillis())
new OnOffEventData(struct.enable, System.currentTimeMillis())
));
/* }*/
return list;

View file

@ -4,8 +4,6 @@ import org.junit.Before;
import org.junit.Test;
import se.hal.intf.*;
import se.hal.struct.devicedata.DimmerEventData;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.TemperatureSensorData;
import zutil.converter.Converter;
import java.util.ArrayList;

View file

@ -1,8 +1,7 @@
package se.hal.plugin.tellstick;
import se.hal.plugin.tellstick.device.NexaSelfLearning;
import se.hal.plugin.tellstick.protocol.NexaSelfLearningProtocol;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.OnOffEventData;
/**
* Created by Ziver on 2015-11-19.
@ -25,7 +24,7 @@ public class TelstickSerialCommNexaOnOffTest {
nexaDevice.setGroup(false);
nexaDevice.setUnit(1);
SwitchEventData nexaData = new SwitchEventData();
OnOffEventData nexaData = new OnOffEventData();
System.out.println("Up and Running");
while(true) {

View file

@ -3,7 +3,6 @@ package se.hal.plugin.tellstick;
import org.junit.Before;
import org.junit.Test;
import se.hal.intf.*;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.TemperatureSensorData;
import zutil.converter.Converter;
@ -21,7 +20,7 @@ public class TelstickSerialCommSensorTest {
public void init(){
TellstickParser.registerProtocol(TestSensor.class);
}
//############ Normal TCs

View file

@ -22,12 +22,11 @@
package se.hal.plugin.tellstick.protocol;
import se.hal.plugin.tellstick.TellstickProtocol;
import se.hal.plugin.tellstick.TellstickProtocol.TellstickDecodedEntry;
import se.hal.plugin.tellstick.device.NexaSelfLearning;
import se.hal.plugin.tellstick.device.NexaSelfLearningDimmer;
import se.hal.struct.devicedata.DimmerEventData;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.OnOffEventData;
import zutil.converter.Converter;
import java.nio.charset.StandardCharsets;
@ -42,7 +41,7 @@ public class NexaSelfLearningTest {
NexaSelfLearning nexaDevice = new NexaSelfLearning();
nexaDevice.setHouse(11_772_006);
nexaDevice.setUnit(3);
SwitchEventData nexaData = new SwitchEventData(true, System.currentTimeMillis());
OnOffEventData nexaData = new OnOffEventData(true, System.currentTimeMillis());
byte[] expected = Converter.toBytes(new char[]{
84, // T
@ -99,7 +98,7 @@ public class NexaSelfLearningTest {
assertEquals("House Code", 11772006, ((NexaSelfLearning)nexa.getDevice()).getHouse());
assertEquals("Unit Code", 0, ((NexaSelfLearning)nexa.getDevice()).getUnit());
assertTrue("Enabled", ((SwitchEventData)nexa.getData()).isOn());
assertTrue("Enabled", ((OnOffEventData)nexa.getData()).isOn());
}
@org.junit.Test
@ -108,7 +107,7 @@ public class NexaSelfLearningTest {
assertEquals("House Code", 11772006, ((NexaSelfLearning)nexa.getDevice()).getHouse());
assertEquals("Unit Code", 0, ((NexaSelfLearning)nexa.getDevice()).getUnit());
assertFalse("Enabled", ((SwitchEventData)nexa.getData()).isOn());
assertFalse("Enabled", ((OnOffEventData)nexa.getData()).isOn());
}
private TellstickDecodedEntry decode(String data){