]> git.basschouten.com Git - openhab-addons.git/blob
8882042cebed41361ce9271db05a3738b88162dc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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         ScheduledFuture<?> localLegacyTimer = legacyTimer;
52         if (data.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) {
53             if (localLegacyTimer != null) {
54                 localLegacyTimer.cancel(true);
55             }
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);
60             }
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"));
66         }
67     }
68 }