[astro] Trigger or not jobs restart when a positional channel is linked or unlinked (#9104)

Don't rely on calls to channelLinked at startup

Related to openhab/openhab-core#1707

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2020-11-23 19:17:18 +01:00 committed by GitHub
parent 9de9955c49
commit ba98ef30e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,7 +82,7 @@ public abstract class AstroThingHandler extends BaseThingHandler {
private final Set<ScheduledFuture<?>> scheduledFutures = new HashSet<>(); private final Set<ScheduledFuture<?>> scheduledFutures = new HashSet<>();
private int linkedPositionalChannels = 0; private boolean linkedPositionalChannels;
protected AstroThingConfig thingConfig = new AstroThingConfig(); protected AstroThingConfig thingConfig = new AstroThingConfig();
@ -204,7 +204,8 @@ public abstract class AstroThingHandler extends BaseThingHandler {
// Repeat positional job every configured seconds // Repeat positional job every configured seconds
// Use scheduleAtFixedRate to avoid time drift associated with scheduleWithFixedDelay // Use scheduleAtFixedRate to avoid time drift associated with scheduleWithFixedDelay
if (isPositionalChannelLinked()) { linkedPositionalChannels = isPositionalChannelLinked();
if (linkedPositionalChannels) {
Job positionalJob = new PositionalJob(thingUID); Job positionalJob = new PositionalJob(thingUID);
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(positionalJob, 0, thingConfig.interval, ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(positionalJob, 0, thingConfig.interval,
TimeUnit.SECONDS); TimeUnit.SECONDS);
@ -244,23 +245,23 @@ public abstract class AstroThingHandler extends BaseThingHandler {
@Override @Override
public void channelLinked(ChannelUID channelUID) { public void channelLinked(ChannelUID channelUID) {
linkedChannelChange(channelUID, 1); linkedChannelChange(channelUID);
publishChannelIfLinked(channelUID); publishChannelIfLinked(channelUID);
} }
@Override @Override
public void channelUnlinked(ChannelUID channelUID) { public void channelUnlinked(ChannelUID channelUID) {
linkedChannelChange(channelUID, -1); linkedChannelChange(channelUID);
} }
/** /**
* Counts positional channels and restarts Astro jobs. * Counts positional channels and restarts Astro jobs.
*/ */
private void linkedChannelChange(ChannelUID channelUID, int step) { private void linkedChannelChange(ChannelUID channelUID) {
if (Arrays.asList(getPositionalChannelIds()).contains(channelUID.getId())) { if (Arrays.asList(getPositionalChannelIds()).contains(channelUID.getId())) {
int oldValue = linkedPositionalChannels; boolean oldValue = linkedPositionalChannels;
linkedPositionalChannels += step; linkedPositionalChannels = isPositionalChannelLinked();
if (oldValue == 0 && linkedPositionalChannels > 0 || oldValue > 0 && linkedPositionalChannels == 0) { if (oldValue != linkedPositionalChannels) {
restartJobs(); restartJobs();
} }
} }