]> git.basschouten.com Git - openhab-addons.git/blob
b70d145efca43d0bfb4280386b4d2efdffb44efe
[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.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 synchronized void handleConnectionFailed(@Nullable Throwable e) {
76         AbstractConnectionState localState = state;
77         if (localState != null) {
78             localState.handleConnectionFailed(e);
79         }
80     }
81
82     /**
83      * Processes a received PCK message by passing it to the current State.
84      *
85      * @param data the PCK message
86      */
87     public synchronized void onInputReceived(String data) {
88         AbstractConnectionState localState = state;
89         if (localState != null) {
90             localState.onPckMessageReceived(data);
91         }
92     }
93
94     /**
95      * Shuts the StateMachine down finally. A shut-down StateMachine cannot be re-used.
96      */
97     public void shutdownFinally() {
98         AbstractConnectionState localState = state;
99         if (localState != null) {
100             localState.shutdownFinally();
101         }
102     }
103 }