]> git.basschouten.com Git - openhab-addons.git/blob
78c1e0cc2ba0029808e3d189ba1b951007ba190c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openhab.binding.lcn.internal.common.LcnAddr;
21 import org.openhab.binding.lcn.internal.common.LcnDefs;
22
23 /**
24  * Base class representing LCN-PCK gateway connection states.
25  *
26  * @author Fabian Wolter - Initial Contribution
27  */
28 @NonNullByDefault
29 public abstract class AbstractConnectionState extends AbstractState<ConnectionStateMachine, AbstractConnectionState> {
30     /** The PCK gateway's Connection */
31     protected final Connection connection;
32
33     public AbstractConnectionState(ConnectionStateMachine context) {
34         super(context);
35         this.connection = context.getConnection();
36     }
37
38     /**
39      * Callback method when a PCK message has been received.
40      *
41      * @param data the received PCK message without line termination character
42      */
43     public abstract void onPckMessageReceived(String data);
44
45     /**
46      * Gets the framework's scheduler.
47      *
48      * @return the scheduler
49      */
50     public ScheduledExecutorService getScheduler() {
51         return context.getScheduler();
52     }
53
54     /**
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
57      * is established.
58      *
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
62      */
63     public void queue(LcnAddr addr, boolean wantsAck, byte[] data) {
64         connection.queueOffline(addr, wantsAck, data);
65     }
66
67     /**
68      * Shuts the Connection down finally. A shut-down connection cannot re-used.
69      */
70     public void shutdownFinally() {
71         nextState(ConnectionStateShutdown::new);
72     }
73
74     /**
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.
77      *
78      * @param pck the PCK message to check
79      */
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);
84         }
85     }
86
87     /**
88      * Closes the Connection SocketChannel.
89      */
90     protected void closeSocketChannel() {
91         try {
92             Channel socketChannel = connection.getSocketChannel();
93             if (socketChannel != null) {
94                 socketChannel.close();
95             }
96         } catch (IOException e) {
97             // ignore
98         }
99     }
100 }