Fix blocking initialization (#17057)

Resolves #16806

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-07-15 13:57:51 +02:00 committed by Ciprian Pascu
parent bc0bbaed4f
commit 246f247796

View File

@ -83,6 +83,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
private DenonMarantzConnectorFactory connectorFactory = new DenonMarantzConnectorFactory(); private DenonMarantzConnectorFactory connectorFactory = new DenonMarantzConnectorFactory();
private DenonMarantzState denonMarantzState; private DenonMarantzState denonMarantzState;
private @Nullable ScheduledFuture<?> retryJob; private @Nullable ScheduledFuture<?> retryJob;
private @Nullable ScheduledFuture<?> initJob;
public DenonMarantzHandler(Thing thing, HttpClient httpClient) { public DenonMarantzHandler(Thing thing, HttpClient httpClient) {
super(thing); super(thing);
@ -211,7 +212,9 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
* When not set we will try to auto-detect the correct values * When not set we will try to auto-detect the correct values
* for isTelnet and zoneCount and update the Thing accordingly. * for isTelnet and zoneCount and update the Thing accordingly.
*/ */
if (config.isTelnet() == null) { if (config.isTelnet() != null) {
return;
}
logger.debug("Trying to auto-detect the connection."); logger.debug("Trying to auto-detect the connection.");
ContentResponse response; ContentResponse response;
boolean telnetEnable = true; boolean telnetEnable = true;
@ -221,8 +224,8 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
// try to reach the HTTP API at port 80 (most models, except Denon ...H should respond. // try to reach the HTTP API at port 80 (most models, except Denon ...H should respond.
String host = config.getHost(); String host = config.getHost();
try { try {
response = httpClient.newRequest("http://" + host + "/goform/Deviceinfo.xml") response = httpClient.newRequest("http://" + host + "/goform/Deviceinfo.xml").timeout(3, TimeUnit.SECONDS)
.timeout(3, TimeUnit.SECONDS).send(); .send();
if (response.getStatus() == HttpURLConnection.HTTP_OK) { if (response.getStatus() == HttpURLConnection.HTTP_OK) {
logger.debug("We can access the HTTP API, disabling the Telnet mode by default."); logger.debug("We can access the HTTP API, disabling the Telnet mode by default.");
telnetEnable = false; telnetEnable = false;
@ -245,8 +248,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
httpApiUsable = true; httpApiUsable = true;
} }
} catch (TimeoutException | ExecutionException e) { } catch (TimeoutException | ExecutionException e) {
logger.debug( logger.debug("Additionally tried to connect to port 8080, this also failed. Reverting to Telnet mode.",
"Additionally tried to connect to port 8080, this also failed. Reverting to Telnet mode.",
e); e);
} }
} }
@ -301,12 +303,14 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
configuration.put(PARAMETER_ZONE_COUNT, zoneCount); configuration.put(PARAMETER_ZONE_COUNT, zoneCount);
updateConfiguration(configuration); updateConfiguration(configuration);
} }
}
@Override @Override
public void initialize() { public void initialize() {
config = getConfigAs(DenonMarantzConfiguration.class); config = getConfigAs(DenonMarantzConfiguration.class);
updateStatus(ThingStatus.UNKNOWN);
initJob = scheduler.schedule(() -> {
// Configure Connection type (Telnet/HTTP) and number of zones // Configure Connection type (Telnet/HTTP) and number of zones
// Note: this only happens for discovered Things // Note: this only happens for discovered Things
try { try {
@ -321,10 +325,10 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
} }
configureZoneChannels(); configureZoneChannels();
updateStatus(ThingStatus.UNKNOWN);
// create connection (either Telnet or HTTP) // create connection (either Telnet or HTTP)
// ThingStatus ONLINE/OFFLINE is set when AVR status is known. // ThingStatus ONLINE/OFFLINE is set when AVR status is known.
createConnection(); createConnection();
}, 0, TimeUnit.SECONDS);
} }
private void createConnection() { private void createConnection() {
@ -337,6 +341,14 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
connector.connect(); connector.connect();
} }
private void cancelInitJob() {
ScheduledFuture<?> initJob = this.initJob;
if (initJob != null) {
initJob.cancel(true);
}
this.initJob = null;
}
private void cancelRetry() { private void cancelRetry() {
ScheduledFuture<?> retryJob = this.retryJob; ScheduledFuture<?> retryJob = this.retryJob;
if (retryJob != null) { if (retryJob != null) {
@ -430,6 +442,7 @@ public class DenonMarantzHandler extends BaseThingHandler implements DenonMarant
} }
this.connector = null; this.connector = null;
cancelRetry(); cancelRetry();
cancelInitJob();
super.dispose(); super.dispose();
} }