use of org.openhab.core.automation.Trigger in project openhab-addons by openhab.
the class RuleUtils method createTriggerForTimeString.
/**
* Creates a trigger based on the given time string.
* According to <a href="https://developers.meethue.com/develop/hue-api/datatypes-and-time-patterns/">the Hue
* documentation</a> this can be:
* <p>
* <ul>
* <li>Absolute time [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss] ([date]T[time])
* <li>Randomized time [YYYY]:[MM]:[DD]T[hh]:[mm]:[ss]A[hh]:[mm]:[ss] ([date]T[time]A[time])
* <li>Recurring times W[bbb]/T[hh]:[mm]:[ss]
* <li>Every day of the week given by bbb at given time
* <li>Recurring randomized times W[bbb]/T[hh]:[mm]:[ss]A[hh]:[mm]:[ss]
* <li>Every weekday given by bbb at given left side time, randomized by right side time. Right side time has to be
* smaller than 12 hours
* <li>
* <ul>
* Timers
* <li>PT[hh]:[mm]:[ss] Timer, expiring after given time
* <li>PT[hh]:[mm]:[ss] Timer, expiring after given time
* <li>PT[hh]:[mm]:[ss]A[hh]:[mm]:[ss] Timer with random element
* <li>R[nn]/PT[hh]:[mm]:[ss] Recurring timer
* <li>R/PT[hh]:[mm]:[ss] Recurring timer
* <li>R[nn]/PT[hh]:[mm]:[ss]A[hh]:[mm]:[ss] Recurring timer with random element
* </ul>
* </ul>
*
* @param localtimeParameter An absolute time or recurring time or timer pattern
* @return A trigger based on {@link org.openhab.io.hueemulation.internal.automation.AbsoluteDateTimeTriggerHandler}
* or {@link org.openhab.io.hueemulation.internal.automation.TimerTriggerHandler}
*/
public static Trigger createTriggerForTimeString(final String localtimeParameter) throws IllegalStateException {
String localtime = localtimeParameter;
Trigger ruleTrigger;
// Normalize timer patterns
if (localtime.startsWith("PT")) {
localtime = "R1/" + localtime;
}
if (!localtime.contains("A")) {
localtime += "A";
}
// Recurring pattern "W[bbb]/T[hh]:[mm]:[ss]A[hh]:[mm]:[ss]"
if (localtime.startsWith("W")) {
ruleTrigger = createRecurringFromTimeString(localtime);
} else // Timer pattern: R[nn]/PT[hh]:[mm]:[ss]A[hh]:[mm]:[ss]
if (localtime.startsWith("R")) {
ruleTrigger = createTimerFromTimeString(localtime);
} else // Absolute date/time pattern "[YYYY]:[MM]:[DD]T[hh]:[mm]:[ss]A[hh]:[mm]:[ss]"
{
ruleTrigger = createAbsoluteDateTimeFromTimeString(localtime);
}
return ruleTrigger;
}
use of org.openhab.core.automation.Trigger in project openhab-core by openhab.
the class RuleEngineImpl method setTriggerOutputs.
/**
* The method updates {@link Output} of the {@link Trigger} with a new triggered data.
*
* @param td new Triggered data.
*/
private void setTriggerOutputs(String ruleUID, TriggerData td) {
Trigger t = td.getTrigger();
updateContext(ruleUID, t.getId(), td.getOutputs());
}
use of org.openhab.core.automation.Trigger in project openhab-core by openhab.
the class CompositeModuleHandlerFactory method internalCreate.
@Override
@Nullable
public ModuleHandler internalCreate(Module module, String ruleUID) {
ModuleHandler handler = null;
String moduleType = module.getTypeUID();
ModuleType mt = mtRegistry.get(moduleType);
if (mt instanceof CompositeTriggerType) {
List<Trigger> childModules = ((CompositeTriggerType) mt).getChildren();
LinkedHashMap<Trigger, @Nullable TriggerHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeTriggerHandler((Trigger) module, (CompositeTriggerType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeConditionType) {
List<Condition> childModules = ((CompositeConditionType) mt).getChildren();
LinkedHashMap<Condition, @Nullable ConditionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeConditionHandler((Condition) module, (CompositeConditionType) mt, mapModuleToHandler, ruleUID);
}
} else if (mt instanceof CompositeActionType) {
List<Action> childModules = ((CompositeActionType) mt).getChildren();
LinkedHashMap<Action, @Nullable ActionHandler> mapModuleToHandler = getChildHandlers(module.getId(), module.getConfiguration(), childModules, ruleUID);
if (mapModuleToHandler != null) {
handler = new CompositeActionHandler((Action) module, (CompositeActionType) mt, mapModuleToHandler, ruleUID);
}
}
if (handler != null) {
logger.debug("Set module handler: {} -> {} of rule {}.", module.getId(), handler.getClass().getSimpleName() + "(" + moduleType + ")", ruleUID);
} else {
logger.debug("Not found module handler {} for moduleType {} of rule {}.", module.getId(), moduleType, ruleUID);
}
return handler;
}
use of org.openhab.core.automation.Trigger in project openhab-core by openhab.
the class RuleSimulationTest method createRuleWithTimeOfDayTrigger.
/**
* creates a rule with an day of time trigger at 16:00, that has an side condition to fire only
* on Monday and Wednesday.
*/
private static Rule createRuleWithTimeOfDayTrigger() {
int rand = new Random().nextInt();
Map<String, Object> configs = new HashMap<>();
configs.put(TimeOfDayTriggerHandler.CFG_TIME, "16:00");
final Configuration triggerConfig = new Configuration(configs);
final String triggerUID = "TimeOfDayTrigger_" + rand;
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerUID).withTypeUID(TimeOfDayTriggerHandler.MODULE_TYPE_ID).withConfiguration(triggerConfig).build());
final Configuration actionConfig = new Configuration();
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction_" + rand).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
configs = new HashMap<>();
configs.put(DayOfWeekConditionHandler.CFG_DAYS, Arrays.asList("MON", "WED"));
final Configuration conditionConfig = new Configuration(configs);
List<Condition> conditions = List.of(ModuleBuilder.createCondition().withId("DayCondition" + rand).withTypeUID(DayOfWeekConditionHandler.MODULE_TYPE_ID).withConfiguration(conditionConfig).build());
return RuleBuilder.create("timeOfdayRule_" + rand).withTriggers(triggers).withActions(actions).withConditions(conditions).withName("TimeOfDayRule").withTags("Schedule").build();
}
use of org.openhab.core.automation.Trigger in project openhab-core by openhab.
the class RuleSimulationTest method createRuleWithEphemerisCondition.
/**
* creates a rule with an day of time trigger at 10:00, that has an side condition to fire only
* on the weekend.
*/
private static Rule createRuleWithEphemerisCondition() {
int rand = new Random().nextInt();
Map<String, Object> configs = new HashMap<>();
configs.put(TimeOfDayTriggerHandler.CFG_TIME, "10:00");
final Configuration triggerConfig = new Configuration(configs);
final String triggerUID = "TimeOfDayTrigger_" + rand;
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerUID).withTypeUID(TimeOfDayTriggerHandler.MODULE_TYPE_ID).withConfiguration(triggerConfig).build());
final Configuration actionConfig = new Configuration();
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction_" + rand).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
final Configuration conditionConfig = new Configuration();
List<Condition> conditions = List.of(ModuleBuilder.createCondition().withId("DayCondition" + rand).withTypeUID(EphemerisConditionHandler.WEEKEND_MODULE_TYPE_ID).withConfiguration(conditionConfig).build());
return RuleBuilder.create("weekdayRule_" + rand).withTriggers(triggers).withActions(actions).withConditions(conditions).withName("WeekdaysRule").withTags("Schedule").build();
}
Aggregations