Skip to content

Commit

Permalink
[powermax] Add null annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Sep 20, 2021
1 parent ce8fac8 commit 5a8d9ac
Show file tree
Hide file tree
Showing 46 changed files with 661 additions and 425 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@
*/
package org.openhab.binding.powermax.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link PowermaxIpConfiguration} is responsible for holding
* configuration informations associated to a Powermax IP thing type
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxIpConfiguration {

public String ip;
public Integer tcpPort;
public Integer motionOffDelay;
public Boolean allowArming;
public Boolean allowDisarming;
public String pinCode;
public Boolean forceStandardMode;
public String panelType;
public Boolean autoSyncTime;
public String ip = "";
public int tcpPort = 0;
public int motionOffDelay = 3;
public boolean allowArming = false;
public boolean allowDisarming = false;
public String pinCode = "";
public boolean forceStandardMode = false;
public String panelType = "PowerMaxPro";
public boolean autoSyncTime = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
*/
package org.openhab.binding.powermax.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link PowermaxSerialConfiguration} is responsible for holding
* configuration informations associated to a Powermax serial thing type
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxSerialConfiguration {

public String serialPort;
public Integer motionOffDelay;
public Boolean allowArming;
public Boolean allowDisarming;
public String pinCode;
public Boolean forceStandardMode;
public String panelType;
public Boolean autoSyncTime;
public String serialPort = "";
public int motionOffDelay = 3;
public boolean allowArming = false;
public boolean allowDisarming = false;
public String pinCode = "";
public boolean forceStandardMode = false;
public String panelType = "PowerMaxPro";
public boolean autoSyncTime = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
*/
package org.openhab.binding.powermax.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link PowermaxX10Configuration} is responsible for holding
* configuration informations associated to a Powermax IP thing type
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxX10Configuration {

public static final String DEVICE_NUMBER = "deviceNumber";

public Integer deviceNumber;
public int deviceNumber = -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
*/
package org.openhab.binding.powermax.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link PowermaxZoneConfiguration} is responsible for holding
* configuration informations associated to a Powermax IP thing type
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxZoneConfiguration {

public static final String ZONE_NUMBER = "zoneNumber";

public Integer zoneNumber;
public int zoneNumber = -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.powermax.internal.message.PowermaxBaseMessage;
import org.openhab.binding.powermax.internal.message.PowermaxMessageEvent;
import org.openhab.binding.powermax.internal.message.PowermaxMessageEventListener;
Expand All @@ -30,15 +32,16 @@
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public abstract class PowermaxConnector implements PowermaxConnectorInterface {

private final Logger logger = LoggerFactory.getLogger(PowermaxConnector.class);

private InputStream input;
private OutputStream output;
private @Nullable InputStream input;
private @Nullable OutputStream output;
private boolean connected;
protected String readerThreadName;
private Thread readerThread;
private @Nullable Thread readerThread;
private long waitingForResponse;
private List<PowermaxMessageEventListener> listeners = new ArrayList<>();

Expand All @@ -58,26 +61,29 @@ public PowermaxConnector(String readerThreadName) {
protected void cleanup(boolean closeStreams) {
logger.debug("cleanup(): cleaning up Connection");

if (readerThread != null) {
readerThread.interrupt();
Thread thread = readerThread;
if (thread != null) {
thread.interrupt();
try {
readerThread.join();
thread.join();
} catch (InterruptedException e) {
}
}

if (closeStreams) {
if (output != null) {
OutputStream out = output;
if (out != null) {
try {
output.close();
out.close();
} catch (IOException e) {
logger.debug("Error while closing the output stream: {}", e.getMessage());
}
}

if (input != null) {
InputStream in = input;
if (in != null) {
try {
input.close();
in.close();
} catch (IOException e) {
logger.debug("Error while closing the input stream: {}", e.getMessage());
}
Expand Down Expand Up @@ -107,16 +113,20 @@ public void handleIncomingMessage(byte[] incomingMessage) {
/**
* Handles a communication failure
*/
public void handleCommunicationFailure(String message) {
public void handleCommunicationFailure(@Nullable String message) {
close();
listeners.forEach(listener -> listener.onCommunicationFailure(message));
listeners.forEach(listener -> listener.onCommunicationFailure(message != null ? message : ""));
}

@Override
public void sendMessage(byte[] data) {
try {
output.write(data);
output.flush();
OutputStream out = output;
if (out == null) {
throw new IOException("output stream is undefined");
}
out.write(data);
out.flush();
} catch (IOException e) {
logger.debug("sendMessage(): Writing error: {}", e.getMessage(), e);
handleCommunicationFailure(e.getMessage());
Expand All @@ -125,7 +135,11 @@ public void sendMessage(byte[] data) {

@Override
public int read(byte[] buffer) throws IOException {
return input.read(buffer);
InputStream in = input;
if (in == null) {
throw new IOException("input stream is undefined");
}
return in.read(buffer);
}

@Override
Expand All @@ -141,7 +155,7 @@ public void removeEventListener(PowermaxMessageEventListener listener) {
/**
* @return the input stream
*/
public InputStream getInput() {
public @Nullable InputStream getInput() {
return input;
}

Expand All @@ -150,14 +164,14 @@ public InputStream getInput() {
*
* @param input the input stream
*/
public void setInput(InputStream input) {
public void setInput(@Nullable InputStream input) {
this.input = input;
}

/**
* @return the output stream
*/
public OutputStream getOutput() {
public @Nullable OutputStream getOutput() {
return output;
}

Expand All @@ -166,7 +180,7 @@ public OutputStream getOutput() {
*
* @param output the output stream
*/
public void setOutput(OutputStream output) {
public void setOutput(@Nullable OutputStream output) {
this.output = output;
}

Expand All @@ -190,7 +204,7 @@ public void setConnected(boolean connected) {
/**
* @return the thread that handles the message reading
*/
public Thread getReaderThread() {
public @Nullable Thread getReaderThread() {
return readerThread;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

import java.io.IOException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.powermax.internal.message.PowermaxMessageEventListener;

/**
* Interface for communication with the Visonic alarm panel
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface PowermaxConnectorInterface {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.InterruptedIOException;
import java.util.Arrays;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.powermax.internal.message.PowermaxCommManager;
import org.openhab.binding.powermax.internal.message.PowermaxReceiveType;
import org.openhab.core.util.HexUtils;
Expand All @@ -27,13 +28,14 @@
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxReaderThread extends Thread {

private final Logger logger = LoggerFactory.getLogger(PowermaxReaderThread.class);

private static final int READ_BUFFER_SIZE = 20;
private static final int MAX_MSG_SIZE = 0xC0;

private final Logger logger = LoggerFactory.getLogger(PowermaxReaderThread.class);

private PowermaxConnector connector;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
package org.openhab.binding.powermax.internal.connector;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.transport.serial.SerialPort;
import org.openhab.core.io.transport.serial.SerialPortEvent;
import org.openhab.core.io.transport.serial.SerialPortEventListener;
Expand All @@ -27,14 +31,15 @@
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {

private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);

private final String serialPortName;
private final int baudRate;
private final SerialPortManager serialPortManager;
private SerialPort serialPort;
private @Nullable SerialPort serialPort;

/**
* Constructor
Expand All @@ -50,7 +55,6 @@ public PowermaxSerialConnector(SerialPortManager serialPortManager, String seria
this.serialPortManager = serialPortManager;
this.serialPortName = serialPortName;
this.baudRate = baudRate;
this.serialPort = null;
}

@Override
Expand All @@ -65,26 +69,31 @@ public void open() throws Exception {
SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

serialPort = commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
serialPort.enableReceiveTimeout(250);
commPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
commPort.enableReceiveThreshold(1);
commPort.enableReceiveTimeout(250);

setInput(serialPort.getInputStream());
setOutput(serialPort.getOutputStream());
InputStream inputStream = commPort.getInputStream();
setInput(inputStream);
OutputStream outputStream = commPort.getOutputStream();
setOutput(outputStream);

getOutput().flush();
if (getInput().markSupported()) {
getInput().reset();
if (outputStream != null) {
outputStream.flush();
}
if (inputStream != null && inputStream.markSupported()) {
inputStream.reset();
}

// RXTX serial port library causes high CPU load
// Start event listener, which will just sleep and slow down event
// loop
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
commPort.addEventListener(this);
commPort.notifyOnDataAvailable(true);

setReaderThread(new PowermaxReaderThread(this, readerThreadName));
getReaderThread().start();
PowermaxReaderThread readerThread = new PowermaxReaderThread(this, readerThreadName);
setReaderThread(readerThread);
readerThread.start();

setConnected(true);
}
Expand All @@ -93,14 +102,15 @@ public void open() throws Exception {
public void close() {
logger.debug("close(): Closing Serial Connection");

if (serialPort != null) {
serialPort.removeEventListener();
SerialPort commPort = serialPort;
if (commPort != null) {
commPort.removeEventListener();
}

super.cleanup(true);

if (serialPort != null) {
serialPort.close();
if (commPort != null) {
commPort.close();
}

serialPort = null;
Expand Down
Loading

0 comments on commit 5a8d9ac

Please sign in to comment.