2 * Copyright (c) 2010-2023 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) {
52 case LcnDefs.LCNCONNSTATE_DISCONNECTED:
54 connection.getCallback().onOffline("LCN-PCHK/VISU not connected to LCN data wire");
56 case LcnDefs.LCNCONNSTATE_CONNECTED:
58 connection.getCallback().onOnline();
59 nextState(ConnectionStateSendDimMode::new);
61 case LcnDefs.INSUFFICIENT_LICENSES:
63 handleConnectionFailed(
64 new LcnException("LCN-PCHK/VISU has not enough licenses to handle this connection"));
69 private void cancelLegacyTimer() {
70 ScheduledFuture<?> localLegacyTimer = legacyTimer;
71 if (localLegacyTimer != null) {
72 localLegacyTimer.cancel(true);