Search in sources :

Example 1 with ProcessCommunicatorImpl

use of tuwien.auto.calimero.process.ProcessCommunicatorImpl 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)

Example 2 with ProcessCommunicatorImpl

use of tuwien.auto.calimero.process.ProcessCommunicatorImpl in project openhab1-addons by openhab.

the class KNXConnection method connect.

/**
     * Tries to connect either by IP or serial bus, depending on supplied config data.
     * 
     * @return true if connection was established, false otherwise
     */
public static synchronized boolean connect() {
    boolean successRetVal = false;
    sShutdown = false;
    try {
        if (StringUtils.isNotBlank(sIp)) {
            sLink = connectByIp(sIpConnectionType, sLocalIp, sIp, sPort);
        } else if (StringUtils.isNotBlank(sSerialPort)) {
            sLink = connectBySerial(sSerialPort);
        } else {
            sLogger.error("No IP address or serial port could be found in configuration!");
            return false;
        }
        NetworkLinkListener linkListener = new NetworkLinkListener() {

            @Override
            public void linkClosed(CloseEvent e) {
                if (!(CloseEvent.USER_REQUEST == e.getInitiator()) && !sShutdown) {
                    sLogger.warn("KNX link has been lost (reason: {} on object {})", e.getReason(), e.getSource().toString());
                    for (KNXConnectionListener listener : KNXConnection.sConnectionListeners) {
                        listener.connectionLost();
                    }
                    if (sAutoReconnectPeriod > 0) {
                        sLogger.info("KNX link will be retried in " + sAutoReconnectPeriod + " seconds");
                        final Timer timer = new Timer();
                        TimerTask timerTask = new ConnectTimerTask(timer);
                        timer.schedule(timerTask, sAutoReconnectPeriod * 1000, sAutoReconnectPeriod * 1000);
                    }
                }
            }

            @Override
            public void indication(FrameEvent e) {
            }

            @Override
            public void confirmation(FrameEvent e) {
            }
        };
        sLink.addLinkListener(linkListener);
        if (sPC != null) {
            sPC.removeProcessListener(sProcessCommunicationListener);
            sPC.detach();
        }
        sPC = new ProcessCommunicatorImpl(sLink);
        sPC.setResponseTimeout((int) sResponseTimeout / 1000);
        if (sProcessCommunicationListener != null) {
            sPC.addProcessListener(sProcessCommunicationListener);
        }
        if (sLogger.isInfoEnabled()) {
            if (sLink instanceof KNXNetworkLinkIP) {
                String ipConnectionTypeString = KNXConnection.sIpConnectionType == KNXNetworkLinkIP.ROUTING ? "ROUTER" : "TUNNEL";
                sLogger.info("Established connection to KNX bus on {} in mode {}.", sIp + ":" + sPort, ipConnectionTypeString);
            } else {
                sLogger.info("Established connection to KNX bus through FT1.2 on serial port {}.", sSerialPort);
            }
        }
        for (KNXConnectionListener listener : KNXConnection.sConnectionListeners) {
            listener.connectionEstablished();
        }
        successRetVal = true;
    } catch (KNXException e) {
        sLogger.error("Error connecting to KNX bus: {}", e.getMessage());
    } catch (UnknownHostException e) {
        sLogger.error("Error connecting to KNX bus (unknown host): {}", e.getMessage());
    } catch (InterruptedException e) {
        sLogger.error("Error connecting to KNX bus (interrupted): {}", e.getMessage());
    }
    return successRetVal;
}
Also used : KNXException(tuwien.auto.calimero.exception.KNXException) CloseEvent(tuwien.auto.calimero.CloseEvent) UnknownHostException(java.net.UnknownHostException) KNXNetworkLinkIP(tuwien.auto.calimero.link.KNXNetworkLinkIP) FrameEvent(tuwien.auto.calimero.FrameEvent) NetworkLinkListener(tuwien.auto.calimero.link.NetworkLinkListener) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ProcessCommunicatorImpl(tuwien.auto.calimero.process.ProcessCommunicatorImpl)

Aggregations

UnknownHostException (java.net.UnknownHostException)2 ProcessCommunicatorImpl (tuwien.auto.calimero.process.ProcessCommunicatorImpl)2 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Consumer (java.util.function.Consumer)1 CloseEvent (tuwien.auto.calimero.CloseEvent)1 FrameEvent (tuwien.auto.calimero.FrameEvent)1 StateDP (tuwien.auto.calimero.datapoint.StateDP)1 KNXException (tuwien.auto.calimero.exception.KNXException)1 KNXNetworkLinkIP (tuwien.auto.calimero.link.KNXNetworkLinkIP)1 NetworkLinkListener (tuwien.auto.calimero.link.NetworkLinkListener)1 TPSettings (tuwien.auto.calimero.link.medium.TPSettings)1