]> git.basschouten.com Git - openhab-addons.git/blob
4764c764ad300c874dbbba016949fdd869593e8a
[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.powermax.internal.connector;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.openhab.binding.powermax.internal.message.PowermaxBaseMessage;
22 import org.openhab.binding.powermax.internal.message.PowermaxMessageEvent;
23 import org.openhab.binding.powermax.internal.message.PowermaxMessageEventListener;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * An abstract class for the communication with the Visonic alarm panel that
29  * handles stuff common to all communication types
30  *
31  * @author Laurent Garnier - Initial contribution
32  */
33 public abstract class PowermaxConnector implements PowermaxConnectorInterface {
34
35     private final Logger logger = LoggerFactory.getLogger(PowermaxConnector.class);
36
37     private InputStream input;
38     private OutputStream output;
39     private boolean connected;
40     protected String readerThreadName;
41     private Thread readerThread;
42     private long waitingForResponse;
43     private List<PowermaxMessageEventListener> listeners = new ArrayList<>();
44
45     public PowermaxConnector(String readerThreadName) {
46         this.readerThreadName = readerThreadName;
47     }
48
49     @Override
50     public abstract void open() throws Exception;
51
52     @Override
53     public abstract void close();
54
55     /**
56      * Cleanup everything; to be called when closing the communication
57      */
58     protected void cleanup(boolean closeStreams) {
59         logger.debug("cleanup(): cleaning up Connection");
60
61         if (readerThread != null) {
62             readerThread.interrupt();
63             try {
64                 readerThread.join();
65             } catch (InterruptedException e) {
66             }
67         }
68
69         if (closeStreams) {
70             if (output != null) {
71                 try {
72                     output.close();
73                 } catch (IOException e) {
74                     logger.debug("Error while closing the output stream: {}", e.getMessage());
75                 }
76             }
77
78             if (input != null) {
79                 try {
80                     input.close();
81                 } catch (IOException e) {
82                     logger.debug("Error while closing the input stream: {}", e.getMessage());
83                 }
84             }
85         }
86
87         readerThread = null;
88         input = null;
89         output = null;
90
91         logger.debug("cleanup(): Connection Cleanup");
92     }
93
94     /**
95      * Handles an incoming message
96      *
97      * @param incomingMessage the received message as a table of bytes
98      */
99     public void handleIncomingMessage(byte[] incomingMessage) {
100         PowermaxMessageEvent event = new PowermaxMessageEvent(this,
101                 PowermaxBaseMessage.getMessageHandler(incomingMessage));
102
103         // send message to event listeners
104         listeners.forEach(listener -> listener.onNewMessageEvent(event));
105     }
106
107     /**
108      * Handles a communication failure
109      */
110     public void handleCommunicationFailure(String message) {
111         close();
112         listeners.forEach(listener -> listener.onCommunicationFailure(message));
113     }
114
115     @Override
116     public void sendMessage(byte[] data) {
117         try {
118             output.write(data);
119             output.flush();
120         } catch (IOException e) {
121             logger.debug("sendMessage(): Writing error: {}", e.getMessage(), e);
122             handleCommunicationFailure(e.getMessage());
123         }
124     }
125
126     @Override
127     public int read(byte[] buffer) throws IOException {
128         return input.read(buffer);
129     }
130
131     @Override
132     public void addEventListener(PowermaxMessageEventListener listener) {
133         listeners.add(listener);
134     }
135
136     @Override
137     public void removeEventListener(PowermaxMessageEventListener listener) {
138         listeners.remove(listener);
139     }
140
141     /**
142      * @return the input stream
143      */
144     public InputStream getInput() {
145         return input;
146     }
147
148     /**
149      * Set the input stream
150      *
151      * @param input the input stream
152      */
153     public void setInput(InputStream input) {
154         this.input = input;
155     }
156
157     /**
158      * @return the output stream
159      */
160     public OutputStream getOutput() {
161         return output;
162     }
163
164     /**
165      * Set the output stream
166      *
167      * @param output the output stream
168      */
169     public void setOutput(OutputStream output) {
170         this.output = output;
171     }
172
173     /**
174      * @return true if connected or false if not
175      */
176     @Override
177     public boolean isConnected() {
178         return connected;
179     }
180
181     /**
182      * Set the connection state
183      *
184      * @param connected true if connected or false if not
185      */
186     public void setConnected(boolean connected) {
187         this.connected = connected;
188     }
189
190     /**
191      * @return the thread that handles the message reading
192      */
193     public Thread getReaderThread() {
194         return readerThread;
195     }
196
197     /**
198      * Set the thread that handles the message reading
199      *
200      * @param readerThread the thread
201      */
202     public void setReaderThread(Thread readerThread) {
203         this.readerThread = readerThread;
204     }
205
206     /**
207      * @return the start time of the time frame to receive a response
208      */
209     public synchronized long getWaitingForResponse() {
210         return waitingForResponse;
211     }
212
213     /**
214      * Set the start time of the time frame to receive a response
215      *
216      * @param timeLastReceive the time in milliseconds
217      */
218     public synchronized void setWaitingForResponse(long waitingForResponse) {
219         this.waitingForResponse = waitingForResponse;
220     }
221 }