Use dataObjects

This commit is contained in:
Wolfgang Hottgenroth 2020-09-07 18:15:49 +02:00
parent 251769120e
commit e2a505f780
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
8 changed files with 119 additions and 8 deletions

View File

@ -0,0 +1,77 @@
package de.hottis.mbusMaster;
import java.io.Serializable;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public abstract class ADataObject implements Serializable {
private static final long serialVersionUID = 1L;
static final String KIND_KEY = "kind";
final protected Logger logger = LogManager.getRootLogger();
private String name;
private Map<String, Object> values;
private String kind;
public ADataObject(String name, String kind) {
this.name = name;
this.kind = kind;
}
public void setValues(Map<String, Object> values) {
this.values = values;
}
abstract public String getTableName();
public Map<String, Object> getValues() {
return values;
}
public String getName() {
return name;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("{\"name\":\"");
sb.append(name);
sb.append("\", \"kind\":\"");
sb.append(kind);
sb.append("\", \"values\":{");
boolean first = true;
for (Map.Entry<String, Object> entry : values.entrySet()) {
if (! first) {
sb.append(", ");
} else {
first = false;
}
sb.append("\"");
sb.append(entry.getKey());
sb.append("\":");
Object value = entry.getValue();
if (! (value instanceof Double)) {
sb.append("\"");
}
sb.append(value);
if (! (value instanceof Double)) {
sb.append("\"");
}
}
sb.append("}}");
/*
sb.append("<");
sb.append(name);
sb.append(", ");
sb.append(timestamp);
sb.append(", ");
sb.append(values.toString());
sb.append(">");
*/
return sb.toString();
}
}

View File

@ -8,9 +8,9 @@ import org.apache.logging.log4j.Logger;
public class DummyDequeuer extends Thread {
static final Logger logger = LogManager.getRootLogger();
private BlockingQueue<String> queue;
private BlockingQueue<ADataObject> queue;
public DummyDequeuer(BlockingQueue<String> queue) {
public DummyDequeuer(BlockingQueue<ADataObject> queue) {
super("DummyDequeuer");
this.queue = queue;
@ -19,8 +19,8 @@ public class DummyDequeuer extends Thread {
public void run() {
while(true) {
try {
String o = this.queue.take();
System.out.println("DummyDequeuer: " + o);
ADataObject o = this.queue.take();
System.out.println("DummyDequeuer: " + o.toString());
} catch (InterruptedException e) {
}
}

View File

@ -0,0 +1,24 @@
package de.hottis.mbusMaster;
import java.util.HashMap;
public class ElectricEnergyDataObject extends ADataObject {
private static final long serialVersionUID = 1L;
static final String ENERGY_KEY = "energy";
static final String POWER_KEY = "power";
static final String TABLE_NAME = "ElectricEnergy";
static final String KIND_NAME = "ElectricEnergy";
public ElectricEnergyDataObject(String name, double energy, double power) {
super(name, KIND_NAME);
HashMap<String, Object> values = new HashMap<String, Object>();
values.put(ENERGY_KEY, energy);
values.put(POWER_KEY, power);
setValues(values);
}
@Override
public String getTableName() {
return TABLE_NAME;
}
}

View File

@ -9,4 +9,8 @@ public class FinderOnePhasePowerMeter extends MbusDevice {
this.dataPoints.add(new DataPoint("activePower", 4));
this.dataPoints.add(new DataPoint("reactivePower", 5));
}
public ADataObject getDataObject() throws MbusException {
return new ElectricEnergyDataObject(this.getName(), this.getValue("energy"), this.getValue("activePower"));
}
}

View File

@ -19,4 +19,8 @@ public class FinderThreePhasePowerMeter extends MbusDevice {
this.dataPoints.add(new DataPoint("activePowerTotal", 16));
this.dataPoints.add(new DataPoint("reactivePowerTotal", 17));
}
public ADataObject getDataObject() throws MbusException {
return new ElectricEnergyDataObject(this.getName(), this.getValue("energy"), this.getValue("activePowerTotal"));
}
}

View File

@ -108,4 +108,6 @@ abstract public class MbusDevice {
}
throw new MbusException("dataPoint " + dataPointName + " in getValue not found");
}
abstract public ADataObject getDataObject() throws MbusException;
}

View File

@ -30,7 +30,7 @@ public class MbusMaster {
logger.debug("Shutdown hook added");
*/
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
LinkedBlockingQueue<ADataObject> queue = new LinkedBlockingQueue<>();
MbusScheduledQuerier querier = new MbusScheduledQuerier(config, queue);

View File

@ -16,9 +16,9 @@ public class MbusScheduledQuerier extends Thread {
private ArrayList<MbusDevice> devices;
private boolean stopSignal = false;
private ConfigProperties config;
private BlockingQueue<String> queue;
private BlockingQueue<ADataObject> queue;
public MbusScheduledQuerier(ConfigProperties config, BlockingQueue<String> queue) {
public MbusScheduledQuerier(ConfigProperties config, BlockingQueue<ADataObject> queue) {
super("MbusScheduledQuerier");
this.config = config;
@ -58,7 +58,7 @@ public class MbusScheduledQuerier extends Thread {
device.parse(frame);
logger.info("Got: " + device.toString());
this.queue.add(device.toString());
this.queue.add(device.getDataObject());
successCnt++;
} catch (IOException e) {