[lcn] Fix displaying of "not enough licenses" message (#9761)

Signed-off-by: Fabian Wolter <github@fabian-wolter.de>
pull/9806/head
Fabian Wolter 2021-01-12 22:05:12 +01:00 committed by GitHub
parent 0f118c6b7e
commit 79dfb43e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 26 deletions

View File

@ -17,6 +17,7 @@ import java.nio.channels.Channel;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault; 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.LcnAddr;
import org.openhab.binding.lcn.internal.common.LcnDefs; import org.openhab.binding.lcn.internal.common.LcnDefs;
@ -84,6 +85,23 @@ public abstract class AbstractConnectionState extends AbstractState<ConnectionSt
} }
} }
/**
* 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. * Closes the Connection SocketChannel.
*/ */

View File

@ -41,7 +41,7 @@ public abstract class AbstractConnectionStateSendCredentials extends AbstractCon
*/ */
protected void startTimeoutTimer() { protected void startTimeoutTimer() {
addTimer(getScheduler().schedule( addTimer(getScheduler().schedule(
() -> context.handleConnectionFailed( () -> handleConnectionFailed(
new LcnException("Network timeout in state " + getClass().getSimpleName())), new LcnException("Network timeout in state " + getClass().getSimpleName())),
connection.getSettings().getTimeout(), TimeUnit.MILLISECONDS)); connection.getSettings().getTimeout(), TimeUnit.MILLISECONDS));
} }

View File

@ -86,7 +86,7 @@ public class ConnectionStateConnecting extends AbstractConnectionState {
message = e.getMessage(); message = e.getMessage();
} }
connection.getCallback().onOffline(Objects.requireNonNullElse(message, "")); connection.getCallback().onOffline(Objects.requireNonNullElse(message, ""));
context.handleConnectionFailed(e); handleConnectionFailed(e);
} }
@Override @Override

View File

@ -15,6 +15,7 @@ package org.openhab.binding.lcn.internal.connection;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault; 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 * This state is active when the connection failed. A grace period is enforced to prevent fast cycling through the
@ -42,4 +43,9 @@ public class ConnectionStateGracePeriodBeforeReconnect extends AbstractConnectio
public void onPckMessageReceived(String data) { public void onPckMessageReceived(String data) {
// nothing // nothing
} }
@Override
public void handleConnectionFailed(@Nullable Throwable e) {
// nothing
}
} }

View File

@ -72,15 +72,10 @@ public class ConnectionStateMachine extends AbstractStateMachine<ConnectionState
* *
* @param e the cause * @param e the cause
*/ */
public void handleConnectionFailed(@Nullable Throwable e) { public synchronized void handleConnectionFailed(@Nullable Throwable e) {
if (!(state instanceof ConnectionStateShutdown)) { AbstractConnectionState localState = state;
if (e != null) { if (localState != null) {
String message = e.getMessage(); localState.handleConnectionFailed(e);
connection.getCallback().onOffline(message != null ? message : "");
} else {
connection.getCallback().onOffline("");
}
setState(ConnectionStateGracePeriodBeforeReconnect::new);
} }
} }

View File

@ -13,6 +13,7 @@
package org.openhab.binding.lcn.internal.connection; package org.openhab.binding.lcn.internal.connection;
import org.eclipse.jdt.annotation.NonNullByDefault; 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.LcnAddr;
/** /**
@ -42,4 +43,9 @@ public class ConnectionStateShutdown extends AbstractConnectionState {
public void onPckMessageReceived(String data) { public void onPckMessageReceived(String data) {
// nothing // nothing
} }
@Override
public void handleConnectionFailed(@Nullable Throwable e) {
// nothing
}
} }

View File

@ -48,21 +48,28 @@ public class ConnectionStateWaitForLcnBusConnected extends AbstractConnectionSta
@Override @Override
public void onPckMessageReceived(String data) { public void onPckMessageReceived(String data) {
ScheduledFuture<?> localLegacyTimer = legacyTimer; switch (data) {
if (data.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) { case LcnDefs.LCNCONNSTATE_DISCONNECTED:
if (localLegacyTimer != null) { cancelLegacyTimer();
localLegacyTimer.cancel(true);
}
connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE"); connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE");
} else if (data.equals(LcnDefs.LCNCONNSTATE_CONNECTED)) { break;
if (localLegacyTimer != null) { case LcnDefs.LCNCONNSTATE_CONNECTED:
localLegacyTimer.cancel(true); cancelLegacyTimer();
}
connection.getCallback().onOnline(); connection.getCallback().onOnline();
nextState(ConnectionStateSendDimMode::new); nextState(ConnectionStateSendDimMode::new);
} else if (data.equals(LcnDefs.INSUFFICIENT_LICENSES)) { break;
context.handleConnectionFailed( case LcnDefs.INSUFFICIENT_LICENSES:
cancelLegacyTimer();
handleConnectionFailed(
new LcnException("LCN-PCHK/PKE has not enough licenses to handle this connection")); new LcnException("LCN-PCHK/PKE has not enough licenses to handle this connection"));
break;
}
}
private void cancelLegacyTimer() {
ScheduledFuture<?> localLegacyTimer = legacyTimer;
if (localLegacyTimer != null) {
localLegacyTimer.cancel(true);
} }
} }
} }