status output

This commit is contained in:
2020-09-11 11:11:50 +02:00
parent 7fac8b23d0
commit 9cf2c00771
7 changed files with 83 additions and 15 deletions

View File

@ -16,10 +16,14 @@ public abstract class ADataObject implements Serializable {
private String name; private String name;
private Map<String, Object> values; private Map<String, Object> values;
private String kind; private String kind;
private String status;
private String statusText;
public ADataObject(String name, String kind) { public ADataObject(String name, String kind, String status, String statusText) {
this.name = name; this.name = name;
this.kind = kind; this.kind = kind;
this.status = status;
this.statusText = statusText;
} }
public void setValues(Map<String, Object> values) { public void setValues(Map<String, Object> values) {
@ -44,12 +48,24 @@ public abstract class ADataObject implements Serializable {
return this.kind; return this.kind;
} }
public String getStatus() {
return this.status;
}
public String getStatusText() {
return this.statusText;
}
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append("{\"name\":\""); sb.append("{\"name\":\"");
sb.append(name); sb.append(this.name);
sb.append("\", \"kind\":\""); sb.append("\", \"kind\":\"");
sb.append(kind); sb.append(this.kind);
sb.append("\", \"status\":\"");
sb.append(this.status);
sb.append("\", \"statusText\":\"");
sb.append(this.statusText);
sb.append("\", \"values\":{"); sb.append("\", \"values\":{");
boolean first = true; boolean first = true;
for (Map.Entry<String, Object> entry : values.entrySet()) { for (Map.Entry<String, Object> entry : values.entrySet()) {

View File

@ -10,8 +10,8 @@ public class ElectricEnergyDataObject extends ADataObject {
static final String TABLE_NAME = "ElectricEnergy"; static final String TABLE_NAME = "ElectricEnergy";
static final String KIND_NAME = "ElectricEnergy"; static final String KIND_NAME = "ElectricEnergy";
public ElectricEnergyDataObject(String name, double energy, double power, double errorRatio) { public ElectricEnergyDataObject(String name, String status, String statusText, double energy, double power, double errorRatio) {
super(name, KIND_NAME); super(name, KIND_NAME, status, statusText);
HashMap<String, Object> values = new HashMap<String, Object>(); HashMap<String, Object> values = new HashMap<String, Object>();
values.put(ENERGY_KEY, energy); values.put(ENERGY_KEY, energy);
values.put(POWER_KEY, power); values.put(POWER_KEY, power);

View File

@ -18,6 +18,8 @@ public class FinderOnePhasePowerMeter extends MbusDevice {
} }
public ADataObject getDataObject() throws MbusException { public ADataObject getDataObject() throws MbusException {
return new ElectricEnergyDataObject(this.getName(), this.getValue("energy"), this.getValue("activePower"), this.getErrorRatio()); return new ElectricEnergyDataObject(this.getName(), this.getStatus(), this.getStatusText(),
this.getValue("energy"), this.getValue("activePower"),
this.getErrorRatio());
} }
} }

View File

@ -26,6 +26,8 @@ public class FinderThreePhasePowerMeter extends MbusDevice {
} }
public ADataObject getDataObject() throws MbusException { public ADataObject getDataObject() throws MbusException {
return new ElectricEnergyDataObject(this.getName(), this.getValue("energy"), this.getValue("activePowerTotal"), this.getErrorRatio()); return new ElectricEnergyDataObject(this.getName(), this.getStatus(), this.getStatusText(),
this.getValue("energy"), this.getValue("activePowerTotal"),
this.getErrorRatio());
} }
} }

View File

@ -23,8 +23,10 @@ abstract public class MbusDevice {
private int successCnt; private int successCnt;
private int errorCnt; private int errorCnt;
private String errorStatus = null;
protected List<DataRecord> dataRecords; protected List<DataRecord> dataRecords = null;
protected ArrayList<DataRecord> lastDataRecords;
protected boolean validlyParsed; protected boolean validlyParsed;
static protected class DataPoint { static protected class DataPoint {
@ -70,6 +72,42 @@ abstract public class MbusDevice {
return (double)this.errorCnt / (double)(this.successCnt + this.errorCnt); return (double)this.errorCnt / (double)(this.successCnt + this.errorCnt);
} }
public void setErrorStatus(String errorStatus) {
this.errorStatus = errorStatus;
}
public String getStatus() {
String status = "?";
if (this.validlyParsed) {
status = "good";
} else {
status = "bad";
}
return status;
}
public String getStatusText() {
String status = "?";
if (this.validlyParsed) {
status = "-";
} else {
if (this.errorStatus == null) {
status = "unknown cause";
} else {
status = this.errorStatus;
}
}
return status;
}
public void preParse() {
if (this.dataRecords != null) {
this.lastDataRecords = new ArrayList<>(this.dataRecords);
}
this.errorStatus = null;
this.validlyParsed = false;
}
public void parse(byte[] frame) throws MbusException { public void parse(byte[] frame) throws MbusException {
try { try {
MBusMessage mbusMsg = MBusMessage.decode(frame, frame.length); MBusMessage mbusMsg = MBusMessage.decode(frame, frame.length);
@ -109,6 +147,7 @@ abstract public class MbusDevice {
sb.append(this.getClass().getName() + " ["); sb.append(this.getClass().getName() + " [");
sb.append("<name=" + this.getName() + "><address=" + this.getAddress() + ">"); sb.append("<name=" + this.getName() + "><address=" + this.getAddress() + ">");
sb.append("<successCnt=" + this.successCnt + "><errorCnt=" + this.errorCnt + ">"); sb.append("<successCnt=" + this.successCnt + "><errorCnt=" + this.errorCnt + ">");
sb.append("<status=" + this.getStatus() + "><statusText>=" + this.getStatusText() + ">");
if (longOutput && this.validlyParsed) { if (longOutput && this.validlyParsed) {
sb.append(this.dataToString()); sb.append(this.dataToString());
} }
@ -125,12 +164,16 @@ abstract public class MbusDevice {
} }
public double getValue(String dataPointName) throws MbusException { public double getValue(String dataPointName) throws MbusException {
if (! validlyParsed) {
throw new MbusException("trying to get value before valid parsing");
}
for (DataPoint dp : this.dataPoints) { for (DataPoint dp : this.dataPoints) {
if (dataPointName.equals(dp.name)) { if (dataPointName.equals(dp.name)) {
if (validlyParsed) {
return this.dataRecords.get(dp.index).getScaledDataValue(); return this.dataRecords.get(dp.index).getScaledDataValue();
} else {
if (this.lastDataRecords == null) {
throw new MbusException("trying to get value before any valid parsing");
}
return this.lastDataRecords.get(dp.index).getScaledDataValue();
}
} }
} }
throw new MbusException("dataPoint " + dataPointName + " in getValue not found"); throw new MbusException("dataPoint " + dataPointName + " in getValue not found");

View File

@ -103,19 +103,24 @@ public class MbusScheduledQuerier extends Thread {
for (MbusDevice device : this.devices) { for (MbusDevice device : this.devices) {
logger.info("Querying " + device.getName() + " meter"); logger.info("Querying " + device.getName() + " meter");
try { try {
device.preParse();
mbusgw.sendRequest((byte)0x5b, device.getAddress()); mbusgw.sendRequest((byte)0x5b, device.getAddress());
byte[] frame = mbusgw.collectResponse(); byte[] frame = mbusgw.collectResponse();
device.parse(frame); device.parse(frame);
logger.info("Got: " + device.toString()); logger.info("Got: " + device.toString());
device.incSuccessCnt(); device.incSuccessCnt();
this.queue.add(device.getDataObject());
successCnt++; successCnt++;
} catch (IOException e) { } catch (IOException e) {
device.incErrorCnt(); device.incErrorCnt();
errCnt++; device.setErrorStatus(e.toString());
logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device.shortString()); logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device.shortString());
errCnt++;
} finally {
this.queue.add(device.getDataObject());
} }
maxErrorRatio = (maxErrorRatio > device.getErrorRatio()) ? maxErrorRatio : device.getErrorRatio(); maxErrorRatio = (maxErrorRatio > device.getErrorRatio()) ? maxErrorRatio : device.getErrorRatio();
} }

View File

@ -17,7 +17,7 @@ public class MbusStatisticsDataObject extends ADataObject {
public MbusStatisticsDataObject(String name, int cycle, int error, int success, int shutdowns, public MbusStatisticsDataObject(String name, int cycle, int error, int success, int shutdowns,
double maxErrorRatio, double meanErrorRatio, int timeSinceLastShutdown, int meantimeBetweenShutdowns) { double maxErrorRatio, double meanErrorRatio, int timeSinceLastShutdown, int meantimeBetweenShutdowns) {
super(name, KIND_NAME); super(name, KIND_NAME, "good", "-");
HashMap<String, Object> values = new HashMap<String, Object>(); HashMap<String, Object> values = new HashMap<String, Object>();
values.put(CYCLE_CNT_KEY, cycle); values.put(CYCLE_CNT_KEY, cycle);
values.put(ERROR_CNT_KEY, error); values.put(ERROR_CNT_KEY, error);