forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[automation] Added
EphemerisConditions
for NGRE (openhab#915)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
- Loading branch information
1 parent
36ca073
commit a3d91be
Showing
18 changed files
with
516 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...va/org/openhab/core/automation/internal/module/factory/EphemerisModuleHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...n/java/org/openhab/core/automation/internal/module/handler/EphemerisConditionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
38 changes: 19 additions & 19 deletions
38
...core.automation/src/main/resources/ESH-INF/automation/moduletypes/DayOfWeekCondition.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
} |
75 changes: 75 additions & 0 deletions
75
...ore.automation/src/main/resources/ESH-INF/automation/moduletypes/EphemerisConditions.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.