Search in sources :

Example 1 with FrameEvent

use of tuwien.auto.calimero.FrameEvent 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)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CloseEvent (tuwien.auto.calimero.CloseEvent)1 FrameEvent (tuwien.auto.calimero.FrameEvent)1 KNXException (tuwien.auto.calimero.exception.KNXException)1 KNXNetworkLinkIP (tuwien.auto.calimero.link.KNXNetworkLinkIP)1 NetworkLinkListener (tuwien.auto.calimero.link.NetworkLinkListener)1 ProcessCommunicatorImpl (tuwien.auto.calimero.process.ProcessCommunicatorImpl)1