]> git.basschouten.com Git - openhab-addons.git/blob
78a76b73079f8b20a9a2960b936f04ace577e380
[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.io.IOException;
16 import java.nio.channels.Channel;
17 import java.util.concurrent.ScheduledExecutorService;
18
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;
23
24 /**
25  * Base class representing LCN-PCK gateway connection states.
26  *
27  * @author Fabian Wolter - Initial Contribution
28  */
29 @NonNullByDefault
30 public abstract class AbstractConnectionState extends AbstractState<ConnectionStateMachine, AbstractConnectionState> {
31     /** The PCK gateway's Connection */
32     protected final Connection connection;
33
34     public AbstractConnectionState(ConnectionStateMachine context) {
35         super(context);
36         this.connection = context.getConnection();
37     }
38
39     /**
40      * Callback method when a PCK message has been received.
41      *
42      * @param data the received PCK message without line termination character
43      */
44     public abstract void onPckMessageReceived(String data);
45
46     /**
47      * Gets the framework's scheduler.
48      *
49      * @return the scheduler
50      */
51     public ScheduledExecutorService getScheduler() {
52         return context.getScheduler();
53     }
54
55     /**
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
58      * is established.
59      *
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
63      */
64     public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
65         connection.queueOffline(addr, wantsAck, data);
66     }
67
68     /**
69      * Shuts the Connection down finally. A shut-down connection cannot re-used.
70      */
71     public void shutdownFinally() {
72         nextState(ConnectionStateShutdown::new);
73     }
74
75     /**
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.
78      *
79      * @param pck the PCK message to check
80      */
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);
85         }
86     }
87
88     /**
89      * Invoked by any state, if the connection fails.
90      *
91      * @param e the cause
92      */
93     public void handleConnectionFailed(@Nullable Throwable e) {
94         synchronized (context) {
95             if (e != null) {
96                 String message = e.getMessage();
97                 connection.getCallback().onOffline(message != null ? message : "");
98             } else {
99                 connection.getCallback().onOffline("");
100             }
101             context.setState(ConnectionStateGracePeriodBeforeReconnect::new);
102         }
103     }
104
105     /**
106      * Closes the Connection SocketChannel.
107      */
108     protected void closeSocketChannel() {
109         try {
110             Channel socketChannel = connection.getSocketChannel();
111             if (socketChannel != null) {
112                 socketChannel.close();
113             }
114         } catch (IOException e) {
115             // ignore
116         }
117     }
118 }