use of org.openmuc.jdlms.datatypes.BitString in project open-smart-grid-platform by OSGP.
the class GetConfigurationObjectServiceSmr5 method getConfigurationObject.
@Override
ConfigurationObjectDto getConfigurationObject(final GetResult result) throws ProtocolAdapterException {
final DataObject resultData = result.getResultData();
if (resultData == null || !resultData.isBitString()) {
LOGGER.warn("Configuration object result data is not a BitString: {}", resultData);
throw new ProtocolAdapterException("Expected bit-string data as Configuration object result data, but got: " + (resultData == null ? "null" : resultData.getType()));
}
LOGGER.info("SMR5 Configuration object current BitString: {}", this.dlmsHelper.getDebugInfo(resultData));
final BitString bitString = resultData.getValue();
final byte[] flagByteArray = bitString.getBitString();
final List<ConfigurationFlagDto> configurationFlagDtos = this.toConfigurationFlags(flagByteArray);
final ConfigurationFlagsDto configurationFlagsDto = new ConfigurationFlagsDto(configurationFlagDtos);
return new ConfigurationObjectDto(configurationFlagsDto);
}
use of org.openmuc.jdlms.datatypes.BitString in project open-smart-grid-platform by OSGP.
the class SetConfigurationObjectServiceDsmr4 method addFlags.
private void addFlags(final ConfigurationObjectDto configurationToSet, final ConfigurationObjectDto configurationOnDevice, final List<DataObject> dataObjects) throws ProtocolAdapterException {
final BitString flags = this.getFlags(configurationToSet, configurationOnDevice);
final DataObject bitString = DataObject.newBitStringData(flags);
dataObjects.add(bitString);
}
use of org.openmuc.jdlms.datatypes.BitString in project open-smart-grid-platform by OSGP.
the class GetConfigurationObjectServiceDsmr4 method getConfigurationFlags.
private ConfigurationFlagsDto getConfigurationFlags(final DataObject flags) throws ProtocolAdapterException {
if (flags == null || !flags.isBitString()) {
final String message = String.format("Expected ConfigurationObject flags as BitString, but got: %s", flags);
LOGGER.warn(message);
throw new ProtocolAdapterException(message);
}
final BitString bitString = flags.getValue();
final byte[] flagBytes = bitString.getBitString();
final List<ConfigurationFlagDto> configurationFlags = this.toConfigurationFlags(flagBytes);
return new ConfigurationFlagsDto(configurationFlags);
}
use of org.openmuc.jdlms.datatypes.BitString in project open-smart-grid-platform by OSGP.
the class DlmsAttributeValuesClient method asBitString.
private static BitString asBitString(final JsonNode valueNode) {
String bitStringText = valueNode.textValue();
if (bitStringText == null) {
throw new IllegalArgumentException("JSON node for BitString must have a text value: " + valueNode);
}
final int numBits = bitStringText.length();
if (numBits == 0) {
return new BitString(new byte[0], 0);
}
if (numBits % 8 != 0) {
final String trailingZerosFormat = "%0" + (8 - (numBits % 8)) + "d";
bitStringText = bitStringText + String.format(trailingZerosFormat, 0);
}
final int numBytes = bitStringText.length() / 8;
final byte[] bitStringBytes = new byte[numBytes];
for (int i = 0; i < numBytes; i++) {
final String bytePart = bitStringText.substring(i * 8, (i + 1) * 8);
bitStringBytes[i] = Byte.parseByte(bytePart, 2);
}
return new BitString(bitStringBytes, numBits);
}
use of org.openmuc.jdlms.datatypes.BitString in project open-smart-grid-platform by OSGP.
the class SetConfigurationObjectCommandExecutorDsmr4IT method execute.
@Test
public void execute() throws IOException, ProtocolAdapterException {
// SETUP
// configurationToSet: 0111-----1------
final GprsOperationModeTypeDto gprsModeToSet = GprsOperationModeTypeDto.TRIGGERED;
final ConfigurationObjectDto configurationToSet = this.createConfigurationObjectDto(gprsModeToSet, this.createFlagDto(ConfigurationFlagTypeDto.DISCOVER_ON_OPEN_COVER, false), this.createFlagDto(ConfigurationFlagTypeDto.DISCOVER_ON_POWER_ON, true), this.createFlagDto(ConfigurationFlagTypeDto.DYNAMIC_MBUS_ADDRESS, true), this.createFlagDto(ConfigurationFlagTypeDto.PO_ENABLE, true), this.createFlagDto(ConfigurationFlagTypeDto.HLS_5_ON_PO_ENABLE, true));
// flagsOnDevice: 1000101010000000
final byte[] flagsOnDevice = this.createFlagBytes(ConfigurationFlagTypeDto.DISCOVER_ON_OPEN_COVER, ConfigurationFlagTypeDto.HLS_3_ON_P_3_ENABLE, ConfigurationFlagTypeDto.HLS_5_ON_P_3_ENABLE, ConfigurationFlagTypeDto.HLS_4_ON_PO_ENABLE);
// result of merging configurationToSet and flagsOnDevice
final byte firstExpectedByte = this.asByte("01111010");
final byte secondExpectedByte = this.asByte("11000000");
final GprsOperationModeTypeDto gprsModeOnDevice = GprsOperationModeTypeDto.ALWAYS_ON;
final DataObject deviceData = this.createStructureData(flagsOnDevice, gprsModeOnDevice);
when(this.getResult.getResultData()).thenReturn(deviceData);
final DlmsDevice device = new DlmsDevice();
device.setProtocol(Protocol.DSMR_4_2_2);
// CALL
final AccessResultCode result = this.instance.execute(this.conn, device, configurationToSet, this.messageMetadata);
// VERIFY
assertThat(result).isEqualTo(AccessResultCode.SUCCESS);
final List<DataObject> elements = this.captureSetParameterStructureData();
final Number gprsOperationMode = elements.get(INDEX_OF_GPRS_OPERATION_MODE).getValue();
assertThat(gprsOperationMode).isEqualTo(gprsModeToSet.getNumber());
final BitString configurationFlags = elements.get(INDEX_OF_CONFIGURATION_FLAGS).getValue();
final byte[] flagsSentToDevice = configurationFlags.getBitString();
assertThat(flagsSentToDevice[0]).isEqualTo(firstExpectedByte);
assertThat(flagsSentToDevice[1]).isEqualTo(secondExpectedByte);
}
Aggregations