use of tuwien.auto.calimero.link.NetworkLinkListener 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;
}
Aggregations