2 * Copyright (c) 2010-2021 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.openhab.binding.lcn.internal.common.LcnAddr;
21 import org.openhab.binding.lcn.internal.common.LcnDefs;
24 * Base class representing LCN-PCK gateway connection states.
26 * @author Fabian Wolter - Initial Contribution
29 public abstract class AbstractConnectionState extends AbstractState<ConnectionStateMachine, AbstractConnectionState> {
30 /** The PCK gateway's Connection */
31 protected final Connection connection;
33 public AbstractConnectionState(ConnectionStateMachine context) {
35 this.connection = context.getConnection();
39 * Callback method when a PCK message has been received.
41 * @param data the received PCK message without line termination character
43 public abstract void onPckMessageReceived(String data);
46 * Gets the framework's scheduler.
48 * @return the scheduler
50 public ScheduledExecutorService getScheduler() {
51 return context.getScheduler();
55 * Enqueues a PCK message to be sent. When the connection is offline, the message will be buffered and sent when the
56 * connection is established. When the enqueued PCK message is too old, it will be discarded before a new connection
59 * @param addr the module's address to which is message shall be sent
60 * @param wantsAck true, if the module shall respond with an Ack upon successful processing
61 * @param data the PCK message to be sent
63 public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
64 connection.queueOffline(addr, wantsAck, data);
68 * Shuts the Connection down finally. A shut-down connection cannot re-used.
70 public void shutdownFinally() {
71 nextState(ConnectionStateShutdown::new);
75 * Checks if the given PCK message is an LCN bus disconnect message. If so, openHAB will be informed and the
76 * Connection's State Machine waits for a re-connect.
78 * @param pck the PCK message to check
80 protected void parseLcnBusDiconnectMessage(String pck) {
81 if (pck.equals(LcnDefs.LCNCONNSTATE_DISCONNECTED)) {
82 connection.getCallback().onOffline("LCN bus not connected to LCN-PCHK/PKE");
83 nextState(ConnectionStateWaitForLcnBusConnectedAfterDisconnected::new);
88 * Closes the Connection SocketChannel.
90 protected void closeSocketChannel() {
92 Channel socketChannel = connection.getSocketChannel();
93 if (socketChannel != null) {
94 socketChannel.close();
96 } catch (IOException e) {