Search in sources :

Example 46 with DlmsDevice

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

the class InvocationCounterManager method initializeWithInvocationCounterStoredOnDeviceTask.

void initializeWithInvocationCounterStoredOnDeviceTask(final DlmsDevice device, final DlmsConnectionManager connectionManager) {
    try {
        final Long previousKnownInvocationCounter = device.getInvocationCounter();
        final Long invocationCounterFromDevice = this.getInvocationCounter(connectionManager);
        if (Objects.equals(previousKnownInvocationCounter, invocationCounterFromDevice)) {
            LOGGER.warn("Initializing invocationCounter of device {} with the value that was already known: {}", device.getDeviceIdentification(), previousKnownInvocationCounter);
        } else {
            device.setInvocationCounter(invocationCounterFromDevice);
            /*
         * After saving the version is incremented. To prevent optimistic locking failures if the
         * device gets saved again (for instance from updateInvocationCounterForDevice, called from
         * doConnectionPostProcessing in DlmsConnectionMessageProcessor) set the version on the
         * device that was passed on as a parameter to the InvocationCounterManager.
         */
            final DlmsDevice updatedDevice = this.deviceRepository.save(device);
            device.setVersion(updatedDevice.getVersion());
            LOGGER.info("Property invocationCounter of device {} initialized to the value of the invocation counter " + "stored on the device: {}{}", device.getDeviceIdentification(), device.getInvocationCounter(), previousKnownInvocationCounter == null ? "" : " (previous known value: " + previousKnownInvocationCounter + ")");
        }
    } catch (final FunctionalException e) {
        LOGGER.warn("Something went wrong while trying to get the invocation counter", e);
    }
}
Also used : DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Example 47 with DlmsDevice

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

the class SetEncryptionKeyExchangeOnGMeterCommandExecutor method getTransferKeyMethodParameter.

private MethodParameter getTransferKeyMethodParameter(final String mbusDeviceIdentification, final int channel, final byte[] gMeterUserKey, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    final DlmsDevice mbusDevice = this.dlmsDeviceRepository.findByDeviceIdentification(mbusDeviceIdentification);
    if (mbusDevice == null) {
        throw new ProtocolAdapterException("Unknown M-Bus device: " + mbusDeviceIdentification);
    }
    final byte[] mbusDefaultKey = this.secretManagementService.getKey(messageMetadata, mbusDeviceIdentification, G_METER_MASTER);
    final byte[] encryptedUserKey = this.encryptMbusUserKey(mbusDefaultKey, gMeterUserKey);
    final DataObject methodParameter = DataObject.newOctetStringData(encryptedUserKey);
    final MBusClientMethod method = MBusClientMethod.TRANSFER_KEY;
    return new MethodParameter(method.getInterfaceClass().id(), OBIS_HASHMAP.get(channel), method.getMethodId(), methodParameter);
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) MethodParameter(org.openmuc.jdlms.MethodParameter) MBusClientMethod(org.opensmartgridplatform.dlms.interfaceclass.method.MBusClientMethod)

Example 48 with DlmsDevice

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

the class DeviceRequestMessageProcessor method processMessageTasks.

public void processMessageTasks(final Serializable messageObject, final MessageMetadata messageMetadata, final DlmsConnectionManager connectionManager, final DlmsDevice device) throws OsgpException {
    DlmsDevice device = null;
    try {
        if (this.maxScheduleTimeExceeded(messageMetadata)) {
            log.info("Processing message of type {} for correlation UID {} exceeded max schedule time: {} ({})", messageMetadata.getMessageType(), messageMetadata.getCorrelationUid(), messageMetadata.getMaxScheduleTime(), Instant.ofEpochMilli(messageMetadata.getMaxScheduleTime()));
            this.sendErrorResponse(messageMetadata, new FunctionalException(FunctionalExceptionType.MAX_SCHEDULE_TIME_EXCEEDED, ComponentType.PROTOCOL_DLMS), messageObject);
            return;
        }
        log.info("{} called for device: {} for organisation: {}, correlationUID: {}", messageMetadata.getMessageType(), messageMetadata.getDeviceIdentification(), messageMetadata.getOrganisationIdentification(), messageMetadata.getCorrelationUid());
        final Serializable response = this.getResponse(connectionManager, device, messageObject, messageMetadata);
        this.sendResponse(messageMetadata, response);
    } finally {
        this.doConnectionPostProcessing(device, connectionManager, messageMetadata);
    }
}
Also used : Serializable(java.io.Serializable) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Example 49 with DlmsDevice

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

the class GetGsmDiagnosticCommandExecutor method execute.

