use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class InstallationServiceTest method addEMeter.
@Test
void addEMeter() throws FunctionalException {
// GIVEN
final SmartMeteringDeviceDto deviceDto = new SmartMeteringDeviceDto();
deviceDto.setDeviceIdentification(this.deviceIdentification);
deviceDto.setMasterKey(new byte[16]);
deviceDto.setAuthenticationKey(new byte[16]);
deviceDto.setGlobalEncryptionUnicastKey(new byte[16]);
deviceDto.setMbusDefaultKey(new byte[0]);
final DlmsDevice dlmsDevice = new DlmsDevice();
when(this.installationMapper.map(deviceDto, DlmsDevice.class)).thenReturn(dlmsDevice);
when(this.dlmsDeviceRepository.save(dlmsDevice)).thenReturn(dlmsDevice);
when(this.rsaEncrypter.decrypt(any())).thenReturn(new byte[16]);
// WHEN
this.testService.addMeter(this.messageMetadata, deviceDto);
// THEN
verify(this.secretManagementService, times(1)).storeNewKeys(eq(this.messageMetadata), eq(this.deviceIdentification), any());
verify(this.secretManagementService, times(1)).activateNewKeys(eq(this.messageMetadata), eq(this.deviceIdentification), any());
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class UpdateFirmwareCommandExecutor method addMac.
private FirmwareFile addMac(final MessageMetadata messageMetadata, final String deviceIdentification, final FirmwareFile firmwareFile) throws ProtocolAdapterException {
log.debug("Adding MAC to firmware file for M-Bus device with deviceIdentification {}", deviceIdentification);
final DlmsDevice mbusDevice = this.dlmsDeviceRepository.findByDeviceIdentification(deviceIdentification);
if (mbusDevice == null) {
throw new ProtocolAdapterException(String.format(EXCEPTION_MSG_DEVICE_NOT_AVAILABLE_IN_DATABASE, deviceIdentification));
}
final String identificationNumber = this.getIdentificationNumber(mbusDevice);
log.debug("Original Firmware file header: {}", firmwareFile.getHeader());
log.debug("Setting M-Bus Identification number: {}", identificationNumber);
firmwareFile.setMbusDeviceIdentificationNumber(identificationNumber);
final int mbusVersion = 80;
log.debug("Setting M-Bus Version: {}", mbusVersion);
firmwareFile.setMbusVersion(mbusVersion);
log.debug("Modified Firmware file header: {}", firmwareFile.getHeader());
final byte[] calculatedMac = this.macGenerationService.calculateMac(messageMetadata, mbusDevice.getDeviceIdentification(), firmwareFile);
log.debug("Calculated MAC: {}", Hex.toHexString(calculatedMac));
firmwareFile.setSecurityByteArray(calculatedMac);
return firmwareFile;
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class GetPeriodicMeterReadsGasCommandExecutorIntegrationTest method testExecute.
private void testExecute(final Protocol protocol, final PeriodTypeDto type, final boolean useNullData) throws Exception {
// SETUP
final MessageMetadata messageMetadata = MessageMetadata.newBuilder().withCorrelationUid("123456").build();
// Reset stub
this.connectionStub.clearRequestedAttributeAddresses();
// Create device with requested protocol version
final DlmsDevice device = this.createDlmsDevice(protocol);
// Create request object
final PeriodicMeterReadsRequestDto request = new PeriodicMeterReadsRequestDto(type, this.TIME_FROM, this.TIME_TO, ChannelDto.fromNumber(1));
// Get expected values
final AttributeAddress expectedAddressProfile = this.createAttributeAddress(protocol, type, this.TIME_FROM, this.TIME_TO);
final List<AttributeAddress> expectedScalerUnitAddresses = this.getScalerUnitAttributeAddresses(protocol);
// Set response in stub
this.setResponseForProfile(expectedAddressProfile, protocol, type, useNullData);
this.setResponsesForScalerUnit(expectedScalerUnitAddresses);
// CALL
final PeriodicMeterReadGasResponseDto response = this.executor.execute(this.connectionManagerStub, device, request, messageMetadata);
// VERIFY
// Get resulting requests from connection stub
final List<AttributeAddress> requestedAttributeAddresses = this.connectionStub.getRequestedAttributeAddresses();
assertThat(requestedAttributeAddresses.size()).isEqualTo(2);
// There should be 1 request to the buffer (id = 2) of a profile
// (class-id = 7)
final AttributeAddress actualAttributeAddressProfile = requestedAttributeAddresses.stream().filter(a -> a.getClassId() == this.CLASS_ID_PROFILE).collect(Collectors.toList()).get(0);
AttributeAddressAssert.is(actualAttributeAddressProfile, expectedAddressProfile);
// Check the amount of requests to the scaler_unit of the meter value in
// the extended register
final List<AttributeAddress> attributeAddressesScalerUnit = requestedAttributeAddresses.stream().filter(a -> a.getClassId() == this.CLASS_ID_EXTENDED_REGISTER && a.getId() == this.ATTR_ID_SCALER_UNIT).collect(Collectors.toList());
assertThat(attributeAddressesScalerUnit.size()).isEqualTo(1);
// Check response
assertThat(response.getPeriodType()).isEqualTo(type);
final List<PeriodicMeterReadsGasResponseItemDto> periodicMeterReads = response.getPeriodicMeterReadsGas();
final int AMOUNT_OF_PERIODS = 2;
assertThat(periodicMeterReads.size()).isEqualTo(AMOUNT_OF_PERIODS);
this.checkClockValues(periodicMeterReads, type, useNullData);
this.checkValues(periodicMeterReads);
this.checkAmrStatus(periodicMeterReads, protocol, type);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class DlmsConnectionHelperTest method initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForIskraDevice.
@Test
void initializesInvocationCounterWhenInvocationCounterIsOutOfSyncForIskraDevice() throws Exception {
final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withProtocol("SMR").withInvocationCounter(123L).build();
final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
final ConnectionException exception = new ConnectionException("Error creating connection for device E0033006878667817 with Ip address:62.133.86.119 Port:4059 " + "UseHdlc:false UseSn:false Message:UNKNOWN: Received an association response (AARE) with an" + " error message. Result name REJECTED_PERMANENT. Assumed fault: user.");
doThrow(exception).when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
assertThrows(ConnectionException.class, () -> this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, null, this.task));
verify(this.invocationCounterManager).initializeInvocationCounter(this.messageMetadata, device);
verify(this.connectionFactory, times(2)).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.
the class DlmsConnectionHelperTest method createsConnectionForDeviceThatNeedsInvocationCounterWithInvocationCounterInitialized.
@Test
void createsConnectionForDeviceThatNeedsInvocationCounterWithInvocationCounterInitialized() throws Exception {
final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withInvocationCounter(123L).build();
final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener();
doNothing().when(this.connectionFactory).createAndHandleConnection(this.messageMetadata, device, listener, null, this.task);
this.helper.createAndHandleConnectionForDevice(this.messageMetadata, device, listener, this.task);
verifyNoMoreInteractions(this.invocationCounterManager);
}
Aggregations