Skip to content

Commit

Permalink
[powermax] Thread naming (openhab#8236)
Browse files Browse the repository at this point in the history
Related to openhab#8216

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and CSchlipp committed Sep 12, 2020
1 parent 3471684 commit 8be106c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ public abstract class PowermaxConnector implements PowermaxConnectorInterface {
private InputStream input;
private OutputStream output;
private boolean connected;
protected String readerThreadName;
private Thread readerThread;
private long waitingForResponse;
private List<PowermaxMessageEventListener> listeners = new ArrayList<>();

public PowermaxConnector(String readerThreadName) {
this.readerThreadName = readerThreadName;
}

@Override
public abstract void open();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public class PowermaxReaderThread extends Thread {
*
* @param in the input stream
* @param connector the object that should handle the received message
* @param threadName the name of the thread
*/
public PowermaxReaderThread(PowermaxConnector connector) {
public PowermaxReaderThread(PowermaxConnector connector, String threadName) {
super(threadName);
this.connector = connector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public class PowermaxSerialConnector extends PowermaxConnector implements Serial
* @param serialPortManager the serial port manager
* @param serialPortName the serial port name
* @param baudRate the baud rate to be used
* @param readerThreadName the name of thread to be created
*/
public PowermaxSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate) {
public PowermaxSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
String readerThreadName) {
super(readerThreadName);
this.serialPortManager = serialPortManager;
this.serialPortName = serialPortName;
this.baudRate = baudRate;
Expand Down Expand Up @@ -87,7 +90,7 @@ public void open() {
logger.debug("Too Many Listeners Exception: {}", e.getMessage(), e);
}

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

setConnected(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public class PowermaxTcpConnector extends PowermaxConnector {
* @param ip the IP address
* @param port the TCP port number
* @param timeout the timeout for socket communications
* @param readerThreadName the name of thread to be created
*/
public PowermaxTcpConnector(String ip, int port, int timeout) {
public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
super(readerThreadName);
ipAddress = ip;
tcpPort = port;
connectTimeout = timeout;
Expand All @@ -63,7 +65,7 @@ public void open() {
setInput(tcpSocket.getInputStream());
setOutput(tcpSocket.getOutputStream());

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

setConnected(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ public void initialize() {

commManager = null;

String threadName = "OH-binding-" + getThing().getUID().getAsString();

String errorMsg = null;
if (getThing().getThingTypeUID().equals(BRIDGE_TYPE_SERIAL)) {
errorMsg = initializeBridgeSerial(getConfigAs(PowermaxSerialConfiguration.class));
errorMsg = initializeBridgeSerial(getConfigAs(PowermaxSerialConfiguration.class), threadName);
} else if (getThing().getThingTypeUID().equals(BRIDGE_TYPE_IP)) {
errorMsg = initializeBridgeIp(getConfigAs(PowermaxIpConfiguration.class));
errorMsg = initializeBridgeIp(getConfigAs(PowermaxIpConfiguration.class), threadName);
} else {
errorMsg = "Unexpected thing type " + getThing().getThingTypeUID();
}
Expand Down Expand Up @@ -141,7 +143,7 @@ public void initialize() {
}
}

private String initializeBridgeSerial(PowermaxSerialConfiguration config) {
private String initializeBridgeSerial(PowermaxSerialConfiguration config, String threadName) {
String errorMsg = null;
if (config.serialPort != null && !config.serialPort.trim().isEmpty()
&& !config.serialPort.trim().startsWith("rfc2217")) {
Expand All @@ -162,7 +164,7 @@ private String initializeBridgeSerial(PowermaxSerialConfiguration config) {
PowermaxArmMode.ARMED_NIGHT_INSTANT.setAllowedCommand(allowArming);

commManager = new PowermaxCommManager(config.serialPort, panelType, forceStandardMode, autoSyncTime,
serialPortManager);
serialPortManager, threadName);
} else {
if (config.serialPort != null && config.serialPort.trim().startsWith("rfc2217")) {
errorMsg = "Please use the IP Connection thing type for a serial over IP connection.";
Expand All @@ -173,7 +175,7 @@ private String initializeBridgeSerial(PowermaxSerialConfiguration config) {
return errorMsg;
}

private String initializeBridgeIp(PowermaxIpConfiguration config) {
private String initializeBridgeIp(PowermaxIpConfiguration config, String threadName) {
String errorMsg = null;
if (config.ip != null && !config.ip.trim().isEmpty() && config.tcpPort != null) {
motionOffDelay = getMotionOffDelaySetting(config.motionOffDelay, DEFAULT_MOTION_OFF_DELAY);
Expand All @@ -192,8 +194,8 @@ private String initializeBridgeIp(PowermaxIpConfiguration config) {
PowermaxArmMode.ARMED_NIGHT.setAllowedCommand(allowArming);
PowermaxArmMode.ARMED_NIGHT_INSTANT.setAllowedCommand(allowArming);

commManager = new PowermaxCommManager(config.ip, config.tcpPort, panelType, forceStandardMode,
autoSyncTime);
commManager = new PowermaxCommManager(config.ip, config.tcpPort, panelType, forceStandardMode, autoSyncTime,
threadName);
} else {
errorMsg = "ip and port settings must be defined in thing configuration";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ public class PowermaxCommManager implements PowermaxMessageEventListener {
private static final int DEFAULT_BAUD_RATE = 9600;
private static final int WAITING_DELAY_FOR_RESPONSE = 750;
private static final long DELAY_BETWEEN_SETUP_DOWNLOADS = TimeUnit.SECONDS.toMillis(45);
private static final String COMM_MANAGER_THREAD_POOL_NAME = "powermax-comm";

private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(COMM_MANAGER_THREAD_POOL_NAME);
private final ScheduledExecutorService scheduler;

/** The object to store the current settings of the Powermax alarm system */
private PowermaxPanelSettings panelSettings;
Expand Down Expand Up @@ -99,16 +97,19 @@ public class PowermaxCommManager implements PowermaxMessageEventListener {
* @param forceStandardMode true to force the standard mode rather than trying using the Powerlink mode
* @param autoSyncTime true for automatic sync time
* @param serialPortManager the serial port manager
* @param threadName the prefix name of threads to be created
*/
public PowermaxCommManager(String sPort, PowermaxPanelType panelType, boolean forceStandardMode,
boolean autoSyncTime, SerialPortManager serialPortManager) {
boolean autoSyncTime, SerialPortManager serialPortManager, String threadName) {
this.panelType = panelType;
this.forceStandardMode = forceStandardMode;
this.autoSyncTime = autoSyncTime;
this.panelSettings = new PowermaxPanelSettings(panelType);
this.scheduler = ThreadPoolManager.getScheduledPool(threadName + "-sender");
String serialPort = (sPort != null && !sPort.trim().isEmpty()) ? sPort.trim() : null;
if (serialPort != null) {
connector = new PowermaxSerialConnector(serialPortManager, serialPort, DEFAULT_BAUD_RATE);
connector = new PowermaxSerialConnector(serialPortManager, serialPort, DEFAULT_BAUD_RATE,
threadName + "-reader");
} else {
connector = null;
}
Expand All @@ -123,17 +124,19 @@ public PowermaxCommManager(String sPort, PowermaxPanelType panelType, boolean fo
* @param forceStandardMode true to force the standard mode rather than trying using the Powerlink mode
* @param autoSyncTime true for automatic sync time
* @param serialPortManager
* @param threadName the prefix name of threads to be created
*/
public PowermaxCommManager(String ip, int port, PowermaxPanelType panelType, boolean forceStandardMode,
boolean autoSyncTime) {
boolean autoSyncTime, String threadName) {
this.panelType = panelType;
this.forceStandardMode = forceStandardMode;
this.autoSyncTime = autoSyncTime;
this.panelSettings = new PowermaxPanelSettings(panelType);
this.scheduler = ThreadPoolManager.getScheduledPool(threadName + "-sender");
String ipAddress = (ip != null && !ip.trim().isEmpty()) ? ip.trim() : null;
int tcpPort = (port > 0) ? port : DEFAULT_TCP_PORT;
if (ipAddress != null) {
connector = new PowermaxTcpConnector(ipAddress, tcpPort, TCP_CONNECTION_TIMEOUT);
connector = new PowermaxTcpConnector(ipAddress, tcpPort, TCP_CONNECTION_TIMEOUT, threadName + "-reader");
} else {
connector = null;
}
Expand Down

0 comments on commit 8be106c

Please sign in to comment.