Skip to content

Commit

Permalink
Added rest endpoint for rule simulation.
Browse files Browse the repository at this point in the history
Signed-off-by: Sönke Küper <soenkekueper@gmx.de>
  • Loading branch information
soenkekueper committed Jan 17, 2021
1 parent 19a0e86 commit bcb2106
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.security.RolesAllowed;
Expand All @@ -47,6 +49,7 @@
import org.openhab.core.automation.ManagedRuleProvider;
import org.openhab.core.automation.Module;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleExecution;
import org.openhab.core.automation.RuleManager;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.Trigger;
Expand All @@ -70,6 +73,7 @@
import org.openhab.core.io.rest.RESTConstants;
import org.openhab.core.io.rest.RESTResource;
import org.openhab.core.io.rest.Stream2JSONInputStream;
import org.openhab.core.library.types.DateTimeType;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -339,6 +343,23 @@ public Response getTriggers(@PathParam("ruleUID") @Parameter(description = "rule
}
}

@GET
@Path("/simulation")
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getRuleSimulation", summary = "Simulates the executions for the rules within the schedule.", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = RuleExecution.class)))) })
public Response simulateRules(@Parameter(description = "from") String from,
@Parameter(description = "from") String until) {
final Stream<RuleExecution> ruleExecutions = ruleManager.simulateRuleExecutions(parseTime(from),
parseTime(until));
return Response.ok(ruleExecutions.collect(Collectors.toList())).build();
}

private static ZonedDateTime parseTime(String sTime) {
final DateTimeType dateTime = new DateTimeType(sTime);
return dateTime.getZonedDateTime();
}

@GET
@Path("/{ruleUID}/conditions")
@Produces(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit bcb2106

Please sign in to comment.