Search in sources :

Example 36 with ProtocolAdapterException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException 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 37 with ProtocolAdapterException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.

the class SetEncryptionKeyExchangeOnGMeterCommandExecutor method execute.

@Override
public MethodResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final GMeterInfoDto gMeterInfo, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    try {
        LOGGER.debug("SetEncryptionKeyExchangeOnGMeterCommandExecutor.execute called");
        final String mbusDeviceIdentification = gMeterInfo.getDeviceIdentification();
        final int channel = gMeterInfo.getChannel();
        final ObisCode obisCode = OBIS_HASHMAP.get(channel);
        final byte[] gMeterEncryptionKey = this.secretManagementService.generate128BitsKeyAndStoreAsNewKey(messageMetadata, mbusDeviceIdentification, G_METER_ENCRYPTION);
        MethodResult methodResultCode = this.transferKey(conn, mbusDeviceIdentification, channel, gMeterEncryptionKey, messageMetadata);
        this.checkMethodResultCode(methodResultCode, "M-Bus Setup transfer_key", obisCode);
        methodResultCode = this.setEncryptionKey(conn, channel, gMeterEncryptionKey);
        this.checkMethodResultCode(methodResultCode, "M-Bus Setup set_encryption_key", obisCode);
        this.secretManagementService.activateNewKey(messageMetadata, mbusDeviceIdentification, G_METER_ENCRYPTION);
        return MethodResultCode.SUCCESS;
    } catch (final IOException e) {
        throw new ConnectionException(e);
    } catch (final EncrypterException e) {
        throw new ProtocolAdapterException("Unexpected exception during decryption of security keys, reason = " + e.getMessage(), e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) ObisCode(org.openmuc.jdlms.ObisCode) IOException(java.io.IOException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) MethodResult(org.openmuc.jdlms.MethodResult)

Example 38 with ProtocolAdapterException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException 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 39 with ProtocolAdapterException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.

the class GetGsmDiagnosticCommandExecutor method getCaptureTime.

private Date getCaptureTime(final List<GetResult> getResultList) throws ProtocolAdapterException {
    // createAttributeAddresses.
    if (RESULT_CAPTURE_TIME_INDEX >= getResultList.size()) {
        return null;
    }
    final GetResult result = getResultList.get(RESULT_CAPTURE_TIME_INDEX);
    if (this.isResultSuccess(result)) {
        final CosemDateTimeDto cosemDateTime = this.dlmsHelper.readDateTime(result.getResultData(), "Clock from gsm diagnostic");
        final Date captureTime;
        if (cosemDateTime.isDateTimeSpecified()) {
            captureTime = cosemDateTime.asDateTime().toDate();
        } else {
            throw new ProtocolAdapterException("Unexpected values in gsm diagnostic capture time");
        }
        return captureTime;
    } else {
        return null;
    }
}
Also used : GetResult(org.openmuc.jdlms.GetResult) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) Date(java.util.Date) CosemDateTimeDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)

Example 40 with ProtocolAdapterException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.

the class GetOutagesCommandExecutor method execute.

@Override
public List<OutageDto> execute(final DlmsConnectionManager conn, final DlmsDevice device, final GetOutagesRequestDto getOutagesRequestDto, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    final AttributeAddress eventLogBuffer = new AttributeAddress(CLASS_ID, new ObisCode(OBIS_CODE), ATTRIBUTE_ID);
    conn.getDlmsMessageListener().setDescription("RetrieveOutages, retrieve attribute: " + JdlmsObjectToStringUtil.describeAttributes(eventLogBuffer));
    final GetResult getResult;
    try {
        getResult = conn.getConnection().get(eventLogBuffer);
    } catch (final IOException e) {
        throw new ConnectionException(e);
    }
    if (getResult == null) {
        throw new ProtocolAdapterException("No GetResult received while retrieving event register POWER_FAILURE_EVENT_LOG");
    }
    if (!AccessResultCode.SUCCESS.equals(getResult.getResultCode())) {
        log.info("Result of getting events for POWER_FAILURE_EVENT_LOG is {}", getResult.getResultCode());
        throw new ProtocolAdapterException("Getting the outages from POWER_FAILURE_EVENT_LOG from the meter resulted in: " + getResult.getResultCode());
    }
    final DataObject resultData = getResult.getResultData();
    return this.dataObjectToOutageListConverter.convert(resultData);
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) GetResult(org.openmuc.jdlms.GetResult) AttributeAddress(org.openmuc.jdlms.AttributeAddress) ObisCode(org.openmuc.jdlms.ObisCode) IOException(java.io.IOException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Aggregations

ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)95 DataObject (org.openmuc.jdlms.datatypes.DataObject)32 Test (org.junit.jupiter.api.Test)22 AccessResultCode (org.openmuc.jdlms.AccessResultCode)15 GetResult (org.openmuc.jdlms.GetResult)15 AttributeAddress (org.openmuc.jdlms.AttributeAddress)14 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)10 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)9 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)9 BitString (org.openmuc.jdlms.datatypes.BitString)7 CosemDateTimeDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.CosemDateTimeDto)7 ObisCode (org.openmuc.jdlms.ObisCode)6 SetParameter (org.openmuc.jdlms.SetParameter)5 MessageMetadata (org.opensmartgridplatform.shared.infra.jms.MessageMetadata)5 DateTime (org.joda.time.DateTime)4 DlmsObjectType (org.opensmartgridplatform.adapter.protocol.dlms.domain.commands.dlmsobjectconfig.DlmsObjectType)4 ConfigurationFlagsDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.ConfigurationFlagsDto)4 Date (java.util.Date)3 List (java.util.List)3