Skip to content

Commit

Permalink
[hpprinter] Prevent "handler disposed" warnings on shutdown (openhab#…
Browse files Browse the repository at this point in the history
…10549)

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
Signed-off-by: John Marshall <john.marshall.au@gmail.com>
  • Loading branch information
andrewfg authored and themillhousegroup committed May 10, 2021
1 parent c7fda7d commit 45ce974
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ public class HPPrinterBinder {
private @Nullable ScheduledFuture<?> usageScheduler;
private @Nullable ScheduledFuture<?> offlineScheduler;

private boolean handlerDisposed;

/**
* Creates a new HP Printer Binder object
*
*
* @param handler {HPPrinterBinderEvent} The Event handler for the binder.
* @param httpClient {HttpClient} The HttpClient object to use to perform HTTP
* requests.
Expand All @@ -95,6 +97,7 @@ public HPPrinterBinder(HPPrinterHandler handler, HttpClient httpClient, Schedule
throw new IllegalStateException("ip-address should have been validated already and may not be empty.");
}
printerClient = new HPWebServerClient(httpClient, ipAddress);
handlerDisposed = false;
}

public void retrieveProperties() {
Expand All @@ -107,7 +110,7 @@ public void retrieveProperties() {

public synchronized void channelsChanged() {
logger.trace("Channels have been changed");
close();
closeInternal();
open();
}

Expand Down Expand Up @@ -548,9 +551,20 @@ public void open() {
}

/**
* 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() {
handlerDisposed = true;
closeInternal();
}

/**
* Private (internal) method to close the connection to the Embedded Web Server
*/
private void closeInternal() {
stopBackgroundSchedules();

final ScheduledFuture<?> localOfflineScheduler = offlineScheduler;
Expand All @@ -564,10 +578,10 @@ public void close() {
/**
* The device has gone offline
*/
public void goneOffline() {
private void goneOffline() {
handler.updateStatus(ThingStatus.OFFLINE);

close();
closeInternal();
runOfflineScheduler();
}

Expand Down Expand Up @@ -615,6 +629,9 @@ private synchronized void startBackgroundSchedules() {
private void checkScannerStatus() {
HPServerResult<HPScannerStatus> result = printerClient.getScannerStatus();

if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) {
handler.updateState(CGROUP_STATUS, CHANNEL_SCANNER_STATUS,
new StringType(result.getData().getScannerStatus()));
Expand All @@ -628,6 +645,9 @@ private void checkScannerStatus() {
private void checkStatus() {
HPServerResult<HPStatus> result = printerClient.getStatus();

if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) {
handler.updateState(CGROUP_STATUS, CHANNEL_STATUS, new StringType(result.getData().getPrinterStatus()));
handler.updateState(CGROUP_STATUS, CHANNEL_TRAYEMPTYOROPEN,
Expand All @@ -648,6 +668,9 @@ private static State convertToOnOffType(Boolean bool) {
private void checkUsage() {
HPServerResult<HPUsage> result = printerClient.getUsage();

if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) {
// Inks
handler.updateState(CGROUP_INK, CHANNEL_BLACK_LEVEL,
Expand Down Expand Up @@ -774,6 +797,9 @@ private void goneOnline() {
private void checkOnline() {
HPServerResult<HPStatus> result = printerClient.getStatus();

if (handlerDisposed) {
return;
}
if (result.getStatus() == RequestStatus.SUCCESS) {
goneOnline();
} else if (result.getStatus() == RequestStatus.TIMEOUT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
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.List;
Expand Down Expand Up @@ -65,6 +65,13 @@ public void handleCommand(final ChannelUID channelUID, final Command command) {

@Override
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);

if (!"".equals(config.ipAddress)) {
Expand Down Expand Up @@ -96,6 +103,7 @@ protected Boolean areStatusChannelsLinked(final String[] channels) {
return false;
}

@Override
protected void updateStatus(final ThingStatus status) {
super.updateStatus(status);
}
Expand All @@ -116,6 +124,7 @@ public void channelUnlinked(ChannelUID channelUID) {
}
}

@Override
protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
@Nullable final String message) {
super.updateStatus(status, thingStatusDetail, message);
Expand All @@ -135,14 +144,17 @@ public void binderAddChannels(final List<Channel> channels) {
updateThing(editThing().withChannels(thingChannels).build());
}

@Override
protected ThingBuilder editThing() {
return super.editThing();
}

@Override
protected @Nullable ThingHandlerCallback getCallback() {
return super.getCallback();
}

@Override
protected void updateProperties(final Map<String, String> properties) {
super.updateProperties(properties);
}
Expand Down

0 comments on commit 45ce974

Please sign in to comment.