2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lcn.internal.connection;
15 import java.util.concurrent.ScheduledExecutorService;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lcn.internal.common.LcnAddr;
22 * Implements a state machine for managing the connection to the LCN-PCK gateway. Setting states is thread-safe.
24 * @author Fabian Wolter - Initial Contribution
27 public class ConnectionStateMachine extends AbstractStateMachine<ConnectionStateMachine, AbstractConnectionState> {
28 private final Connection connection;
29 final ScheduledExecutorService scheduler;
31 public ConnectionStateMachine(Connection connection, ScheduledExecutorService scheduler) {
32 this.connection = connection;
33 this.scheduler = scheduler;
35 setState(ConnectionStateInit::new);
39 * Gets the framework's scheduler.
41 * @return the scheduler
43 protected ScheduledExecutorService getScheduler() {
48 * Gets the PCHK Connection object.
50 * @return the connection
52 public Connection getConnection() {
57 * Enqueues a PCK command. Implementation is state dependent.
59 * @param addr the destination address
60 * @param wantsAck true, if the module shall respond with an Ack
61 * @param data the data
63 public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
64 AbstractConnectionState localState = state;
65 if (localState != null) {
66 localState.queue(addr, wantsAck, data);
71 * Invoked by any state, if the connection fails.
75 public synchronized void handleConnectionFailed(@Nullable Throwable e) {
76 AbstractConnectionState localState = state;
77 if (localState != null) {
78 localState.handleConnectionFailed(e);
83 * Processes a received PCK message by passing it to the current State.
85 * @param data the PCK message
87 public synchronized void onInputReceived(String data) {
88 AbstractConnectionState localState = state;
89 if (localState != null) {
90 localState.onPckMessageReceived(data);
95 * Shuts the StateMachine down finally. A shut-down StateMachine cannot be re-used.
97 public void shutdownFinally() {
98 AbstractConnectionState localState = state;
99 if (localState != null) {
100 localState.shutdownFinally();