import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.lcn.internal.common.LcnAddr;
import org.openhab.binding.lcn.internal.common.LcnDefs;
}
}
+ /**
+ * Invoked by any state, if the connection fails.
+ *
+ * @param e the cause
+ */
+ public void handleConnectionFailed(@Nullable Throwable e) {
+ synchronized (context) {
+ if (e != null) {
+ String message = e.getMessage();
+ connection.getCallback().onOffline(message != null ? message : "");
+ } else {
+ connection.getCallback().onOffline("");
+ }
+ context.setState(ConnectionStateGracePeriodBeforeReconnect::new);
+ }
+ }
+
/**
* Closes the Connection SocketChannel.
*/
*/
protected void startTimeoutTimer() {
addTimer(getScheduler().schedule(
- () -> context.handleConnectionFailed(
+ () -> handleConnectionFailed(
new LcnException("Network timeout in state " + getClass().getSimpleName())),
connection.getSettings().getTimeout(), TimeUnit.MILLISECONDS));
}
message = e.getMessage();
}
connection.getCallback().onOffline(Objects.requireNonNullElse(message, ""));
- context.handleConnectionFailed(e);
+ handleConnectionFailed(e);
}
@Override
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
/**
* This state is active when the connection failed. A grace period is enforced to prevent fast cycling through the
public void onPckMessageReceived(String data) {
// nothing
}
+
+ @Override
+ public void handleConnectionFailed(@Nullable Throwable e) {
+ // nothing
+ }
}
/**
* Gets the PCHK Connection object.
- *
+ *
* @return the connection
*/
public Connection getConnection() {
*
* @param e the cause
*/
- public void handleConnectionFailed(@Nullable Throwable e) {
- if (!(state instanceof ConnectionStateShutdown)) {
- if (e != null) {
- String message = e.getMessage();
- connection.getCallback().onOffline(message != null ? message : "");
- } else {
- connection.getCallback().onOffline("");
- }
- setState(ConnectionStateGracePeriodBeforeReconnect::new);
+ public synchronized void handleConnectionFailed(@Nullable Throwable e) {
+ AbstractConnectionState localState = state;
+ if (localState != null) {
+ localState.handleConnectionFailed(e);
}
}
package org.openhab.binding.lcn.internal.connection;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.lcn.internal.common.LcnAddr;
/**
public void onPckMessageReceived(String data) {
// nothing
}
+
+ @Override
+ public void handleConnectionFailed(@Nullable Throwable e) {
+ // nothing
+ }
}
@Override
public void onPckMessageReceived(String data) {
+ switch (data) {
+ case LcnDefs.LCNCONNSTATE_DISCONNECTED:
+ cancelLegacyTimer();
+ connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE");
+ break;
+ case LcnDefs.LCNCONNSTATE_CONNECTED:
+ cancelLegacyTimer();
+ connection.getCallback().onOnline();
+ nextState(ConnectionStateSendDimMode::new);
+ break;
+ case LcnDefs.INSUFFICIENT_LICENSES:
+ cancelLegacyTimer();
+ handleConnectionFailed(
+ new LcnException("LCN-PCHK/PKE has not enough licenses to handle this connection"));
+ break;
+ }
+ }
+
+ private void cancelLegacyTimer() {
ScheduledFuture<?> localLegacyTimer = legacyTimer;
- if (data.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) {
- if (localLegacyTimer != null) {
- localLegacyTimer.cancel(true);
- }
- connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE");
- } else if (data.equals(LcnDefs.LCNCONNSTATE_CONNECTED)) {
- if (localLegacyTimer != null) {
- localLegacyTimer.cancel(true);
- }
- connection.getCallback().onOnline();
- nextState(ConnectionStateSendDimMode::new);
- } else if (data.equals(LcnDefs.INSUFFICIENT_LICENSES)) {
- context.handleConnectionFailed(
- new LcnException("LCN-PCHK/PKE has not enough licenses to handle this connection"));
+ if (localLegacyTimer != null) {
+ localLegacyTimer.cancel(true);
}
}
}