]> git.basschouten.com Git - openhab-addons.git/blob
9e83f7ffeaa3b008fef8c080bb02444203670f6f
[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.ScheduledExecutorService;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lcn.internal.common.LcnAddr;
20
21 /**
22  * Implements a state machine for managing the connection to the LCN-PCK gateway. Setting states is thread-safe.
23  *
24  * @author Fabian Wolter - Initial Contribution
25  */
26 @NonNullByDefault
27 public class ConnectionStateMachine extends AbstractStateMachine<ConnectionStateMachine, AbstractConnectionState> {
28     private final Connection connection;
29     final ScheduledExecutorService scheduler;
30
31     public ConnectionStateMachine(Connection connection, ScheduledExecutorService scheduler) {
32         this.connection = connection;
33         this.scheduler = scheduler;
34
35         setState(ConnectionStateInit::new);
36     }
37
38     /**
39      * Gets the framework's scheduler.
40      *
41      * @return the scheduler
42      */
43     protected ScheduledExecutorService getScheduler() {
44         return scheduler;
45     }
46
47     /**
48      * Gets the PCHK Connection object.
49      * 
50      * @return the connection
51      */
52     public Connection getConnection() {
53         return connection;
54     }
55
56     /**
57      * Enqueues a PCK command. Implementation is state dependent.
58      *
59      * @param addr the destination address
60      * @param wantsAck true, if the module shall respond with an Ack
61      * @param data the data
62      */
63     public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
64         AbstractConnectionState localState = state;
65         if (localState != null) {
66             localState.queue(addr, wantsAck, data);
67         }
68     }
69
70     /**
71      * Invoked by any state, if the connection fails.
72      *
73      * @param e the cause
74      */
75     public void handleConnectionFailed(@Nullable Throwable e) {
76         if (!(state instanceof ConnectionStateShutdown)) {
77             if (e != null) {
78                 connection.getCallback().onOffline(e.getMessage());
79             } else {
80                 connection.getCallback().onOffline("");
81             }
82             setState(ConnectionStateGracePeriodBeforeReconnect::new);
83         }
84     }
85
86     /**
87      * Processes a received PCK message by passing it to the current State.
88      *
89      * @param data the PCK message
90      */
91     public void onInputReceived(String data) {
92         AbstractConnectionState localState = state;
93         if (localState != null) {
94             localState.onPckMessageReceived(data);
95         }
96     }
97
98     /**
99      * Shuts the StateMachine down finally. A shut-down StateMachine cannot be re-used.
100      */
101     public void shutdownFinally() {
102         AbstractConnectionState localState = state;
103         if (localState != null) {
104             localState.shutdownFinally();
105         }
106     }
107 }