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.
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() {
public synchronized void channelsChanged() {
logger.trace("Channels have been changed");
- close();
+ closeInternal();
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;
/**
* The device has gone offline
*/
- public void goneOffline() {
+ private void goneOffline() {
handler.updateStatus(ThingStatus.OFFLINE);
- close();
+ closeInternal();
runOfflineScheduler();
}
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()));
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,
private void checkUsage() {
HPServerResult<HPUsage> result = printerClient.getUsage();
+ if (handlerDisposed) {
+ return;
+ }
if (result.getStatus() == RequestStatus.SUCCESS) {
// Inks
handler.updateState(CGROUP_INK, CHANNEL_BLACK_LEVEL,
private void checkOnline() {
HPServerResult<HPStatus> result = printerClient.getStatus();
+ if (handlerDisposed) {
+ return;
+ }
if (result.getStatus() == RequestStatus.SUCCESS) {
goneOnline();
} else if (result.getStatus() == RequestStatus.TIMEOUT) {
*/
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;
@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)) {
return false;
}
+ @Override
protected void updateStatus(final ThingStatus status) {
super.updateStatus(status);
}
}
}
+ @Override
protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
@Nullable final String message) {
super.updateStatus(status, thingStatusDetail, message);
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);
}