2 * Copyright (c) 2010-2020 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();
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 for (int i = 0; i < listeners.size(); i++) {
105 listeners.get(i).onNewMessageEvent(event);
110 public void sendMessage(byte[] data) {
114 } catch (IOException e) {
115 logger.debug("sendMessage(): Writing error: {}", e.getMessage(), e);
121 public int read(byte[] buffer) throws IOException {
122 return input.read(buffer);
126 public void addEventListener(PowermaxMessageEventListener listener) {
127 listeners.add(listener);
131 public void removeEventListener(PowermaxMessageEventListener listener) {
132 listeners.remove(listener);
136 * @return the input stream
138 public InputStream getInput() {
143 * Set the input stream
145 * @param input the input stream
147 public void setInput(InputStream input) {
152 * @return the output stream
154 public OutputStream getOutput() {
159 * Set the output stream
161 * @param output the output stream
163 public void setOutput(OutputStream output) {
164 this.output = output;
168 * @return true if connected or false if not
171 public boolean isConnected() {
176 * Set the connection state
178 * @param connected true if connected or false if not
180 public void setConnected(boolean connected) {
181 this.connected = connected;
185 * @return the thread that handles the message reading
187 public Thread getReaderThread() {
192 * Set the thread that handles the message reading
194 * @param readerThread the thread
196 public void setReaderThread(Thread readerThread) {
197 this.readerThread = readerThread;
201 * @return the start time of the time frame to receive a response
203 public synchronized long getWaitingForResponse() {
204 return waitingForResponse;
208 * Set the start time of the time frame to receive a response
210 * @param timeLastReceive the time in milliseconds
212 public synchronized void setWaitingForResponse(long waitingForResponse) {
213 this.waitingForResponse = waitingForResponse;