Search in sources :

Example 11 with DlmsDevice

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.

the class InstallationService method addMeter.

public void addMeter(final MessageMetadata messageMetadata, final SmartMeteringDeviceDto smartMeteringDevice) throws FunctionalException {
    if (smartMeteringDevice.getDeviceIdentification() == null) {
        throw new FunctionalException(FunctionalExceptionType.VALIDATION_ERROR, ComponentType.PROTOCOL_DLMS, new IllegalArgumentException("Provided device does not contain device identification"));
    }
    this.storeAndActivateKeys(messageMetadata, smartMeteringDevice);
    final DlmsDevice dlmsDevice = this.installationMapper.map(smartMeteringDevice, DlmsDevice.class);
    this.dlmsDeviceRepository.save(dlmsDevice);
}
Also used : DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Example 12 with DlmsDevice

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.

the class DlmsObjectConfigService method getDlmsObjectForCommunicationMethod.

public DlmsObject getDlmsObjectForCommunicationMethod(final DlmsDevice device, final DlmsObjectType type) throws ProtocolAdapterException {
    final Protocol protocol = Protocol.forDevice(device);
    final CommunicationMethod method = CommunicationMethod.getCommunicationMethod(device.getCommunicationMethod());
    return this.dlmsObjectConfigs.stream().filter(config -> config.contains(protocol)).findAny().flatMap(dlmsObjectConfig -> dlmsObjectConfig.findObjectForCommunicationMethod(type, method)).orElseThrow(() -> new ProtocolAdapterException("Did not find " + type.name() + " object with communication method " + method.getMethodName() + " for device " + device.getDeviceId()));
}
Also used : CommunicationMethod(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.CommunicationMethod) DlmsProfile(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsProfile) Arrays(java.util.Arrays) DlmsHelper(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper) DateTime(org.joda.time.DateTime) DataObject(org.openmuc.jdlms.datatypes.DataObject) Autowired(org.springframework.beans.factory.annotation.Autowired) DlmsObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject) ArrayList(java.util.ArrayList) AttributeAddress(org.openmuc.jdlms.AttributeAddress) List(java.util.List) DlmsRegister(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsRegister) ObisCode(org.openmuc.jdlms.ObisCode) CommunicationMethod(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.CommunicationMethod) Medium(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.Medium) SelectiveAccessDescription(org.openmuc.jdlms.SelectiveAccessDescription) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) Protocol(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) Protocol(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol)

Example 13 with DlmsDevice

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.

the class RecoverKeyProcess method run.

@Override
public void run() {
    final DlmsDevice device = this.findDeviceAndCheckState();
    log.info("[{}] Attempting key recovery for device {}", this.messageMetadata.getCorrelationUid(), this.deviceIdentification);
    try {
        // The process started in this try-catch block should only be stopped in the
        // ThrottlingPermitDeniedException catch block. If a DeviceKeyProcessAlreadyRunningException
        // is thrown the process was already started and should not be stopped. This method below
        // could also throw a RecoverKeyException in this case the process wasn't started either
        // The next try-catch block has a finally block to ensure stopping the process started here.
        this.startProcessing(device);
        if (!this.canConnectUsingNewKeys(device)) {
            log.warn("[{}] Could not recover keys: could not connect to device {} using New keys", this.messageMetadata.getCorrelationUid(), this.deviceIdentification);
            return;
        }
    } catch (final ThrottlingPermitDeniedException e) {
        log.warn("RecoverKeyProcess could not connect to the device due to throttling constraints", e);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                RecoverKeyProcess.this.run();
            }
        }, this.throttlingClientConfig.permitRejectedDelay().toMillis());
        this.deviceKeyProcessingService.stopProcessing(this.deviceIdentification);
        return;
    } catch (final DeviceKeyProcessAlreadyRunningException e) {
        log.info("RecoverKeyProcess could not be started while other key changing process is already running.");
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                RecoverKeyProcess.this.run();
            }
        }, this.deviceKeyProcessingService.getDeviceKeyProcessingTimeout().toMillis());
        return;
    }
    try {
        this.secretManagementService.activateNewKeys(this.messageMetadata, this.deviceIdentification, Arrays.asList(E_METER_ENCRYPTION, E_METER_AUTHENTICATION));
    } catch (final Exception e) {
        throw new RecoverKeyException(e);
    } finally {
        this.deviceKeyProcessingService.stopProcessing(this.deviceIdentification);
    }
}
Also used : DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) RecoverKeyException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) RecoverKeyException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.RecoverKeyException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) DeviceKeyProcessAlreadyRunningException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.DeviceKeyProcessAlreadyRunningException) ThrottlingPermitDeniedException(org.opensmartgridplatform.throttling.ThrottlingPermitDeniedException) IOException(java.io.IOException)

