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.powermax.internal.connector;
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;
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;
28 * An abstract class for the communication with the Visonic alarm panel that
29 * handles stuff common to all communication types
31 * @author Laurent Garnier - Initial contribution
33 public abstract class PowermaxConnector implements PowermaxConnectorInterface {
35 private final Logger logger = LoggerFactory.getLogger(PowermaxConnector.class);
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<>();
45 public PowermaxConnector(String readerThreadName) {
46 this.readerThreadName = readerThreadName;
50 public abstract void open() throws Exception;
53 public abstract void close();
56 * Cleanup everything; to be called when closing the communication
58 protected void cleanup(boolean closeStreams) {
59 logger.debug("cleanup(): cleaning up Connection");
61 if (readerThread != null) {
62 readerThread.interrupt();
65 } catch (InterruptedException e) {
73 } catch (IOException e) {
74 logger.debug("Error while closing the output stream: {}", e.getMessage());
81 } catch (IOException e) {
82 logger.debug("Error while closing the input stream: {}", e.getMessage());
91 logger.debug("cleanup(): Connection Cleanup");
95 * Handles an incoming message
97 * @param incomingMessage the received message as a table of bytes
99 public void handleIncomingMessage(byte[] incomingMessage) {
100 PowermaxMessageEvent event = new PowermaxMessageEvent(this,
101 PowermaxBaseMessage.getMessageHandler(incomingMessage));
103 // send message to event listeners
104 listeners.forEach(listener -> listener.onNewMessageEvent(event));
108 * Handles a communication failure
110 public void handleCommunicationFailure(String message) {
112 listeners.forEach(listener -> listener.onCommunicationFailure(message));
116 public void sendMessage(byte[] data) {
120 } catch (IOException e) {
121 logger.debug("sendMessage(): Writing error: {}", e.getMessage(), e);
122 handleCommunicationFailure(e.getMessage());
127 public int read(byte[] buffer) throws IOException {
128 return input.read(buffer);
132 public void addEventListener(PowermaxMessageEventListener listener) {
133 listeners.add(listener);
137 public void removeEventListener(PowermaxMessageEventListener listener) {
138 listeners.remove(listener);
142 * @return the input stream
144 public InputStream getInput() {
149 * Set the input stream
151 * @param input the input stream
153 public void setInput(InputStream input) {
158 * @return the output stream
160 public OutputStream getOutput() {
165 * Set the output stream
167 * @param output the output stream
169 public void setOutput(OutputStream output) {
170 this.output = output;
174 * @return true if connected or false if not
177 public boolean isConnected() {
182 * Set the connection state
184 * @param connected true if connected or false if not
186 public void setConnected(boolean connected) {
187 this.connected = connected;
191 * @return the thread that handles the message reading
193 public Thread getReaderThread() {
198 * Set the thread that handles the message reading
200 * @param readerThread the thread
202 public void setReaderThread(Thread readerThread) {
203 this.readerThread = readerThread;
207 * @return the start time of the time frame to receive a response
209 public synchronized long getWaitingForResponse() {
210 return waitingForResponse;
214 * Set the start time of the time frame to receive a response
216 * @param timeLastReceive the time in milliseconds
218 public synchronized void setWaitingForResponse(long waitingForResponse) {
219 this.waitingForResponse = waitingForResponse;