Skip to content

Commit

Permalink
[automation] Added EphemerisConditions for NGRE (openhab#915)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and kaikreuzer committed Oct 19, 2019
1 parent 36ca073 commit a3d91be
Show file tree
Hide file tree
Showing 18 changed files with 516 additions and 235 deletions.
5 changes: 5 additions & 0 deletions bundles/org.openhab.core.automation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>org.openhab.core.thing</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.ephemeris</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Module;
Expand All @@ -34,7 +33,7 @@
@NonNullByDefault
public abstract class BaseModuleHandlerFactory implements ModuleHandlerFactory {

private final Map<@NonNull String, @NonNull ModuleHandler> handlers = new HashMap<>();
private final Map<String, ModuleHandler> handlers = new HashMap<>();

/**
* Should be overridden by the implementations that extend this base class. Called from DS to deactivate the
Expand Down Expand Up @@ -73,7 +72,7 @@ protected Map<String, ModuleHandler> getHandlers() {
/**
* Creates a new {@link ModuleHandler} for a given {@code module} and {@code ruleUID}.
*
* @param module the {@link Module} for which a handler should be created.
* @param module the {@link Module} for which a handler should be created.
* @param ruleUID the identifier of the {@link Rule} that the given module belongs to.
* @return a {@link ModuleHandler} instance or {@code null} if thins module type is not supported.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.internal.module.factory;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.ephemeris.EphemerisManager;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.Module;
import org.openhab.core.automation.handler.BaseModuleHandlerFactory;
import org.openhab.core.automation.handler.ModuleHandler;
import org.openhab.core.automation.handler.ModuleHandlerFactory;
import org.openhab.core.automation.internal.module.handler.EphemerisConditionHandler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This HandlerFactory creates ModuleHandlers to control items within the RuleManager. It contains basic Ephemeris
* Conditions.
*
* @author Christoph Weitkamp - Initial contribution
*/
@Component
@NonNullByDefault
public class EphemerisModuleHandlerFactory extends BaseModuleHandlerFactory implements ModuleHandlerFactory {

private final Logger logger = LoggerFactory.getLogger(EphemerisModuleHandlerFactory.class);

private static final Collection<String> TYPES = Collections.unmodifiableList(Stream
.of(EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID, EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID,
EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID, EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID)
.collect(Collectors.toList()));

private final EphemerisManager ephemerisManager;

@Activate
public EphemerisModuleHandlerFactory(final @Reference EphemerisManager ephemerisManager) {
this.ephemerisManager = ephemerisManager;
}

@Override
@Deactivate
protected void deactivate() {
super.deactivate();
}

@Override
public Collection<String> getTypes() {
return TYPES;
}

@Override
protected @Nullable ModuleHandler internalCreate(final Module module, final String ruleUID) {
final String moduleTypeUID = module.getTypeUID();
logger.trace("create {} -> {} : {}", module.getId(), moduleTypeUID, ruleUID);

if (module instanceof Condition) {
switch (moduleTypeUID) {
case EphemerisConditionHandler.HOLIDAY_MODULE_TYPE_ID:
case EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID:
case EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID:
case EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID:
return new EphemerisConditionHandler((Condition) module, ephemerisManager);
}
}

logger.error("The ModuleHandler is not supported: {}", moduleTypeUID);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public boolean isSatisfied(Map<String, Object> context) {
case "GT":
case ">":
// Greater
if (toCompare == null || rightValue == null) {
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) > 0;
Expand All @@ -103,15 +103,15 @@ public boolean isSatisfied(Map<String, Object> context) {
case ">=":
case "=>":
// Greater or equal
if (toCompare == null || rightValue == null) {
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) >= 0;
}
case "lt":
case "LT":
case "<":
if (toCompare == null || rightValue == null) {
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) < 0;
Expand All @@ -120,14 +120,14 @@ public boolean isSatisfied(Map<String, Object> context) {
case "LTE":
case "<=":
case "=<":
if (toCompare == null || rightValue == null) {
if (toCompare == null) {
return false;
} else {
return compare(toCompare, rightValue) <= 0;
}
case "matches":
// Matcher...
if (toCompare instanceof String && rightValue != null && rightValue instanceof String) {
if (toCompare instanceof String && rightValue instanceof String) {
return ((String) toCompare).matches((String) rightValue);
}
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.internal.module.handler;

import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.Configuration;
import org.eclipse.smarthome.core.ephemeris.EphemerisManager;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.handler.BaseModuleHandler;
import org.openhab.core.automation.handler.ConditionHandler;

/**
* ConditionHandler implementation for Ephemeris based conditions.
*
* @author Christoph Weitkamp - Initial contribution
*/
@NonNullByDefault
public class EphemerisConditionHandler extends BaseModuleHandler<Condition> implements ConditionHandler {

public static final String HOLIDAY_MODULE_TYPE_ID = "ephemeris.HolidayCondition";
public static final String WEEKEND_MODULE_TYPE_ID = "ephemeris.WeekendCondition";
public static final String WEEKDAY_MODULE_TYPE_ID = "ephemeris.WeekdayCondition";
public static final String DAYSET_MODULE_TYPE_ID = "ephemeris.DaysetCondition";

private static final String DAYSET = "dayset";
private static final String OFFSET = "offset";

private final EphemerisManager ephemerisManager;
private final @Nullable String dayset;
private final int offset;

public EphemerisConditionHandler(Condition condition, EphemerisManager ephemerisManager) {
super(condition);
this.ephemerisManager = ephemerisManager;

this.dayset = DAYSET_MODULE_TYPE_ID.equals(module.getTypeUID())
? getValidStringConfigParameter(DAYSET, module.getConfiguration(), module.getId())
: null;
this.offset = getValidIntegerConfigParameter(OFFSET, module.getConfiguration(), module.getId());
}

private static int getValidIntegerConfigParameter(String parameter, Configuration config, String moduleId) {
Object value = config.get(parameter);
if (value != null && value instanceof Integer) {
return (Integer) value;
} else {
throw new IllegalStateException(String.format(
"Config parameter '%s' is missing in the configuration of module '%s'.", parameter, moduleId));
}
}

private static String getValidStringConfigParameter(String parameter, Configuration config, String moduleId) {
Object value = config.get(parameter);
if (value != null && value instanceof String && !((String) value).trim().isEmpty()) {
return (String) value;
} else {
throw new IllegalStateException(String.format(
"Config parameter '%s' is missing in the configuration of module '%s'.", parameter, moduleId));
}
}

@Override
public boolean isSatisfied(Map<String, Object> inputs) {
switch (module.getTypeUID()) {
case HOLIDAY_MODULE_TYPE_ID:
return ephemerisManager.isBankHoliday(offset);
case WEEKEND_MODULE_TYPE_ID:
return ephemerisManager.isWeekend(offset);
case WEEKDAY_MODULE_TYPE_ID:
return !ephemerisManager.isWeekend(offset);
case DAYSET_MODULE_TYPE_ID:
final String dayset = this.dayset;
if (dayset != null) {
return ephemerisManager.isInDayset(dayset, offset);
}
break;
}
// If none of these conditions apply false is returned.
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"conditions":[
{
"uid":"timer.DayOfWeekCondition",
"label":"it is a certain day of the week",
"description":"checks for the current day of the week",
"configDescriptions":[
{
"name":"days",
"context":"dayOfWeek",
"type":"TEXT",
"label":"Days of the week",
"description":"the days of the week when the rule should be active",
"required":true,
"multiple":true
}
]
}
]
{
"conditions": [
{
"uid": "timer.DayOfWeekCondition",
"label": "it is a certain day of the week",
"description": "checks for the current day of the week",
"configDescriptions": [
{
"name": "days",
"context": "dayOfWeek",
"type": "TEXT",
"label": "Days of the week",
"description": "the days of the week when the rule should be active",
"required": true,
"multiple": true
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"conditions": [
{
"uid": "ephemeris.HolidayCondition",
"label": "It is a holiday",
"description": "Checks if the current day is a holiday.",
"configDescriptions": [
{
"name": "offset",
"type": "integer",
"label": "Offset",
"description": "Today +/- offset days (+1 = tomorrow, -1 = yesterday).",
"default": 0,
"step": 1,
"required": false
}
]
},
{
"uid": "ephemeris.WeekendCondition",
"label": "It is a weekend",
"description": "Checks if the current day is on the weekend.",
"configDescriptions": [
{
"name": "offset",
"type": "integer",
"label": "Offset",
"description": "Today +/- offset days (+1 = tomorrow, -1 = yesterday).",
"default": 0,
"step": 1,
"required": false
}
]
},
{
"uid": "ephemeris.WeekdayCondition",
"label": "It is a weekday",
"description": "Checks if the current day is not on the weekend.",
"configDescriptions": [
{
"name": "offset",
"type": "integer",
"label": "Offset",
"description": "Today +/- offset days (+1 = tomorrow, -1 = yesterday).",
"default": 0,
"step": 1,
"required": false
}
]
},
{
"uid": "ephemeris.DaysetCondition",
"label": "It is a day in a configured dayset",
"description": "Checks if the current day is in a configured dayset.",
"configDescriptions": [
{
"name": "dayset",
"type": "text",
"label": "Dayset",
"description": "Name of the requested dayset, without prefix.",
"required": true
},
{
"name": "offset",
"type": "integer",
"label": "Offset",
"description": "Today +/- offset days (+1 = tomorrow, -1 = yesterday).",
"default": 0,
"step": 1,
"required": false
}
]
}
]
}
Loading

0 comments on commit a3d91be

Please sign in to comment.