add parsing

This commit is contained in:
Wolfgang Hottgenroth 2020-09-07 13:39:22 +02:00
parent 648bec6654
commit fc5b930d9e
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
7 changed files with 208 additions and 24 deletions

View File

@ -0,0 +1,12 @@
package de.hottis.mbusMaster;
public class FinderOnePhasePowerMeter extends MbusDevice {
public FinderOnePhasePowerMeter(String name, byte address, int queryPeriod) {
super(name, address, queryPeriod);
this.dataPoints.add(new DataPoint("energy", 0));
this.dataPoints.add(new DataPoint("voltage", 2));
this.dataPoints.add(new DataPoint("current", 3));
this.dataPoints.add(new DataPoint("activePower", 4));
this.dataPoints.add(new DataPoint("reactivePower", 5));
}
}

View File

@ -0,0 +1,22 @@
package de.hottis.mbusMaster;
public class FinderThreePhasePowerMeter extends MbusDevice {
public FinderThreePhasePowerMeter(String name, byte address, int queryPeriod) {
super(name, address, queryPeriod);
this.dataPoints.add(new DataPoint("energy", 0));
this.dataPoints.add(new DataPoint("voltage1", 4));
this.dataPoints.add(new DataPoint("current1", 5));
this.dataPoints.add(new DataPoint("activePower1", 6));
this.dataPoints.add(new DataPoint("reactivePower1", 7));
this.dataPoints.add(new DataPoint("voltage2", 8));
this.dataPoints.add(new DataPoint("current2", 9));
this.dataPoints.add(new DataPoint("activePower2", 10));
this.dataPoints.add(new DataPoint("reactivePower2", 11));
this.dataPoints.add(new DataPoint("voltage3", 12));
this.dataPoints.add(new DataPoint("current3", 13));
this.dataPoints.add(new DataPoint("activePower3", 14));
this.dataPoints.add(new DataPoint("reactivePower3", 15));
this.dataPoints.add(new DataPoint("activePowerTotal", 16));
this.dataPoints.add(new DataPoint("reactivePowerTotal", 17));
}
}

View File

@ -0,0 +1,111 @@
package de.hottis.mbusMaster;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openmuc.jmbus.DataRecord;
import org.openmuc.jmbus.MBusMessage;
import org.openmuc.jmbus.VariableDataStructure;
import org.openmuc.jmbus.DecodingException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
abstract public class MbusDevice {
static final protected Logger logger = LogManager.getRootLogger();
private String name;
private byte address;
private int queryPeriod; // in seconds
protected List<DataRecord> dataRecords;
protected boolean validlyParsed;
static protected class DataPoint {
private String name;
private int index;
protected DataPoint(String name, int index) {
this.name = name;
this.index = index;
}
}
protected ArrayList<DataPoint> dataPoints;
protected MbusDevice(String name, byte address, int queryPeriod) {
this.name = name;
this.address = address;
this.queryPeriod = queryPeriod;
this.validlyParsed = false;
this.dataPoints = new ArrayList<>();
}
public void parse(byte[] frame) throws MbusException {
try {
MBusMessage mbusMsg = MBusMessage.decode(frame, frame.length);
VariableDataStructure variableDataStructure = mbusMsg.getVariableDataResponse();
variableDataStructure.decode();
this.dataRecords = variableDataStructure.getDataRecords();
logger.debug("Datarecords parsed");
this.validlyParsed = true;
} catch (IOException | DecodingException e) {
this.validlyParsed = false;
String msg = "Error while coding frame: " + e.toString();
logger.error(msg);
throw new MbusException(msg);
}
}
public String dataToString() {
StringBuffer sb = new StringBuffer();
for (DataPoint dp : this.dataPoints) {
sb.append("{");
sb.append(dp.name + ":" + this.dataRecords.get(dp.index).getScaledDataValue());
sb.append("}");
}
return sb.toString();
}
public String toString() {
return this.innerString(true);
}
public String shortString() {
return this.innerString(false);
}
private String innerString(boolean longOutput) {
StringBuffer sb = new StringBuffer();
sb.append(this.getClass().getName() + " [");
sb.append("<name=" + this.getName() + "><address=" + this.getAddress() + ">");
if (longOutput && this.validlyParsed) {
sb.append(this.dataToString());
}
sb.append("]");
return sb.toString();
}
public String getName() {
return this.name;
}
public byte getAddress() {
return this.address;
}
public double getValue(String dataPointName) throws MbusException {
if (! validlyParsed) {
throw new MbusException("trying to get value before valid parsing");
}
for (DataPoint dp : this.dataPoints) {
if (dataPointName.equals(dp.name)) {
return this.dataRecords.get(dp.index).getScaledDataValue();
}
}
throw new MbusException("dataPoint " + dataPointName + " in getValue not found");
}
}