Example 14 with DlmsDevice

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsGasCommandExecutorTest method createDevice.

private DlmsDevice createDevice(final Protocol protocol) {
    final DlmsDevice device = new DlmsDevice();
    device.setProtocol(protocol);
    return device;
}
Also used : DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)

Example 15 with DlmsDevice

use of org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice in project open-smart-grid-platform by OSGP.

the class GetPeriodicMeterReadsCommandExecutorIntegrationTest 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.timeFrom, this.timeTo);
    // Get expected values
    final AttributeAddress expectedAddressProfile = this.createAttributeAddress(protocol, type, this.timeFrom, this.timeTo);
    final List<AttributeAddress> expectedScalerUnitAddresses = this.getScalerUnitAttributeAddresses(type);
    final int expectedTotalNumberOfAttributeAddresses = expectedScalerUnitAddresses.size() + 1;
    // Set response in stub
    this.setResponseForProfile(expectedAddressProfile, protocol, type, useNullData);
    this.setResponsesForScalerUnit(expectedScalerUnitAddresses);
    // CALL
    final PeriodicMeterReadsResponseDto 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(expectedTotalNumberOfAttributeAddresses);
    // 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_units of the meter values
    // in the registers
    final List<AttributeAddress> attributeAddressesScalerUnit = requestedAttributeAddresses.stream().filter(a -> a.getClassId() == this.CLASS_ID_REGISTER && a.getId() == this.ATTR_ID_SCALER_UNIT).collect(Collectors.toList());
    assertThat(attributeAddressesScalerUnit.size()).isEqualTo(expectedScalerUnitAddresses.size());
    // Check response
    assertThat(response.getPeriodType()).isEqualTo(type);
    final List<PeriodicMeterReadsResponseItemDto> periodicMeterReads = response.getPeriodicMeterReads();
    assertThat(periodicMeterReads.size()).isEqualTo(this.AMOUNT_OF_PERIODS);
    this.checkClockValues(periodicMeterReads, type, useNullData);
    this.checkValues(periodicMeterReads, type);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) PeriodTypeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodTypeDto) DlmsConnectionManagerStub(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.stub.DlmsConnectionManagerStub) Date(java.util.Date) PeriodicMeterReadsResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) ArrayList(java.util.ArrayList) DlmsObjectConfigService(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigService) AttributeAddress(org.openmuc.jdlms.AttributeAddress) Calendar(java.util.Calendar) ObisCode(org.openmuc.jdlms.ObisCode) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DlmsConnectionStub(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.stub.DlmsConnectionStub) PeriodicMeterReadsRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsRequestDto) AttributeAddressAssert(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.testutil.AttributeAddressAssert) Protocol(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) DlmsHelper(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper) DateTime(org.joda.time.DateTime) PeriodicMeterReadsResponseItemDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseItemDto) DataObject(org.openmuc.jdlms.datatypes.DataObject) DlmsObjectConfigConfiguration(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigConfiguration) AmrProfileStatusCodeHelper(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.AmrProfileStatusCodeHelper) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CosemDateTime(org.openmuc.jdlms.datatypes.CosemDateTime) List(java.util.List) SelectiveAccessDescription(org.openmuc.jdlms.SelectiveAccessDescription) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) Collections(java.util.Collections) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) PeriodicMeterReadsResponseItemDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseItemDto) AttributeAddress(org.openmuc.jdlms.AttributeAddress) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) PeriodicMeterReadsRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsRequestDto) PeriodicMeterReadsResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PeriodicMeterReadsResponseDto)

Aggregations

DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)103 Test (org.junit.jupiter.api.Test)58 DlmsDeviceBuilder (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder)24 DlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener)17 InvocationCountingDlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)17 AttributeAddress (org.openmuc.jdlms.AttributeAddress)15 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)13 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)10 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)10 Then (io.cucumber.java.en.Then)9 DataObject (org.openmuc.jdlms.datatypes.DataObject)7 DlmsConnectionManager (org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager)7 AccessResultCode (org.openmuc.jdlms.AccessResultCode)6 Instant (java.time.Instant)5 Protocol (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.Protocol)5 IOException (java.io.IOException)4 Date (java.util.Date)4 List (java.util.List)4 ThrowableAssert.catchThrowable (org.assertj.core.api.ThrowableAssert.catchThrowable)4 SetParameter (org.openmuc.jdlms.SetParameter)4