Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Added composite job for range events if scheduled at same time instant
Browse files Browse the repository at this point in the history
Also-by: Markus Rathgeb <maggu2810@gmail.com>
Signed-off-by: Amit Kumar Mondal <admin@amitinside.com>
  • Loading branch information
amitjoy committed Jun 30, 2017
1 parent 1c0be53 commit 9db1b0b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2014-2017 by the respective copyright holders.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.binding.astro.internal.job;

import java.util.List;

/**
* {@link CompositeJob} comprises multiple {@link Job}s to be executed in order
*
* @author Markus Rathgeb - Initial contribution
* @author Amit Kumar Mondal - Minor modifications
*/
public final class CompositeJob extends AbstractJob {

private final List<Job> jobs;

/**
* Constructor
*
* @param thingUID thing UID
* @param jobs the jobs to execute
* @throws IllegalArgumentException
* if {@code jobs} is {@code null} or empty
*/
public CompositeJob(String thingUID, List<Job> jobs) {
super(thingUID);
checkArgument(jobs != null, "Jobs must not be null");
checkArgument(!jobs.isEmpty(), "Jobs must not be empty");

this.jobs = jobs;

boolean notMatched = jobs.stream().anyMatch(j -> !j.getThingUID().equals(thingUID));
checkArgument(!notMatched, "The jobs must associate the same thing UID");
}

@Override
public void run() {
jobs.forEach(j -> {
try {
j.run();
} catch (RuntimeException ex) {
logger.warn("Job execution failed.", ex);
}
});
}
}
Expand Up @@ -7,12 +7,18 @@
*/
package org.eclipse.smarthome.binding.astro.internal.job;

import static java.util.Arrays.asList;
import static java.util.Calendar.SECOND;
import static java.util.Collections.singletonList;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang.time.DateUtils.truncatedEquals;
import static org.eclipse.smarthome.binding.astro.AstroBindingConstants.*;
import static org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils.*;

import java.lang.invoke.MethodHandles;
import java.util.Calendar;
import java.util.List;

import org.eclipse.smarthome.binding.astro.handler.AstroThingHandler;
import org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig;
Expand Down Expand Up @@ -65,20 +71,34 @@ public static void schedule(String thingUID, AstroThingHandler astroHandler, Job
*/
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt, String event,
String channelId) {
scheduleEvent(thingUID, astroHandler, eventAt, singletonList(event), channelId);
}

/**
* Schedules an {@link EventJob} instance
*
* @param thingUID the Thing UID
* @param astroHandler the {@link ThingHandler} instance
* @param eventAt the {@link Calendar} instance denoting scheduled instant
* @param events the event IDs to schedule
* @param channelId the channel ID
*/
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt,
List<String> events, String channelId) {
boolean thingNull = checkNull(thingUID, "Thing UID is null");
boolean astroHandlerNull = checkNull(astroHandler, "AstroThingHandler is null");
boolean eventAtNull = checkNull(eventAt, "Scheduled Instant is null");
boolean eventNull = checkNull(event, "Event is null");
boolean eventsNull = checkNull(events, "Events list is null");
boolean channelIdNull = checkNull(channelId, "Channel ID is null");

if (thingNull || astroHandlerNull || eventAtNull || eventNull || channelIdNull) {
if (thingNull || astroHandlerNull || eventAtNull || eventsNull || channelIdNull || events.isEmpty()) {
return;
}
AstroChannelConfig config = astroHandler.getThing().getChannel(channelId).getConfiguration()
.as(AstroChannelConfig.class);
Calendar instant = applyConfig(eventAt, config);
Job eventJob = new EventJob(thingUID, channelId, event);
schedule(thingUID, astroHandler, eventJob, instant);
List<Job> jobs = events.stream().map(e -> new EventJob(thingUID, channelId, e)).collect(toList());
schedule(thingUID, astroHandler, new CompositeJob(thingUID, jobs), instant);
}

/**
Expand All @@ -98,8 +118,15 @@ public static void scheduleRange(String thingUID, AstroThingHandler astroHandler
if (thingNull || astroHandlerNull || rangeNull || channelIdNull) {
return;
}
scheduleEvent(thingUID, astroHandler, range.getStart(), EVENT_START, channelId);
scheduleEvent(thingUID, astroHandler, range.getEnd(), EVENT_END, channelId);

Calendar start = range.getStart();
Calendar end = range.getEnd();
if (truncatedEquals(start, end, SECOND)) {
scheduleEvent(thingUID, astroHandler, start, asList(EVENT_START, EVENT_END), channelId);
} else {
scheduleEvent(thingUID, astroHandler, start, EVENT_START, channelId);
scheduleEvent(thingUID, astroHandler, end, EVENT_END, channelId);
}
}

/**
Expand Down

0 comments on commit 9db1b0b

Please sign in to comment.