From d25cb4be062919406fff4dee42bf06bf0ccd03f0 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Tue, 4 Aug 2020 18:16:58 +0200 Subject: [PATCH] [powermax] Thread naming (#8236) Related to #8216 Signed-off-by: Laurent Garnier --- .../internal/connector/PowermaxConnector.java | 5 +++++ .../connector/PowermaxReaderThread.java | 4 +++- .../connector/PowermaxSerialConnector.java | 7 +++++-- .../connector/PowermaxTcpConnector.java | 6 ++++-- .../internal/handler/PowermaxBridgeHandler.java | 16 +++++++++------- .../internal/message/PowermaxCommManager.java | 17 ++++++++++------- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxConnector.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxConnector.java index 0da7f1dbf082d..49f2768709883 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxConnector.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxConnector.java @@ -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 listeners = new ArrayList<>(); + public PowermaxConnector(String readerThreadName) { + this.readerThreadName = readerThreadName; + } + @Override public abstract void open(); diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxReaderThread.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxReaderThread.java index 69f76883deff5..30eba3a7b9692 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxReaderThread.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxReaderThread.java @@ -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; } diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxSerialConnector.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxSerialConnector.java index af98248516fc9..c6f6f25f872de 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxSerialConnector.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxSerialConnector.java @@ -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; @@ -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); diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxTcpConnector.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxTcpConnector.java index f5bb3fa0d7c4b..cd26a02550d4c 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxTcpConnector.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/connector/PowermaxTcpConnector.java @@ -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; @@ -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); diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/handler/PowermaxBridgeHandler.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/handler/PowermaxBridgeHandler.java index d12c597547c3f..cf5b0d7adda88 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/handler/PowermaxBridgeHandler.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/handler/PowermaxBridgeHandler.java @@ -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(); } @@ -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")) { @@ -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."; @@ -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); @@ -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"; } diff --git a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/message/PowermaxCommManager.java b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/message/PowermaxCommManager.java index f0c25ecb4b5e7..1bc3dbe95f915 100644 --- a/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/message/PowermaxCommManager.java +++ b/bundles/org.openhab.binding.powermax/src/main/java/org/openhab/binding/powermax/internal/message/PowermaxCommManager.java @@ -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; @@ -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; } @@ -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; }