[hpprinter] Prevent "handler disposed" warnings on shutdown (#10549)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
This commit is contained in:
Andrew Fiddian-Green 2021-04-25 10:19:39 +01:00 committed by GitHub
parent 10f88c0934
commit ea2721f277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View File

@ -75,6 +75,8 @@ public class HPPrinterBinder {
private @Nullable ScheduledFuture<?> usageScheduler; private @Nullable ScheduledFuture<?> usageScheduler;
private @Nullable ScheduledFuture<?> offlineScheduler; private @Nullable ScheduledFuture<?> offlineScheduler;
private boolean handlerDisposed;
/** /**
* Creates a new HP Printer Binder object * Creates a new HP Printer Binder object
* *
@ -95,6 +97,7 @@ public class HPPrinterBinder {
throw new IllegalStateException("ip-address should have been validated already and may not be empty."); throw new IllegalStateException("ip-address should have been validated already and may not be empty.");
} }
printerClient = new HPWebServerClient(httpClient, ipAddress); printerClient = new HPWebServerClient(httpClient, ipAddress);
handlerDisposed = false;
} }
public void retrieveProperties() { public void retrieveProperties() {
@ -107,7 +110,7 @@ public class HPPrinterBinder {
public synchronized void channelsChanged() { public synchronized void channelsChanged() {
logger.trace("Channels have been changed"); logger.trace("Channels have been changed");
close(); closeInternal();
open(); open();
} }
@ -548,9 +551,20 @@ public class HPPrinterBinder {
} }
/** /**
* Close the connection to the Embedded Web Server * Public method to close the connection to the Embedded Web Server
*
* Set handlerDisposed to prevent call-backs to the handler after it has been disposed
* Then call the closeinternal() method
*/ */
public void close() { public void close() {
handlerDisposed = true;
closeInternal();
}
/**
* Private (internal) method to close the connection to the Embedded Web Server
*/
private void closeInternal() {
stopBackgroundSchedules(); stopBackgroundSchedules();
final ScheduledFuture<?> localOfflineScheduler = offlineScheduler; final ScheduledFuture<?> localOfflineScheduler = offlineScheduler;
@ -564,10 +578,10 @@ public class HPPrinterBinder {
/** /**
* The device has gone offline * The device has gone offline
*/ */
public void goneOffline() { private void goneOffline() {
handler.updateStatus(ThingStatus.OFFLINE); handler.updateStatus(ThingStatus.OFFLINE);
close(); closeInternal();
runOfflineScheduler(); runOfflineScheduler();
} }
@ -615,6 +629,9 @@ public class HPPrinterBinder {
private void checkScannerStatus() { private void checkScannerStatus() {
HPServerResult<HPScannerStatus> result = printerClient.getScannerStatus(); HPServerResult<HPScannerStatus> result = printerClient.getScannerStatus();
if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) { if (result.getStatus() == RequestStatus.SUCCESS) {
handler.updateState(CGROUP_STATUS, CHANNEL_SCANNER_STATUS, handler.updateState(CGROUP_STATUS, CHANNEL_SCANNER_STATUS,
new StringType(result.getData().getScannerStatus())); new StringType(result.getData().getScannerStatus()));
@ -628,6 +645,9 @@ public class HPPrinterBinder {
private void checkStatus() { private void checkStatus() {
HPServerResult<HPStatus> result = printerClient.getStatus(); HPServerResult<HPStatus> result = printerClient.getStatus();
if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) { if (result.getStatus() == RequestStatus.SUCCESS) {
handler.updateState(CGROUP_STATUS, CHANNEL_STATUS, new StringType(result.getData().getPrinterStatus())); handler.updateState(CGROUP_STATUS, CHANNEL_STATUS, new StringType(result.getData().getPrinterStatus()));
handler.updateState(CGROUP_STATUS, CHANNEL_TRAYEMPTYOROPEN, handler.updateState(CGROUP_STATUS, CHANNEL_TRAYEMPTYOROPEN,
@ -648,6 +668,9 @@ public class HPPrinterBinder {
private void checkUsage() { private void checkUsage() {
HPServerResult<HPUsage> result = printerClient.getUsage(); HPServerResult<HPUsage> result = printerClient.getUsage();
if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) { if (result.getStatus() == RequestStatus.SUCCESS) {
// Inks // Inks
handler.updateState(CGROUP_INK, CHANNEL_BLACK_LEVEL, handler.updateState(CGROUP_INK, CHANNEL_BLACK_LEVEL,
@ -774,6 +797,9 @@ public class HPPrinterBinder {
private void checkOnline() { private void checkOnline() {
HPServerResult<HPStatus> result = printerClient.getStatus(); HPServerResult<HPStatus> result = printerClient.getStatus();
if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) { if (result.getStatus() == RequestStatus.SUCCESS) {
goneOnline(); goneOnline();
} else if (result.getStatus() == RequestStatus.TIMEOUT) { } else if (result.getStatus() == RequestStatus.TIMEOUT) {

View File

@ -12,7 +12,7 @@
*/ */
package org.openhab.binding.hpprinter.internal; package org.openhab.binding.hpprinter.internal;
import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.*; import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.CGROUP_STATUS;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -65,6 +65,13 @@ public class HPPrinterHandler extends BaseThingHandler {
@Override @Override
public void initialize() { public void initialize() {
scheduler.submit(() -> initializeScheduled());
}
/**
* Scheduled initialization task which will be executed on a separate thread
*/
private void initializeScheduled() {
final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class); final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class);
if (!"".equals(config.ipAddress)) { if (!"".equals(config.ipAddress)) {
@ -96,6 +103,7 @@ public class HPPrinterHandler extends BaseThingHandler {
return false; return false;
} }
@Override
protected void updateStatus(final ThingStatus status) { protected void updateStatus(final ThingStatus status) {
super.updateStatus(status); super.updateStatus(status);
} }
@ -116,6 +124,7 @@ public class HPPrinterHandler extends BaseThingHandler {
} }
} }
@Override
protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail, protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
@Nullable final String message) { @Nullable final String message) {
super.updateStatus(status, thingStatusDetail, message); super.updateStatus(status, thingStatusDetail, message);
@ -135,14 +144,17 @@ public class HPPrinterHandler extends BaseThingHandler {
updateThing(editThing().withChannels(thingChannels).build()); updateThing(editThing().withChannels(thingChannels).build());
} }
@Override
protected ThingBuilder editThing() { protected ThingBuilder editThing() {
return super.editThing(); return super.editThing();
} }
@Override
protected @Nullable ThingHandlerCallback getCallback() { protected @Nullable ThingHandlerCallback getCallback() {
return super.getCallback(); return super.getCallback();
} }
@Override
protected void updateProperties(final Map<String, String> properties) { protected void updateProperties(final Map<String, String> properties) {
super.updateProperties(properties); super.updateProperties(properties);
} }