]> git.basschouten.com Git - openhab-addons.git/blob
559778faa9d84329fbd2e18514028eaa47947089
[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                 String message = e.getMessage();
79                 connection.getCallback().onOffline(message != null ? message : "");
80             } else {
81                 connection.getCallback().onOffline("");
82             }
83             setState(ConnectionStateGracePeriodBeforeReconnect::new);
84         }
85     }
86
87     /**
88      * Processes a received PCK message by passing it to the current State.
89      *
90      * @param data the PCK message
91      */
92     public void onInputReceived(String data) {
93         AbstractConnectionState localState = state;
94         if (localState != null) {
95             localState.onPckMessageReceived(data);
96         }
97     }
98
99     /**
100      * Shuts the StateMachine down finally. A shut-down StateMachine cannot be re-used.
101      */
102     public void shutdownFinally() {
103         AbstractConnectionState localState = state;
104         if (localState != null) {
105             localState.shutdownFinally();
106         }
107     }
108 }