]> git.basschouten.com Git - openhab-addons.git/blob
49f2768709883f21a148ec70ea224221c5e24427
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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();
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         for (int i = 0; i < listeners.size(); i++) {
105             listeners.get(i).onNewMessageEvent(event);
106         }
107     }
108
109     @Override
110     public void sendMessage(byte[] data) {
111         try {
112             output.write(data);
113             output.flush();
114         } catch (IOException e) {
115             logger.debug("sendMessage(): Writing error: {}", e.getMessage(), e);
116             setConnected(false);
117         }
118     }
119
120     @Override
121     public int read(byte[] buffer) throws IOException {
122         return input.read(buffer);
123     }
124
125     @Override
126     public void addEventListener(PowermaxMessageEventListener listener) {
127         listeners.add(listener);
128     }
129
130     @Override
131     public void removeEventListener(PowermaxMessageEventListener listener) {
132         listeners.remove(listener);
133     }
134
135     /**
136      * @return the input stream
137      */
138     public InputStream getInput() {
139         return input;
140     }
141
142     /**
143      * Set the input stream
144      *
145      * @param input the input stream
146      */
147     public void setInput(InputStream input) {
148         this.input = input;
149     }
150
151     /**
152      * @return the output stream
153      */
154     public OutputStream getOutput() {
155         return output;
156     }
157
158     /**
159      * Set the output stream
160      *
161      * @param output the output stream
162      */
163     public void setOutput(OutputStream output) {
164         this.output = output;
165     }
166
167     /**
168      * @return true if connected or false if not
169      */
170     @Override
171     public boolean isConnected() {
172         return connected;
173     }
174
175     /**
176      * Set the connection state
177      *
178      * @param connected true if connected or false if not
179      */
180     public void setConnected(boolean connected) {
181         this.connected = connected;
182     }
183
184     /**
185      * @return the thread that handles the message reading
186      */
187     public Thread getReaderThread() {
188         return readerThread;
189     }
190
191     /**
192      * Set the thread that handles the message reading
193      *
194      * @param readerThread the thread
195      */
196     public void setReaderThread(Thread readerThread) {
197         this.readerThread = readerThread;
198     }
199
200     /**
201      * @return the start time of the time frame to receive a response
202      */
203     public synchronized long getWaitingForResponse() {
204         return waitingForResponse;
205     }
206
207     /**
208      * Set the start time of the time frame to receive a response
209      *
210      * @param timeLastReceive the time in milliseconds
211      */
212     public synchronized void setWaitingForResponse(long waitingForResponse) {
213         this.waitingForResponse = waitingForResponse;
214     }
215 }