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