Skip to content

Commit

Permalink
[rotel] Thread naming (openhab#8234)
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 markus7017 committed Sep 18, 2020
1 parent 7403d15 commit c4accfd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,6 @@ public class RotelBindingConstants {

// List of all properties
public static final String PROPERTY_PROTOCOL = "protocol";

public static final String THREAD_NAME_PREFIX = "OH-" + BINDING_ID + "-";
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public abstract class RotelConnector {
/** true if the connection is established, false if not */
private boolean connected;

protected String readerThreadName;
private @Nullable Thread readerThread;

private List<RotelMessageEventListener> listeners = new ArrayList<>();
Expand Down Expand Up @@ -221,13 +222,15 @@ public abstract class RotelConnector {
* @param model the Rotel model in use
* @param protocol the protocol to be used
* @param simu whether the communication is simulated or real
* @param readerThreadName the name of thread to be created
*/
public RotelConnector(RotelModel model, RotelProtocol protocol, Map<RotelSource, String> sourcesLabels,
boolean simu) {
boolean simu, String readerThreadName) {
this.model = model;
this.protocol = protocol;
this.sourcesLabels = sourcesLabels;
this.simu = simu;
this.readerThreadName = readerThreadName;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public class RotelIpConnector extends RotelConnector {
* @param port the TCP port to be used
* @param model the projector model in use
* @param protocol the protocol to be used
* @param readerThreadName the name of thread to be created
*/
public RotelIpConnector(String address, Integer port, RotelModel model, RotelProtocol protocol,
Map<RotelSource, String> sourcesLabels) {
super(model, protocol, sourcesLabels, false);
Map<RotelSource, String> sourcesLabels, String readerThreadName) {
super(model, protocol, sourcesLabels, false, readerThreadName);

this.address = address;
this.port = port;
Expand All @@ -69,7 +70,7 @@ public synchronized void open() throws RotelException {
dataOut = new DataOutputStream(clientSocket.getOutputStream());
dataIn = new DataInputStream(clientSocket.getInputStream());

Thread thread = new RotelReaderThread(this);
Thread thread = new RotelReaderThread(this, readerThreadName);
setReaderThread(thread);
thread.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class RotelReaderThread extends Thread {
*
* @param connector the object that should handle the received message
*/
public RotelReaderThread(RotelConnector connector) {
public RotelReaderThread(RotelConnector connector, String threadName) {
super(threadName);
this.connector = connector;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public class RotelSerialConnector extends RotelConnector {
* @param serialPortName the serial port name to be used
* @param model the projector model in use
* @param protocol the protocol to be used
* @param readerThreadName the name of thread to be created
*/
public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, RotelModel model,
RotelProtocol protocol, Map<RotelSource, String> sourcesLabels) {
super(model, protocol, sourcesLabels, false);
RotelProtocol protocol, Map<RotelSource, String> sourcesLabels, String readerThreadName) {
super(model, protocol, sourcesLabels, false, readerThreadName);

this.serialPortManager = serialPortManager;
this.serialPortName = serialPortName;
Expand Down Expand Up @@ -93,7 +94,7 @@ public synchronized void open() throws RotelException {
}
}

Thread thread = new RotelReaderThread(this);
Thread thread = new RotelReaderThread(this, readerThreadName);
setReaderThread(thread);
thread.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public class RotelSimuConnector extends RotelConnector {
*
* @param model the projector model in use
* @param protocol the protocol to be used
* @param readerThreadName the name of thread to be created
*/
public RotelSimuConnector(RotelModel model, RotelProtocol protocol, Map<RotelSource, String> sourcesLabels) {
super(model, protocol, sourcesLabels, true);
public RotelSimuConnector(RotelModel model, RotelProtocol protocol, Map<RotelSource, String> sourcesLabels,
String readerThreadName) {
super(model, protocol, sourcesLabels, true, readerThreadName);
this.minVolume = 0;
this.maxVolume = model.hasVolumeControl() ? model.getVolumeMax() : 0;
this.maxToneLevel = model.hasToneControl() ? model.getToneLevelMax() : 0;
Expand All @@ -92,7 +94,7 @@ public RotelSimuConnector(RotelModel model, RotelProtocol protocol, Map<RotelSou
@Override
public synchronized void open() throws RotelException {
logger.debug("Opening simulated connection");
Thread thread = new RotelReaderThread(this);
Thread thread = new RotelReaderThread(this, readerThreadName);
setReaderThread(thread);
thread.start();
setConnected(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public class RotelHandler extends BaseThingHandler implements RotelMessageEventL
private RotelStateDescriptionOptionProvider stateDescriptionProvider;
private SerialPortManager serialPortManager;

private RotelConnector connector = new RotelSimuConnector(DEFAULT_MODEL, RotelProtocol.HEX, new HashMap<>());
private RotelConnector connector = new RotelSimuConnector(DEFAULT_MODEL, RotelProtocol.HEX, new HashMap<>(),
THREAD_NAME_PREFIX);

private int minVolume;
private int maxVolume;
Expand Down Expand Up @@ -291,7 +292,9 @@ public void initialize() {

Map<RotelSource, String> sourcesLabels = new HashMap<>();

connector = new RotelSimuConnector(rotelModel, rotelProtocol, sourcesLabels);
String readerThreadName = THREAD_NAME_PREFIX + getThing().getUID().getAsString();

connector = new RotelSimuConnector(rotelModel, rotelProtocol, sourcesLabels, readerThreadName);

if (rotelModel.hasVolumeControl()) {
maxVolume = rotelModel.getVolumeMax();
Expand Down Expand Up @@ -376,12 +379,13 @@ public void initialize() {
}

if (USE_SIMULATED_DEVICE) {
connector = new RotelSimuConnector(rotelModel, rotelProtocol, sourcesLabels);
connector = new RotelSimuConnector(rotelModel, rotelProtocol, sourcesLabels, readerThreadName);
} else if (config.serialPort != null) {
connector = new RotelSerialConnector(serialPortManager, config.serialPort, rotelModel, rotelProtocol,
sourcesLabels);
sourcesLabels, readerThreadName);
} else {
connector = new RotelIpConnector(config.host, config.port, rotelModel, rotelProtocol, sourcesLabels);
connector = new RotelIpConnector(config.host, config.port, rotelModel, rotelProtocol, sourcesLabels,
readerThreadName);
}

if (rotelModel.hasSourceControl()) {
Expand Down

0 comments on commit c4accfd

Please sign in to comment.