Skip to content

Commit

Permalink
Fixed lauch of PositionalJob when restarting (openhab#1356)
Browse files Browse the repository at this point in the history
Signed-off-by: Gerhard Riegler <[email protected]>
  • Loading branch information
Gerhard R authored and kaikreuzer committed Oct 26, 2016
1 parent c2d4e2d commit 7c72105
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private void publishChannelIfLinked(ChannelUID channelUID) {
* already scheduled jobs first.
*/
private void restartJobs() {
logger.debug("restartJobs");
logger.debug("Restarting jobs for thing {}", getThing().getUID());

if (schedulerFuture != null && !schedulerFuture.isCancelled()) {
schedulerFuture.cancel(true);
Expand Down Expand Up @@ -202,7 +202,7 @@ public void run() {
.usingJobData(jobDataMap).build();
quartzScheduler.scheduleJob(jobDetail, trigger);

if (linkedPositionalChannels > 0) {
if (isPositionalChannelLinked()) {
// positional intervalJob
jobIdentity = PositionalJob.class.getSimpleName();
Date start = new Date(System.currentTimeMillis() + (thingConfig.getInterval()) * 1000);
Expand All @@ -225,8 +225,11 @@ public void run() {
}, 2000, TimeUnit.MILLISECONDS);
}

/**
* Stops all jobs for this thing.
*/
private void stopJobs() {
logger.debug("stopJobs");
logger.debug("Stopping jobs for thing {}", getThing().getUID());
synchronized (schedulerLock) {
if (quartzScheduler != null) {
try {
Expand All @@ -242,12 +245,18 @@ private void stopJobs() {
}
}

/**
* {@inheritDoc}
*/
@Override
public void channelLinked(ChannelUID channelUID) {
linkedChannelChange(channelUID, 1);
publishChannelIfLinked(channelUID);
}

/**
* {@inheritDoc}
*/
@Override
public void channelUnlinked(ChannelUID channelUID) {
linkedChannelChange(channelUID, -1);
Expand All @@ -257,7 +266,6 @@ public void channelUnlinked(ChannelUID channelUID) {
* Counts positional channels and restarts astro jobs.
*/
private void linkedChannelChange(ChannelUID channelUID, int step) {
logger.debug("linkedChannelChange {} step {}", channelUID, step);
if (ArrayUtils.contains(getPositionalChannelIds(), channelUID.getId())) {
int oldValue = linkedPositionalChannels;
linkedPositionalChannels += step;
Expand All @@ -267,6 +275,19 @@ private void linkedChannelChange(ChannelUID channelUID, int step) {
}
}

/**
* Returns true, if at least one positional channel is linked.
*/
private boolean isPositionalChannelLinked() {
for (Channel channel : getThing().getChannels()) {
if (ArrayUtils.contains(getPositionalChannelIds(), channel.getUID().getId())
&& isLinked(channel.getUID().getId())) {
return true;
}
}
return false;
}

/**
* Calculates and publishes the daily astro data.
*/
Expand Down

0 comments on commit 7c72105

Please sign in to comment.