Search in sources :

Example 1 with TPSettings

use of tuwien.auto.calimero.link.medium.TPSettings in project openhab1-addons by openhab.

the class KNXConnection method connectByIp.

private static KNXNetworkLink connectByIp(int ipConnectionType, String localIp, String ip, int port) throws KNXException, UnknownHostException, InterruptedException {
    InetSocketAddress localEndPoint = null;
    if (StringUtils.isNotBlank(localIp)) {
        localEndPoint = new InetSocketAddress(localIp, 0);
    } else {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            localEndPoint = new InetSocketAddress(localHost, 0);
        } catch (UnknownHostException uhe) {
            sLogger.warn("Couldn't find an IP address for this host. Please check the .hosts configuration or use the 'localIp' parameter to configure a valid IP address.");
        }
    }
    return new KNXNetworkLinkIP(ipConnectionType, localEndPoint, new InetSocketAddress(ip, port), sUseNAT, new TPSettings(new IndividualAddress(sLocalSourceAddr), true));
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) TPSettings(tuwien.auto.calimero.link.medium.TPSettings) KNXNetworkLinkIP(tuwien.auto.calimero.link.KNXNetworkLinkIP) IndividualAddress(tuwien.auto.calimero.IndividualAddress) InetAddress(java.net.InetAddress)

Example 2 with TPSettings

use of tuwien.auto.calimero.link.medium.TPSettings in project openremote by openremote.

the class KNXConnection method connect.

public synchronized void connect() {
    if (connectionStatus == ConnectionStatus.CONNECTED || connectionStatus == ConnectionStatus.CONNECTING) {
        LOG.finest("Already connected or connection in progress");
        return;
    }
    onConnectionStatusChanged(ConnectionStatus.CONNECTING);
    InetSocketAddress localEndPoint;
    InetSocketAddress remoteEndPoint = new InetSocketAddress(this.gatewayAddress, this.gatewayPort);
    try {
        TPSettings tpSettings = new TPSettings(new IndividualAddress(this.messageSourceAddress));
        if (StringUtils.isNotBlank(this.bindAddress)) {
            localEndPoint = new InetSocketAddress(this.bindAddress, 0);
        } else {
            InetAddress localHost = InetAddress.getLocalHost();
            localEndPoint = new InetSocketAddress(localHost, 0);
        }
        if (!routingMode) {
            knxLink = KNXNetworkLinkIP.newTunnelingLink(localEndPoint, remoteEndPoint, this.natMode, tpSettings);
        } else {
            knxLink = KNXNetworkLinkIP.newRoutingLink(localEndPoint.getAddress(), remoteEndPoint.getAddress(), tpSettings);
        }
        if (knxLink.isOpen()) {
            LOG.fine("Successfully connected to: " + gatewayAddress + ":" + port);
            processCommunicator = new ProcessCommunicatorImpl(knxLink);
            processCommunicator.addProcessListener(this);
            knxLink.addLinkListener(this);
            reconnectTask = null;
            reconnectDelayMilliseconds = INITIAL_RECONNECT_DELAY_MILLIS;
            onConnectionStatusChanged(ConnectionStatus.CONNECTED);
            // Get the values of all registered group addresses
            LOG.finer("Initialising group address values");
            synchronized (groupAddressConsumerMap) {
                groupAddressConsumerMap.forEach((groupAddress, datapointConsumerList) -> {
                    if (!datapointConsumerList.isEmpty()) {
                        // Take first data point for the group address and request the value
                        Pair<StateDP, Consumer<Object>> datapointConsumer = datapointConsumerList.get(0);
                        getGroupAddressValue(datapointConsumer.key.getMainAddress(), datapointConsumer.key.getPriority());
                    }
                });
            }
        } else {
            LOG.log(Level.INFO, "Connection error");
            // Failed to connect so schedule reconnection attempt
            scheduleReconnect();
        }
    } catch (final KNXException | InterruptedException e) {
        LOG.log(Level.INFO, "Connection error", e.getMessage());
        scheduleReconnect();
    } catch (final UnknownHostException e) {
        LOG.log(Level.INFO, "Connection error", e.getMessage());
    }
}
Also used : Consumer(java.util.function.Consumer) UnknownHostException(java.net.UnknownHostException) ProcessCommunicatorImpl(tuwien.auto.calimero.process.ProcessCommunicatorImpl) InetSocketAddress(java.net.InetSocketAddress) TPSettings(tuwien.auto.calimero.link.medium.TPSettings) StateDP(tuwien.auto.calimero.datapoint.StateDP) InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 UnknownHostException (java.net.UnknownHostException)2 TPSettings (tuwien.auto.calimero.link.medium.TPSettings)2 Consumer (java.util.function.Consumer)1 IndividualAddress (tuwien.auto.calimero.IndividualAddress)1 StateDP (tuwien.auto.calimero.datapoint.StateDP)1 KNXNetworkLinkIP (tuwien.auto.calimero.link.KNXNetworkLinkIP)1 ProcessCommunicatorImpl (tuwien.auto.calimero.process.ProcessCommunicatorImpl)1