Search in sources :

Example 11 with ConnectionException

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

the class Lls0Connector method connect.

@Override
public DlmsConnection connect(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener) throws OsgpException {
    // Make sure neither device or device.getIpAddress() is null.
    this.checkDevice(device);
    this.checkIpAddress(device);
    // Setup connection to device
    final TcpConnectionBuilder tcpConnectionBuilder;
    try {
        tcpConnectionBuilder = new TcpConnectionBuilder(InetAddress.getByName(device.getIpAddress())).setResponseTimeout(this.responseTimeout).setLogicalDeviceId(this.logicalDeviceAddress).setClientId(this.clientId).setReferencingMethod(device.isUseSn() ? ReferencingMethod.SHORT : ReferencingMethod.LOGICAL);
        if (device.isUseHdlc()) {
            tcpConnectionBuilder.useHdlc();
        }
    } catch (final UnknownHostException e) {
        LOGGER.error("The IP address is not found: {}", device.getIpAddress(), e);
        // Unknown IP, unrecoverable.
        throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
    }
    this.setOptionalValues(device, tcpConnectionBuilder);
    if (device.isInDebugMode()) {
        tcpConnectionBuilder.setRawMessageListener(dlmsMessageListener);
    }
    try {
        return tcpConnectionBuilder.build();
    } catch (final IOException e) {
        final String msg = String.format("Error creating connection for device %s with Ip address:%s Port:%d UseHdlc:%b UseSn:%b Message:%s", device.getDeviceIdentification(), device.getIpAddress(), device.getPort(), device.isUseHdlc(), device.isUseSn(), e.getMessage());
        LOGGER.error(msg);
        throw new ConnectionException(msg, e);
    }
}
Also used : TechnicalException(org.opensmartgridplatform.shared.exceptionhandling.TechnicalException) TcpConnectionBuilder(org.openmuc.jdlms.TcpConnectionBuilder) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 12 with ConnectionException

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

the class GetAllAttributeValuesCommandExecutor method execute.

@Override
public String execute(final DlmsConnectionManager conn, final DlmsDevice device, final DataObject object, final MessageMetadata messageMetadata) throws OsgpException {
    final AttributeAddress attributeAddress = new AttributeAddress(CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
    conn.getDlmsMessageListener().setDescription("RetrieveAllAttributeValues, retrieve attribute: " + JdlmsObjectToStringUtil.describeAttributes(attributeAddress));
    LOGGER.debug("Retrieving all attribute values for class id: {}, obis code: {}, attribute id: {}", CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
    final DataObject objectList = this.dlmsHelper.getAttributeValue(conn, attributeAddress);
    if (!objectList.isComplex()) {
        this.throwUnexpectedTypeProtocolAdapterException();
    }
    final List<DataObject> objectListElements = objectList.getValue();
    final List<ClassIdObisAttr> allObisCodes = this.getAllObisCodes(objectListElements);
    this.logAllObisCodes(allObisCodes);
    try {
        final String output = this.createOutput(conn, allObisCodes);
        LOGGER.debug("Total output is: {}", output);
        return output;
    } catch (final IOException e) {
        throw new ConnectionException(e);
    }
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) AttributeAddress(org.openmuc.jdlms.AttributeAddress) IOException(java.io.IOException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 13 with ConnectionException

use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException 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)

Example 14 with ConnectionException

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

the class SetAdministrativeStatusCommandExecutor method execute.

@Override
public AccessResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final AdministrativeStatusTypeDto administrativeStatusType, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    LOGGER.info("Set administrative status by issuing get request for class id: {}, obis code: {}, attribute id: {}", CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
    final AttributeAddress attributeAddress = new AttributeAddress(CLASS_ID, OBIS_CODE, ATTRIBUTE_ID);
    final DataObject value = DataObject.newEnumerateData(this.configurationMapper.map(administrativeStatusType, Integer.class));
    final SetParameter setParameter = new SetParameter(attributeAddress, value);
    conn.getDlmsMessageListener().setDescription("SetAdminstrativeStatus to " + administrativeStatusType + ", set attribute: " + JdlmsObjectToStringUtil.describeAttributes(attributeAddress));
    try {
        return conn.getConnection().set(setParameter);
    } catch (final IOException e) {
        throw new ConnectionException(e);
    }
}
Also used : DataObject(org.openmuc.jdlms.datatypes.DataObject) AttributeAddress(org.openmuc.jdlms.AttributeAddress) IOException(java.io.IOException) SetParameter(org.openmuc.jdlms.SetParameter) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 15 with ConnectionException

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

the class AbstractReplaceKeyCommandExecutor method sendToDevice.

/**
 * Send the key to the device.
 *
 * @param conn jDLMS connection.
 * @param deviceIdentification Device identification
 * @param keyWrapper Key data
 * @param messageMetadata
 */
private void sendToDevice(final DlmsConnectionManager conn, final String deviceIdentification, final ReplaceKeyInput keyWrapper, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    try {
        final byte[] decryptedKey = keyWrapper.getBytes();
        final byte[] decryptedMasterKey = this.secretManagementService.getKey(messageMetadata, deviceIdentification, SecurityKeyType.E_METER_MASTER);
        final MethodParameter methodParameterAuth = SecurityUtils.keyChangeMethodParamFor(decryptedMasterKey, decryptedKey, keyWrapper.getKeyId());
        conn.getDlmsMessageListener().setDescription("ReplaceKey for " + keyWrapper.getSecurityKeyType() + " " + keyWrapper.getKeyId() + ", call method: " + JdlmsObjectToStringUtil.describeMethod(methodParameterAuth));
        final MethodResultCode methodResultCode = conn.getConnection().action(methodParameterAuth).getResultCode();
        if (!MethodResultCode.SUCCESS.equals(methodResultCode)) {
            throw new ProtocolAdapterException("AccessResultCode for replace keys was not SUCCESS: " + methodResultCode);
        }
        if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_AUTHENTICATION) {
            conn.getConnection().changeClientGlobalAuthenticationKey(decryptedKey);
        } else if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_ENCRYPTION) {
            conn.getConnection().changeClientGlobalEncryptionKey(decryptedKey);
        }
    } 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 : MethodResultCode(org.openmuc.jdlms.MethodResultCode) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) IOException(java.io.IOException) MethodParameter(org.openmuc.jdlms.MethodParameter) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Aggregations

ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)25 IOException (java.io.IOException)18 DataObject (org.openmuc.jdlms.datatypes.DataObject)9 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)8 AttributeAddress (org.openmuc.jdlms.AttributeAddress)7 GetResult (org.openmuc.jdlms.GetResult)6 Test (org.junit.jupiter.api.Test)5 SetParameter (org.openmuc.jdlms.SetParameter)5 DlmsDevice (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDevice)5 DlmsDeviceBuilder (org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.DlmsDeviceBuilder)4 DlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.DlmsMessageListener)4 InvocationCountingDlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)4 EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)4 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)4 UnknownHostException (java.net.UnknownHostException)3 AccessResultCode (org.openmuc.jdlms.AccessResultCode)3 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)3 ArrayList (java.util.ArrayList)2 MethodParameter (org.openmuc.jdlms.MethodParameter)2