mirror of
https://github.com/danieldemus/openhab-addons
synced 2026-07-29 12:34:21 +02:00
[tuya] Avoid leaking polling jobs (#21040)
Signed-off-by: Mike Jagdis <mjagdis@eris-associates.co.uk>
This commit is contained in:
+58
-44
@@ -170,13 +170,15 @@ public class TuyaDeviceHandler extends BaseThingHandler implements DeviceInfoSub
|
|||||||
// re-measure on a function change.
|
// re-measure on a function change.
|
||||||
TuyaDevice tuyaDevice = this.tuyaDevice;
|
TuyaDevice tuyaDevice = this.tuyaDevice;
|
||||||
if (tuyaDevice != null) {
|
if (tuyaDevice != null) {
|
||||||
ScheduledFuture<?> pollingJob = this.pollingJob;
|
synchronized (this) {
|
||||||
if (pollingJob != null) {
|
ScheduledFuture<?> pollingJob = this.pollingJob;
|
||||||
pollingJob.cancel(true);
|
if (pollingJob != null) {
|
||||||
}
|
pollingJob.cancel(true);
|
||||||
|
}
|
||||||
|
|
||||||
pollBurst = 3;
|
pollBurst = 3;
|
||||||
this.pollingJob = scheduler.scheduleWithFixedDelay(this::burstPoller, 0, 1, TimeUnit.SECONDS);
|
this.pollingJob = scheduler.scheduleWithFixedDelay(this::burstPoller, 0, 1, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (!missingStatus) {
|
} else if (!missingStatus) {
|
||||||
// If we have updates for everything we can stand down the burst polling.
|
// If we have updates for everything we can stand down the burst polling.
|
||||||
@@ -193,17 +195,21 @@ public class TuyaDeviceHandler extends BaseThingHandler implements DeviceInfoSub
|
|||||||
tuyaDevice.refreshStatus();
|
tuyaDevice.refreshStatus();
|
||||||
pollBurst = pollBurst - 1;
|
pollBurst = pollBurst - 1;
|
||||||
} else {
|
} else {
|
||||||
ScheduledFuture<?> pollingJob = this.pollingJob;
|
synchronized (this) {
|
||||||
if (pollingJob != null) {
|
if (!Thread.interrupted()) {
|
||||||
this.pollingJob = null;
|
ScheduledFuture<?> pollingJob = this.pollingJob;
|
||||||
pollingJob.cancel(false);
|
if (pollingJob != null) {
|
||||||
}
|
this.pollingJob = null;
|
||||||
|
pollingJob.cancel(false);
|
||||||
|
}
|
||||||
|
|
||||||
int pollingInterval = configuration.pollingInterval;
|
int pollingInterval = configuration.pollingInterval;
|
||||||
if (pollingInterval > 0) {
|
if (pollingInterval > 0) {
|
||||||
this.pollingJob = scheduler.scheduleWithFixedDelay(() -> {
|
this.pollingJob = scheduler.scheduleWithFixedDelay(() -> {
|
||||||
tuyaDevice.refreshStatus();
|
tuyaDevice.refreshStatus();
|
||||||
}, pollingInterval, pollingInterval, TimeUnit.SECONDS);
|
}, pollingInterval, pollingInterval, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,27 +331,31 @@ public class TuyaDeviceHandler extends BaseThingHandler implements DeviceInfoSub
|
|||||||
|
|
||||||
TuyaDevice tuyaDevice = this.tuyaDevice;
|
TuyaDevice tuyaDevice = this.tuyaDevice;
|
||||||
if (tuyaDevice != null) {
|
if (tuyaDevice != null) {
|
||||||
if (pollingJob == null) {
|
synchronized (this) {
|
||||||
// When we first connect the device state is unknown so we want to query for everything.
|
if (pollingJob == null) {
|
||||||
// Some devices seem to initialize their stacks in the wrong order and
|
// When we first connect the device state is unknown so we want to query for everything.
|
||||||
// requests that come too soon can be either ignored completely or responded
|
// Some devices seem to initialize their stacks in the wrong order and
|
||||||
// to with a, "not supported". The lower level protocol handler suggests an
|
// requests that come too soon can be either ignored completely or responded
|
||||||
// initial delay where it might be advisable.
|
// to with a, "not supported". The lower level protocol handler suggests an
|
||||||
this.pollingJob = scheduler.schedule(() -> {
|
// initial delay where it might be advisable.
|
||||||
tuyaDevice.requestStatus();
|
pollingJob = scheduler.schedule(() -> {
|
||||||
|
tuyaDevice.requestStatus();
|
||||||
|
|
||||||
// After that we poll for the measurable DPs.
|
// After that we poll for the measurable DPs.
|
||||||
int pollingInterval = configuration.pollingInterval;
|
int pollingInterval = configuration.pollingInterval;
|
||||||
if (pollingInterval > 0) {
|
if (pollingInterval > 0) {
|
||||||
// The first refresh request is immediate because we do not know how old
|
synchronized (this) {
|
||||||
// the current status might be.
|
// The first refresh request is immediate because we do not know how old
|
||||||
pollingJob = scheduler.scheduleWithFixedDelay(() -> {
|
// the current status might be.
|
||||||
tuyaDevice.refreshStatus();
|
pollingJob = scheduler.scheduleWithFixedDelay(() -> {
|
||||||
}, 0, pollingInterval, TimeUnit.SECONDS);
|
tuyaDevice.refreshStatus();
|
||||||
}
|
}, 0, pollingInterval, TimeUnit.SECONDS);
|
||||||
}, initialDelay, TimeUnit.MILLISECONDS);
|
}
|
||||||
} else {
|
}
|
||||||
logger.debug("{}: polling job already exists?!?", thing.getUID().getId());
|
}, initialDelay, TimeUnit.MILLISECONDS);
|
||||||
|
} else {
|
||||||
|
logger.debug("{}: polling job already exists?!?", thing.getUID().getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,10 +368,12 @@ public class TuyaDeviceHandler extends BaseThingHandler implements DeviceInfoSub
|
|||||||
|
|
||||||
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "@text/online.wait-for-device");
|
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "@text/online.wait-for-device");
|
||||||
|
|
||||||
ScheduledFuture<?> pollingJob = this.pollingJob;
|
synchronized (this) {
|
||||||
if (pollingJob != null) {
|
ScheduledFuture<?> pollingJob = this.pollingJob;
|
||||||
pollingJob.cancel(true);
|
if (pollingJob != null) {
|
||||||
this.pollingJob = null;
|
this.pollingJob = null;
|
||||||
|
pollingJob.cancel(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (channelIdToChannelTypeUID.containsValue(CHANNEL_TYPE_UID_IR_CODE)) {
|
if (channelIdToChannelTypeUID.containsValue(CHANNEL_TYPE_UID_IR_CODE)) {
|
||||||
@@ -534,10 +546,12 @@ public class TuyaDeviceHandler extends BaseThingHandler implements DeviceInfoSub
|
|||||||
public void dispose() {
|
public void dispose() {
|
||||||
logger.debug("{}: dispose", thing.getUID().getId());
|
logger.debug("{}: dispose", thing.getUID().getId());
|
||||||
|
|
||||||
ScheduledFuture<?> future = this.pollingJob;
|
synchronized (this) {
|
||||||
if (future != null) {
|
ScheduledFuture<?> pollingJob = this.pollingJob;
|
||||||
this.pollingJob = null;
|
if (pollingJob != null) {
|
||||||
future.cancel(true);
|
this.pollingJob = null;
|
||||||
|
pollingJob.cancel(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
udpDiscoveryListener.unregisterListener(this);
|
udpDiscoveryListener.unregisterListener(this);
|
||||||
|
|||||||
Reference in New Issue
Block a user