2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lcn.internal.connection;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.lcn.internal.common.LcnDefs;
21 import org.openhab.binding.lcn.internal.common.LcnException;
24 * This state waits for the status answer of the LCN-PCK gateway after connection establishment, rather the LCN bus is
27 * @author Fabian Wolter - Initial Contribution
30 public class ConnectionStateWaitForLcnBusConnected extends AbstractConnectionState {
31 private @Nullable ScheduledFuture<?> legacyTimer;
33 public ConnectionStateWaitForLcnBusConnected(ConnectionStateMachine context) {
38 public void startWorking() {
39 // Legacy support for LCN-PCHK 2.2 and earlier:
40 // There was no explicit "LCN connected" notification after successful authentication.
41 // Only "LCN disconnected" would be reported immediately. That means "LCN connected" used to be the default.
42 ScheduledFuture<?> localLegacyTimer = legacyTimer = getScheduler().schedule(() -> {
43 connection.getCallback().onOnline();
44 nextState(ConnectionStateSendDimMode::new);
45 }, connection.getSettings().getTimeout(), TimeUnit.MILLISECONDS);
46 addTimer(localLegacyTimer);
50 public void onPckMessageReceived(String data) {
51 ScheduledFuture<?> localLegacyTimer = legacyTimer;
52 if (data.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) {
53 if (localLegacyTimer != null) {
54 localLegacyTimer.cancel(true);
56 connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE");
57 } else if (data.equals(LcnDefs.LCNCONNSTATE_CONNECTED)) {
58 if (localLegacyTimer != null) {
59 localLegacyTimer.cancel(true);
61 connection.getCallback().onOnline();
62 nextState(ConnectionStateSendDimMode::new);
63 } else if (data.equals(LcnDefs.INSUFFICIENT_LICENSES)) {
64 context.handleConnectionFailed(
65 new LcnException("LCN-PCHK/PKE has not enough licenses to handle this connection"));