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.io.IOException;
16 import java.nio.channels.Channel;
17 import java.util.concurrent.ScheduledExecutorService;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lcn.internal.common.LcnAddr;
22 import org.openhab.binding.lcn.internal.common.LcnDefs;
25 * Base class representing LCN-PCK gateway connection states.
27 * @author Fabian Wolter - Initial Contribution
30 public abstract class AbstractConnectionState extends AbstractState<ConnectionStateMachine, AbstractConnectionState> {
31 /** The PCK gateway's Connection */
32 protected final Connection connection;
34 public AbstractConnectionState(ConnectionStateMachine context) {
36 this.connection = context.getConnection();
40 * Callback method when a PCK message has been received.
42 * @param data the received PCK message without line termination character
44 public abstract void onPckMessageReceived(String data);
47 * Gets the framework's scheduler.
49 * @return the scheduler
51 public ScheduledExecutorService getScheduler() {
52 return context.getScheduler();
56 * Enqueues a PCK message to be sent. When the connection is offline, the message will be buffered and sent when the
57 * connection is established. When the enqueued PCK message is too old, it will be discarded before a new connection
60 * @param addr the module's address to which is message shall be sent
61 * @param wantsAck true, if the module shall respond with an Ack upon successful processing
62 * @param data the PCK message to be sent
64 public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
65 connection.queueOffline(addr, wantsAck, data);
69 * Shuts the Connection down finally. A shut-down connection cannot re-used.
71 public void shutdownFinally() {
72 nextState(ConnectionStateShutdown::new);
76 * Checks if the given PCK message is an LCN bus disconnect message. If so, openHAB will be informed and the
77 * Connection's State Machine waits for a re-connect.
79 * @param pck the PCK message to check
81 protected void parseLcnBusDiconnectMessage(String pck) {
82 if (pck.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) {
83 connection.getCallback().onOffline("LCN-PCHK/VISU not connected to LCN data wire");
84 nextState(ConnectionStateWaitForLcnBusConnectedAfterDisconnected::new);
89 * Invoked by any state, if the connection fails.
93 public void handleConnectionFailed(@Nullable Throwable e) {
94 synchronized (context) {
96 String message = e.getMessage();
97 connection.getCallback().onOffline(message != null ? message : "");
99 connection.getCallback().onOffline("");
101 context.setState(ConnectionStateGracePeriodBeforeReconnect::new);
106 * Closes the Connection SocketChannel.
108 protected void closeSocketChannel() {
110 Channel socketChannel = connection.getSocketChannel();
111 if (socketChannel != null) {
112 socketChannel.close();
114 } catch (IOException e) {