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

Added Composite Jobs for range events if scheduled at same instant #3752

Merged
merged 1 commit into from Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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