@Override
public GetGsmDiagnosticResponseDto execute(final DlmsConnectionManager conn, final DlmsDevice device, final GetGsmDiagnosticRequestDto getGsmDiagnosticQuery, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    final DlmsObject dlmsObject = this.dlmsObjectConfigService.getDlmsObjectForCommunicationMethod(device, DlmsObjectType.GSM_DIAGNOSTIC);
    final AttributeAddress[] addresses = this.createAttributeAddresses(dlmsObject);
    final String addressesDescriptions = JdlmsObjectToStringUtil.describeAttributes(addresses);
    conn.getDlmsMessageListener().setDescription("Get GsmDiagnostic, retrieve attributes: " + addressesDescriptions);
    LOGGER.info("Get GsmDiagnostic, retrieve attributes: {}", addressesDescriptions);
    final List<GetResult> getResultList = this.dlmsHelper.getAndCheck(conn, device, "Get GsmDiagnostic", addresses);
    LOGGER.info("GetResultList: {}", describeGetResults(getResultList));
    if (!getResultList.stream().allMatch(result -> result.getResultCode() == AccessResultCode.SUCCESS)) {
        throw new ProtocolAdapterException("Get gsm diagnostic failed for " + device.getDeviceId());
    }
    return this.createGetGsmDiagnosticResponse(getResultList);
}
Also used : DlmsObjectType(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectType) GetResult(org.openmuc.jdlms.GetResult) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) JdlmsObjectToStringUtil.describeGetResults(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.JdlmsObjectToStringUtil.describeGetResults) BitErrorRateDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.BitErrorRateDto) MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) MODEM_REGISTRATION_STATUS(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.MODEM_REGISTRATION_STATUS) DlmsObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject) DlmsObjectConfigService(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectConfigService) AttributeAddress(org.openmuc.jdlms.AttributeAddress) PACKET_SWITCHED_STATUS(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.PACKET_SWITCHED_STATUS) ObisCode(org.openmuc.jdlms.ObisCode) CircuitSwitchedStatusDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CircuitSwitchedStatusDto) GetGsmDiagnosticRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.GetGsmDiagnosticRequestDto) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto) ModemRegistrationStatusDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ModemRegistrationStatusDto) AccessResultCode(org.openmuc.jdlms.AccessResultCode) DlmsConnectionManager(org.opensmartgridplatform.adapter.protocol.dlms.domain.factories.DlmsConnectionManager) PacketSwitchedStatusDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.PacketSwitchedStatusDto) Logger(org.slf4j.Logger) DlmsHelper(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.DlmsHelper) AbstractCommandExecutor(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.AbstractCommandExecutor) OPERATOR(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.OPERATOR) DataObject(org.openmuc.jdlms.datatypes.DataObject) Collectors(java.util.stream.Collectors) AdjacentCellInfoDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.AdjacentCellInfoDto) StandardCharsets(java.nio.charset.StandardCharsets) ActionRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionRequestDto) CELL_INFO(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.CELL_INFO) List(java.util.List) Component(org.springframework.stereotype.Component) ADJACENT_CELLS(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.ADJACENT_CELLS) SignalQualityDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SignalQualityDto) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice) CIRCUIT_SWITCHED_STATUS(org.opensmartgridplatform.dlms.interfaceclass.attribute.GsmDiagnosticAttribute.CIRCUIT_SWITCHED_STATUS) Collections(java.util.Collections) CellInfoDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CellInfoDto) JdlmsObjectToStringUtil(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.utils.JdlmsObjectToStringUtil) GetGsmDiagnosticResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.GetGsmDiagnosticResponseDto) GetResult(org.openmuc.jdlms.GetResult) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) DlmsObject(org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.model.DlmsObject)

Example 50 with DlmsDevice

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

the class AbstractReplaceKeyCommandExecutor method replaceKeys.

protected ActionResponseDto replaceKeys(final DlmsConnectionManager conn, final DlmsDevice device, final SetKeysRequestDto setKeysRequestDto, final MessageMetadata messageMetadata) throws OsgpException {
    log.info("Keys set on device :{}", device.getDeviceIdentification());
    SetKeysRequestDto setDecryptedKeysRequestDto = setKeysRequestDto;
    if (!setKeysRequestDto.isGeneratedKeys()) {
        setDecryptedKeysRequestDto = this.decryptRsaKeys(setKeysRequestDto);
    }
    // if keys are generated, then they are unencrypted by the GenerateAndReplaceKeyCommandExecutor
    final DlmsDevice devicePostSave = this.storeAndSendToDevice(conn, device, wrap(setDecryptedKeysRequestDto.getAuthenticationKey(), KeyId.AUTHENTICATION_KEY, SecurityKeyType.E_METER_AUTHENTICATION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
    this.storeAndSendToDevice(conn, devicePostSave, wrap(setDecryptedKeysRequestDto.getEncryptionKey(), KeyId.GLOBAL_UNICAST_ENCRYPTION_KEY, SecurityKeyType.E_METER_ENCRYPTION, setDecryptedKeysRequestDto.isGeneratedKeys()), messageMetadata);
    return new ActionResponseDto(String.format("Replace keys for device %s was successful", device.getDeviceIdentification()));
}
Also used : ActionResponseDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.ActionResponseDto) SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) DlmsDevice(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)

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