use of net.solarnetwork.node.io.mbus.MBusMessage in project solarnetwork-node by SolarNetwork.
the class JMBusConversion method from.
/**
* Convert a JMBus WMBus message
*
* @param message
* The JMBus WMBus message to convert
* @return A message
*/
public static MBusMessage from(WMBusMessage message) {
final MBusMessage msg = new MBusMessage(Instant.now());
msg.status = message.getVariableDataResponse().getStatus();
final VariableDataStructure vds = message.getVariableDataResponse();
try {
vds.decode();
} catch (DecodingException e) {
log.error("Failed to decode JMBus variable data structure: {}", e.getMessage());
}
for (DataRecord record : vds.getDataRecords()) {
final MBusDataRecord rec = from(record);
if (rec != null) {
msg.dataRecords.add(rec);
}
}
msg.moreRecordsFollow = message.getVariableDataResponse().moreRecordsFollow();
return msg;
}
use of net.solarnetwork.node.io.mbus.MBusMessage in project solarnetwork-node by SolarNetwork.
the class WMBusDatumDataSourceTests method readDatumWithStatusInteger.
@Test
public void readDatumWithStatusInteger() throws IOException {
// GIVEN
MBusPropertyConfig[] propConfigs = new MBusPropertyConfig[] { new MBusPropertyConfig(TEST_STATUS_PROP_NAME, Status) };
dataSource.setPropConfigs(propConfigs);
final MBusMessage msg = new MBusMessage(Instant.now());
final int expected = 7;
msg.status = expected;
// WHEN
dataSource.handleMessage(msg);
NodeDatum datum = dataSource.readCurrentDatum();
// THEN
assertThat("Datum returned", datum, notNullValue());
assertThat("Created", datum.getTimestamp(), notNullValue());
assertThat("Status", datum.asSampleOperations().getSampleInteger(Status, TEST_STATUS_PROP_NAME), equalTo(expected));
}
use of net.solarnetwork.node.io.mbus.MBusMessage in project solarnetwork-node by SolarNetwork.
the class WMBusDatumDataSourceTests method readDatumWithInstantaneousValues.
@Test
public void readDatumWithInstantaneousValues() throws IOException {
// GIVEN
MBusPropertyConfig[] propConfigs = new MBusPropertyConfig[] { new MBusPropertyConfig(TEST_BCD_PROP_NAME, Instantaneous, MBusDataType.BCD, MBusDataDescription.Volume, BigDecimal.ONE, 3), new MBusPropertyConfig(TEST_DOUBLE_PROP_NAME, Instantaneous, MBusDataType.Double, MBusDataDescription.Energy, BigDecimal.ONE, 3), new MBusPropertyConfig(TEST_LONG_PROP_NAME, Instantaneous, MBusDataType.Long, MBusDataDescription.Current, BigDecimal.ONE, 0) };
dataSource.setPropConfigs(propConfigs);
final MBusMessage msg = new MBusMessage(Instant.now());
msg.dataRecords.add(new MBusDataRecord(MBusDataDescription.Volume, MBusDataType.BCD, 27L, -3));
msg.dataRecords.add(new MBusDataRecord(MBusDataDescription.Energy, 1.234, 0));
msg.dataRecords.add(new MBusDataRecord(MBusDataDescription.Current, MBusDataType.Long, 874234L, 0));
// WHEN
dataSource.handleMessage(msg);
NodeDatum datum = dataSource.readCurrentDatum();
// THEN
assertThat("Datum returned", datum, notNullValue());
assertThat("Created", datum.getTimestamp(), notNullValue());
assertThat("Source ID", datum.getSourceId(), equalTo(TEST_SOURCE_ID));
assertThat("BCD value", datum.asSampleOperations().getSampleDouble(Instantaneous, TEST_BCD_PROP_NAME), equalTo(0.027));
assertThat("Double value", datum.asSampleOperations().getSampleDouble(Instantaneous, TEST_DOUBLE_PROP_NAME), equalTo(1.234));
assertThat("Long value", datum.asSampleOperations().getSampleDouble(Instantaneous, TEST_LONG_PROP_NAME), equalTo(874234.0));
}
use of net.solarnetwork.node.io.mbus.MBusMessage in project solarnetwork-node by SolarNetwork.
the class WMBusNetworkTests method receiveMessage.
@Test
public void receiveMessage() throws IOException, DecodingException {
final byte[] bytes = copyToByteArray(WMBusNetworkTests.class.getResourceAsStream("wmbus-message.bin"));
final WMBusMessage msg = WMBusMessageDecoder.decode(bytes, 0, keyMap);
final MBusData expected = new MBusData(Instant.now());
expected.dataRecords.add(new MBusDataRecord(MBusDataDescription.Volume, MBusDataType.BCD, 27L, -3));
expected.dataRecords.add(new MBusDataRecord(MBusDataDescription.DateTime, Instant.ofEpochMilli(1593064440000L)));
MBusMessageHandler messageHandler = EasyMock.createMock(MBusMessageHandler.class);
messageHandler.handleMessage(new MBusMessage(expected));
expectLastCall();
replay(messageHandler);
conn.open(messageHandler);
network.newMessage(msg);
verify(messageHandler);
}
use of net.solarnetwork.node.io.mbus.MBusMessage in project solarnetwork-node by SolarNetwork.
the class JMBusWMBusNetwork method newMessage.
@Override
public void newMessage(org.openmuc.jmbus.wireless.WMBusMessage message) {
SecondaryAddress addr = message.getSecondaryAddress();
log.debug("JMBus data received from secondary address {}: {}", addr, message);
// route message to all registered listeners
if (addr != null) {
Set<MBusMessageHandler> handlers = listeners.get(addr);
if (handlers != null) {
MBusMessage msg = JMBusConversion.from(message);
for (MBusMessageHandler handler : handlers) {
handler.handleMessage(msg);
}
}
}
}
Aggregations