Search in sources :

Example 1 with Trigger

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;
}
Also used : Trigger(org.openhab.core.automation.Trigger)

Example 2 with Trigger

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());
}
Also used : WrappedTrigger(org.openhab.core.automation.internal.ruleengine.WrappedTrigger) Trigger(org.openhab.core.automation.Trigger)

Example 3 with Trigger

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;
}
Also used : Action(org.openhab.core.automation.Action) TriggerHandler(org.openhab.core.automation.handler.TriggerHandler) CompositeConditionType(org.openhab.core.automation.type.CompositeConditionType) LinkedHashMap(java.util.LinkedHashMap) ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) CompositeTriggerType(org.openhab.core.automation.type.CompositeTriggerType) ModuleType(org.openhab.core.automation.type.ModuleType) Trigger(org.openhab.core.automation.Trigger) ArrayList(java.util.ArrayList) List(java.util.List) CompositeActionType(org.openhab.core.automation.type.CompositeActionType) ActionHandler(org.openhab.core.automation.handler.ActionHandler) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 4 with Trigger

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();
}
Also used : Condition(org.openhab.core.automation.Condition) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random)

Example 5 with Trigger

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();
}
Also used : Condition(org.openhab.core.automation.Condition) Action(org.openhab.core.automation.Action) Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Trigger(org.openhab.core.automation.Trigger) Random(java.util.Random)

Aggregations

Trigger (org.openhab.core.automation.Trigger)40 Action (org.openhab.core.automation.Action)26 Configuration (org.openhab.core.config.core.Configuration)23 Test (org.junit.jupiter.api.Test)21 Rule (org.openhab.core.automation.Rule)21 Condition (org.openhab.core.automation.Condition)18 HashMap (java.util.HashMap)17 JavaOSGiTest (org.openhab.core.test.java.JavaOSGiTest)17 Random (java.util.Random)14 Event (org.openhab.core.events.Event)12 EventSubscriber (org.openhab.core.events.EventSubscriber)12 ItemCommandEvent (org.openhab.core.items.events.ItemCommandEvent)12 Nullable (org.eclipse.jdt.annotation.Nullable)10 RuleStatusInfoEvent (org.openhab.core.automation.events.RuleStatusInfoEvent)9 EventPublisher (org.openhab.core.events.EventPublisher)9 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 RuleAddedEvent (org.openhab.core.automation.events.RuleAddedEvent)7 RuleRemovedEvent (org.openhab.core.automation.events.RuleRemovedEvent)7 RuleUpdatedEvent (org.openhab.core.automation.events.RuleUpdatedEvent)7