load devices from properties file

This commit is contained in:
Wolfgang Hottgenroth 2020-09-08 16:20:19 +02:00
parent bf4cc13197
commit 87c9b5b2c8
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
6 changed files with 59 additions and 8 deletions

View File

@ -18,6 +18,7 @@ public class ConfigProperties extends Properties {
static final String PROPS_ERRORRATIOTHRESHOLD = "errorRatioThreshold";
static final String PROPS_ERRORRATIOCHECKTHRESHOLD = "errorRatioCheckThreshold";
static final String PROPS_LOOPSHUTDOWNDELAY = "loopShutdownDelay";
static final String PROPS_DEVICES = "mbus.device";
static final Logger logger = LogManager.getRootLogger();

View File

@ -1,6 +1,13 @@
package de.hottis.mbusMaster;
public class FinderOnePhasePowerMeter extends MbusDevice {
public FinderOnePhasePowerMeter(String name, Byte address, Integer queryPeriod) {
this(name, address.byteValue(), queryPeriod.intValue());
}
public FinderOnePhasePowerMeter(String name, byte address, int queryPeriod) {
super(name, address, queryPeriod);
this.dataPoints.add(new DataPoint("energy", 0));

View File

@ -1,6 +1,11 @@
package de.hottis.mbusMaster;
public class FinderThreePhasePowerMeter extends MbusDevice {
public FinderThreePhasePowerMeter(String name, Byte address, Integer queryPeriod) {
this(name, address.byteValue(), queryPeriod.intValue());
}
public FinderThreePhasePowerMeter(String name, byte address, int queryPeriod) {
super(name, address, queryPeriod);
this.dataPoints.add(new DataPoint("energy", 0));

View File

@ -39,6 +39,10 @@ abstract public class MbusDevice {
protected ArrayList<DataPoint> dataPoints;
protected MbusDevice(String name, Byte address, Integer queryPeriod) {
this(name, address.byteValue(), queryPeriod.intValue());
}
protected MbusDevice(String name, byte address, int queryPeriod) {
this.name = name;
this.address = address;

View File

@ -34,6 +34,7 @@ public class MbusMaster {
MbusScheduledQuerier querier = new MbusScheduledQuerier(config, queue);
querier.loadDevices();
querier.start();
DummyDequeuer ddq = new DummyDequeuer(queue);

View File

@ -3,8 +3,12 @@ package de.hottis.mbusMaster;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.Enumeration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -30,14 +34,43 @@ public class MbusScheduledQuerier extends Thread {
this.queue = queue;
this.devices = new ArrayList<>();
this.devices.add(new FinderThreePhasePowerMeter("Total Electricity", (byte)80, 0));
this.devices.add(new FinderOnePhasePowerMeter("Dryer", (byte)81, 0));
this.devices.add(new FinderOnePhasePowerMeter("Laundry", (byte)82, 0));
this.devices.add(new FinderOnePhasePowerMeter("Dishwasher", (byte)83, 0));
this.devices.add(new FinderOnePhasePowerMeter("Light", (byte)84, 0));
this.devices.add(new FinderOnePhasePowerMeter("Computer", (byte)85, 0));
this.devices.add(new FinderOnePhasePowerMeter("Freezer", (byte)86, 0));
this.devices.add(new FinderOnePhasePowerMeter("Fridge", (byte)87, 0));
}
public void loadDevices() throws MbusException {
String deviceClassName = null;
try {
@SuppressWarnings("unchecked")
Enumeration<String> propNames = (Enumeration<String>) config.propertyNames();
while (propNames.hasMoreElements()) {
String propName = propNames.nextElement();
if (propName.startsWith(ConfigProperties.PROPS_DEVICES)) {
String[] devicesConfigElements = config.getProperty(propName).split(",");
String name = devicesConfigElements[0];
byte addr = Byte.parseByte(devicesConfigElements[1]);
deviceClassName = devicesConfigElements[2];
int period = Integer.parseInt(devicesConfigElements[3]);
Class<?> klass = Class.forName(deviceClassName);
Constructor<?> constructor = klass.getConstructor(String.class, Byte.class, Integer.class);
MbusDevice device = (MbusDevice) constructor.newInstance(name, addr, period);
this.devices.add(device);
logger.info("Device " + name + " with class " + deviceClassName + ", address " + addr + ", period " + period + " loaded");
}
}
} catch (ClassNotFoundException e) {
String msg = "Device class " + deviceClassName + " not found when loading devices";
logger.error(msg);
throw new MbusException(msg, e);
} catch (NoSuchMethodException e) {
String msg = "Required constructor in device class " + deviceClassName + " not found when loading devices";
logger.error(msg);
throw new MbusException(msg, e);
} catch (InvocationTargetException | IllegalAccessException | InstantiationException e) {
String msg = "Error when instantiating device class " + deviceClassName + " when loading devices";
logger.error(msg);
throw new MbusException(msg, e);
}
}
public void setStopSignal() {