use of org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelElementValuesDto in project open-smart-grid-platform by OSGP.
the class CoupleMBusDeviceCommandExecutor method execute.
@Override
public MbusChannelElementsResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final MbusChannelElementsDto requestDto, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
log.debug("retrieving mbus info on e-meter");
final List<ChannelElementValuesDto> candidateChannelElementValues = this.deviceChannelsHelper.findCandidateChannelsForDevice(conn, device, requestDto);
final ChannelElementValuesDto lastChannelElementValuesRetrieved = candidateChannelElementValues.get(candidateChannelElementValues.size() - 1);
if (FindMatchingChannelHelper.matches(requestDto, lastChannelElementValuesRetrieved)) {
/*
* Match found, indicating device is already coupled on this
* channel: return it.
*/
return new MbusChannelElementsResponseDto(requestDto, lastChannelElementValuesRetrieved.getChannel(), candidateChannelElementValues);
}
final ChannelElementValuesDto bestMatch = FindMatchingChannelHelper.bestMatch(requestDto, candidateChannelElementValues);
if (bestMatch != null) {
/*
* Good enough match found indicating a channel the device is
* already coupled on: return it.
*/
return new MbusChannelElementsResponseDto(requestDto, bestMatch.getChannel(), candidateChannelElementValues);
}
final ChannelElementValuesDto emptyChannelMatch = this.deviceChannelsHelper.findEmptyChannel(candidateChannelElementValues);
if (emptyChannelMatch == null) {
/*
* No channel free, all are occupied by M-Bus devices not matching
* the one to be coupled here. Return null for the channel.
*/
return new MbusChannelElementsResponseDto(requestDto, null, candidateChannelElementValues);
}
/*
* If a free channel is found, write the attribute values from the
* request to the M-Bus Client Setup for this channel.
*
* Note that this will not work for wired M-Bus devices. In order to
* properly couple a wired M-Bus device the M-Bus Client Setup
* slave_install method needs to be invoked so a primary_address is set
* and its value is transferred to the M-Bus slave device.
*/
final ChannelElementValuesDto updatedChannelElementValues = this.deviceChannelsHelper.writeUpdatedMbus(conn, device, requestDto, emptyChannelMatch.getChannel(), "CoupleMBusDevice");
/*
* Also update the entry in the candidateChannelElementValues list. Take
* into account that the candidateChannelElementsValues List is 0-based,
* while the channel in emptyChannelMatch is not
*/
candidateChannelElementValues.set(this.deviceChannelsHelper.correctFirstChannelOffset(emptyChannelMatch), updatedChannelElementValues);
return new MbusChannelElementsResponseDto(requestDto, updatedChannelElementValues.getChannel(), candidateChannelElementValues);
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelElementValuesDto in project open-smart-grid-platform by OSGP.
the class FindMatchingChannelHelperTest method testAllAttributesInRequestNoneInRetrievedValues.
@Test
public void testAllAttributesInRequestNoneInRetrievedValues() {
final MbusChannelElementsDto requestData = new MbusChannelElementsDto(this.primaryAddress, this.deviceIdentification, this.identificationNumber, this.manufacturerIdentification, this.version, this.deviceTypeIdentification);
final ChannelElementValuesDto channelValues = new ChannelElementValuesDto(this.channel, this.noPrimaryAddress, this.noIdentificationNumber, this.noManufacturerIdentification, this.noVersion, this.noDeviceTypeIdentification);
assertThat(FindMatchingChannelHelper.matches(requestData, channelValues)).withFailMessage(requestData + " should not match " + channelValues).isFalse();
assertThat(FindMatchingChannelHelper.matchesPartially(requestData, channelValues)).withFailMessage(requestData + " should not match partially " + channelValues).isFalse();
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelElementValuesDto in project open-smart-grid-platform by OSGP.
the class FindMatchingChannelHelperTest method testValuesBestMatchingToRequestShouldBePicked.
@Test
public void testValuesBestMatchingToRequestShouldBePicked() {
final MbusChannelElementsDto requestData = new MbusChannelElementsDto(this.primaryAddress, this.deviceIdentification, this.identificationNumber, this.manufacturerIdentification, this.version, this.deviceTypeIdentification);
final ChannelElementValuesDto channelValues = new ChannelElementValuesDto(this.channel, this.primaryAddress, this.identificationNumber, this.manufacturerIdentification, this.version, this.deviceTypeIdentification);
final ChannelElementValuesDto otherChannelValues = new ChannelElementValuesDto(this.otherChannel, this.otherPrimaryAddress, this.otherIdentificationNumber, this.otherManufacturerIdentification, this.otherVersion, this.otherDeviceTypeIdentification);
final ChannelElementValuesDto partiallyMatchingChannelValues = new ChannelElementValuesDto((short) 3, (short) 3, this.noIdentificationNumber, this.manufacturerIdentification, this.version, this.deviceTypeIdentification);
assertThat(FindMatchingChannelHelper.matches(requestData, channelValues)).withFailMessage(requestData + " should match " + channelValues).isTrue();
assertThat(FindMatchingChannelHelper.matches(requestData, otherChannelValues)).withFailMessage(requestData + " should not match " + otherChannelValues).isFalse();
assertThat(FindMatchingChannelHelper.matches(requestData, partiallyMatchingChannelValues)).withFailMessage(requestData + " should not match " + partiallyMatchingChannelValues).isFalse();
assertThat(FindMatchingChannelHelper.matchesPartially(requestData, channelValues)).withFailMessage(requestData + " should match partially " + channelValues).isTrue();
assertThat(FindMatchingChannelHelper.matchesPartially(requestData, otherChannelValues)).withFailMessage(requestData + " should not match partially " + otherChannelValues).isFalse();
assertThat(FindMatchingChannelHelper.matchesPartially(requestData, partiallyMatchingChannelValues)).withFailMessage(requestData + " should match partially " + partiallyMatchingChannelValues).isTrue();
final List<ChannelElementValuesDto> channelValuesList = Arrays.asList(partiallyMatchingChannelValues, otherChannelValues, channelValues);
final ChannelElementValuesDto bestMatch = FindMatchingChannelHelper.bestMatch(requestData, channelValuesList);
assertThat(bestMatch).withFailMessage(requestData + " should have a best match from " + channelValuesList).isNotNull();
assertThat(bestMatch.getChannel()).withFailMessage("Channel for best match").isEqualTo(channelValues.getChannel());
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelElementValuesDto in project open-smart-grid-platform by OSGP.
the class DeviceChannelsHelperTest method testGetChannelElementValuesDsmr4.
@Test
void testGetChannelElementValuesDsmr4() throws Exception {
final List<GetResult> resultList = new ArrayList<>(Arrays.asList(this.primaryAddress, this.identificationNumberInBcd, this.manufacturerIdentification, this.version, this.deviceType));
when(this.device.isWithListSupported()).thenReturn(true);
when(this.conn.getDlmsMessageListener()).thenReturn(this.dlmsMessageListener);
when(this.conn.getConnection()).thenReturn(this.dlmsConnection);
when(this.dlmsConnection.get(anyList())).thenReturn(resultList);
when(this.device.getProtocolName()).thenReturn("DSMR");
when(this.device.getProtocolVersion()).thenReturn("4.2.2");
final ChannelElementValuesDto values = this.deviceChannelsHelper.getChannelElementValues(this.conn, this.device, (short) 1);
assertThat(values.getPrimaryAddress()).isEqualTo(PRIMARY_ADDRESS);
assertThat(values.getIdentificationNumber()).isEqualTo(IDENTIFICATION_NUMBER_AS_STRING);
assertThat(values.getManufacturerIdentification()).isEqualTo(MANUFACTURER_IDENTIFICATION_AS_TEXT);
assertThat(values.getVersion()).isEqualTo(VERSION);
assertThat(values.getDeviceTypeIdentification()).isEqualTo(DEVICE_TYPE);
}
use of org.opensmartgridplatform.dto.valueobjects.smartmetering.ChannelElementValuesDto in project open-smart-grid-platform by OSGP.
the class DeviceChannelsHelperTest method testFindEmptyChannel.
@Test
public void testFindEmptyChannel() {
final ChannelElementValuesDto emptyChannel = new ChannelElementValuesDto((short) 1, (short) 0, "00000000", null, (short) 0, (short) 0);
final ChannelElementValuesDto nonEmptyChannel = new ChannelElementValuesDto((short) 2, (short) 0, "12345678", null, (short) 0, (short) 0);
final ChannelElementValuesDto result = this.deviceChannelsHelper.findEmptyChannel(Arrays.asList(nonEmptyChannel, emptyChannel));
assertThat(result).isEqualTo(emptyChannel);
}
Aggregations