View File

@ -0,0 +1,21 @@
package de.hottis.mbusMaster;
import java.io.IOException;
public class MbusException extends IOException {
public MbusException() {
super();
}
public MbusException(String message, Throwable cause) {
super(message, cause);
}
public MbusException(String message) {
super(message);
}
public MbusException(Throwable cause) {
super(cause);
}
}

View File

@ -35,48 +35,45 @@ public class MbusMaster {
logger.debug("Shutdown hook added");
*/
MbusgwChild mbusgw = new MbusgwChild(true);
MbusgwChild mbusgw = new MbusgwChild(false);
mbusgw.start();
byte[] devices = { (byte)84, (byte)87, (byte)82, (byte)83, (byte)80, (byte)85, (byte)86, (byte)81 };
MbusDevice[] devices = {
new FinderThreePhasePowerMeter("Total Electricity", (byte)80, 0),
new FinderOnePhasePowerMeter("Dryer", (byte)81, 0),
new FinderOnePhasePowerMeter("Laundry", (byte)82, 0),
new FinderOnePhasePowerMeter("Dishwasher", (byte)83, 0),
new FinderOnePhasePowerMeter("Light", (byte)84, 0),
new FinderOnePhasePowerMeter("Computer", (byte)85, 0),
new FinderOnePhasePowerMeter("Freezer", (byte)86, 0),
new FinderOnePhasePowerMeter("Fridge", (byte)87, 0)
};
int cnt = 0;
int errCnt = 0;
int successCnt = 0;
while (! stopSignal) {
System.out.println("--- " + cnt + " - " + successCnt + " - " + errCnt + " ---------------------------------------------------");
cnt++;
for (byte device : devices) {
System.out.println("Querying device " + device);
for (MbusDevice device : devices) {
System.out.println("Querying " + device.getName() + " meter");
try {
mbusgw.sendRequest((byte)0x5b, device);
mbusgw.sendRequest((byte)0x5b, device.getAddress());
byte[] frame = mbusgw.collectResponse();
MBusMessage mbusMsg = MBusMessage.decode(frame, frame.length);
VariableDataStructure variableDataStructure = mbusMsg.getVariableDataResponse();
variableDataStructure.decode();
List<DataRecord> dataRecords = variableDataStructure.getDataRecords();
device.parse(frame);
for (DataRecord dr : dataRecords) {
System.out.println(dr);
}
System.out.println(device);
/*
for (byte x : frame) {
System.out.print(Integer.toHexString(Byte.toUnsignedInt(x)) + " ");
}
*/
System.out.println();
successCnt++;
} catch (IOException e) {
errCnt++;
logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device);
logger.error("Error " + e.toString() + " in Meterbus dialog for device " + device.shortString());
}
}
// if (cnt >= 10) {
// break;
//}
System.out.println("--- " + cnt + " - " + successCnt + " - " + errCnt + " ---------------------------------------------------");
Thread.sleep(5*1000);
}

View File

@ -0,0 +1,21 @@
package de.hottis.mbusMaster;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class MbusScheduledQuerier {
static final Logger logger = LogManager.getRootLogger();
private ArrayList<MbusDevice> devices;
public MbusScheduledQuerier() {
this.devices = new ArrayList<>();
}
}

View File

@ -2,7 +2,7 @@ package de.hottis.mbusMaster;
import java.io.IOException;
public class MbusgwChildException extends IOException {
public class MbusgwChildException extends MbusException {
public MbusgwChildException() {
super();
}