Search in sources :

Example 1 with TcpConnectionBuilder

use of org.openmuc.jdlms.TcpConnectionBuilder 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 2 with TcpConnectionBuilder

use of org.openmuc.jdlms.TcpConnectionBuilder in project open-smart-grid-platform by OSGP.

the class SecureDlmsConnector method createConnection.

/**
 * Create a connection with the device.
 *
 * @param messageMetadata the metadata of the request message
 * @param device The device to connect with.
 * @param dlmsMessageListener Listener to set on the connection.
 * @return The connection.
 * @throws IOException When there are problems in connecting to or communicating with the device.
 * @throws OsgpException When there are problems reading the security and authorization keys.
 */
DlmsConnection createConnection(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener, final SecurityKeyProvider keyProvider) throws IOException, OsgpException {
    // Setup connection to device
    final TcpConnectionBuilder tcpConnectionBuilder = new TcpConnectionBuilder(InetAddress.getByName(device.getIpAddress())).setResponseTimeout(this.responseTimeout).setLogicalDeviceId(this.logicalDeviceAddress);
    tcpConnectionBuilder.setClientId(this.clientId).setReferencingMethod(device.isUseSn() ? ReferencingMethod.SHORT : ReferencingMethod.LOGICAL);
    if (device.isUseHdlc()) {
        tcpConnectionBuilder.useHdlc();
    }
    this.setSecurity(messageMetadata, device, keyProvider, tcpConnectionBuilder);
    this.setOptionalValues(device, tcpConnectionBuilder);
    if (device.isInDebugMode() || dlmsMessageListener instanceof InvocationCountingDlmsMessageListener) {
        tcpConnectionBuilder.setRawMessageListener(dlmsMessageListener);
    }
    return tcpConnectionBuilder.build();
}
Also used : TcpConnectionBuilder(org.openmuc.jdlms.TcpConnectionBuilder) InvocationCountingDlmsMessageListener(org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)

Aggregations

TcpConnectionBuilder (org.openmuc.jdlms.TcpConnectionBuilder)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)1 InvocationCountingDlmsMessageListener (org.opensmartgridplatform.adapter.protocol.dlms.infra.messaging.InvocationCountingDlmsMessageListener)1 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)1