]> git.basschouten.com Git - openhab-addons.git/blob
16c293861c89e9cb527a24afd9d904b2de9fa1aa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lcn.internal.connection;
14
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
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;
22
23 /**
24  * This state waits for the status answer of the LCN-PCK gateway after connection establishment, rather the LCN bus is
25  * connected.
26  *
27  * @author Fabian Wolter - Initial Contribution
28  */
29 @NonNullByDefault
30 public class ConnectionStateWaitForLcnBusConnected extends AbstractConnectionState {
31     private @Nullable ScheduledFuture<?> legacyTimer;
32
33     public ConnectionStateWaitForLcnBusConnected(ConnectionStateMachine context) {
34         super(context);
35     }
36
37     @Override
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);
47     }
48
49     @Override
50     public void onPckMessageReceived(String data) {
51         switch (data) {
52             case LcnDefs.LCNCONNSTATE_DISCONNECTED:
53                 cancelLegacyTimer();
54                 connection.getCallback().onOffline("LCN-PCHK/VISU not connected to LCN data wire");
55                 break;
56             case LcnDefs.LCNCONNSTATE_CONNECTED:
57                 cancelLegacyTimer();
58                 connection.getCallback().onOnline();
59                 nextState(ConnectionStateSendDimMode::new);
60                 break;
61             case LcnDefs.INSUFFICIENT_LICENSES:
62                 cancelLegacyTimer();
63                 handleConnectionFailed(
64                         new LcnException("LCN-PCHK/VISU has not enough licenses to handle this connection"));
65                 break;
66         }
67     }
68
69     private void cancelLegacyTimer() {
70         ScheduledFuture<?> localLegacyTimer = legacyTimer;
71         if (localLegacyTimer != null) {
72             localLegacyTimer.cancel(true);
73         }
74     }
75 }