use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class DSLRuleProvider method toRule.
private Rule toRule(String modelName, org.openhab.core.model.rule.rules.Rule rule, int index) {
String name = rule.getName();
String uid = modelName + "-" + index;
// Create Triggers
triggerId = 0;
List<Trigger> triggers = new ArrayList<>();
for (EventTrigger t : rule.getEventtrigger()) {
Trigger trigger = mapTrigger(t);
if (trigger != null) {
triggers.add(trigger);
}
}
// Create Action
String context = DSLScriptContextProvider.CONTEXT_IDENTIFIER + modelName + "-" + index + "\n";
XBlockExpression expression = rule.getScript();
String script = NodeModelUtils.findActualNodeFor(expression).getText();
Configuration cfg = new Configuration();
cfg.put("script", context + removeIndentation(script));
cfg.put("type", MIMETYPE_OPENHAB_DSL_RULE);
List<Action> actions = List.of(ActionBuilder.create().withId("script").withTypeUID("script.ScriptAction").withConfiguration(cfg).build());
return RuleBuilder.create(uid).withName(name).withTriggers(triggers).withActions(actions).build();
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class AutomationIntegrationTest method assertThatRuleCanBeUpdated.
@Test
public void assertThatRuleCanBeUpdated() throws ItemNotFoundException {
Configuration triggerConfig = new Configuration(Map.of("eventTopic", "runNowEventTopic/*"));
Map<String, Object> params = new HashMap<>();
params.put("itemName", "myLampItem3");
params.put("command", "ON");
Configuration actionConfig = new Configuration(params);
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("GenericEventTriggerId").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandActionId").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
String ruleId = "runNowRule" + new Random().nextInt();
Rule rule = RuleBuilder.create(ruleId).withTriggers(triggers).withActions(actions).build();
logger.info("Rule created: {}", rule.getUID());
ruleRegistry.add(rule);
// TEST RULE
waitForAssert(() -> {
assertThat(ruleEngine.getStatusInfo(rule.getUID()).getStatus(), is(RuleStatus.IDLE));
}, 3000, 100);
EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemCommandEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event e) {
logger.info("Event: {}", e.getTopic());
if (e.getTopic().contains("myLampItem3")) {
itemEvent = e;
}
}
};
registerService(itemEventHandler);
ruleEngine.runNow(rule.getUID());
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
}, 3000, 100);
waitForAssert(() -> {
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}, 3000, 100);
params.put("command", "OFF");
actionConfig = new Configuration(params);
actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandActionId").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
Rule updatedRule = RuleBuilder.create(ruleId).withTriggers(triggers).withActions(actions).build();
ruleRegistry.update(updatedRule);
waitForAssert(() -> {
assertThat(ruleEngine.getStatusInfo(updatedRule.getUID()).getStatus(), is(RuleStatus.IDLE));
}, 3000, 100);
logger.info("Rule updated: {}", updatedRule.getUID());
itemEvent = null;
ruleEngine.runNow(updatedRule.getUID());
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
}, 3000, 100);
waitForAssert(() -> {
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.OFF));
}, 3000, 100);
ruleRegistry.remove(updatedRule.getUID());
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class AutomationIntegrationTest method assertARuleAddedByApiIsExecutedAsExpected.
@Test
public void assertARuleAddedByApiIsExecutedAsExpected() {
logger.info("assert a rule added by api is executed as expected");
// Creation of RULE
Map<String, Object> params = new HashMap<>();
params.put("eventSource", "myMotionItem2");
params.put("eventTopic", "openhab/*");
params.put("eventTypes", "ItemStateEvent");
Configuration triggerConfig = new Configuration(params);
params = new HashMap<>();
params.put("itemName", "myLampItem2");
params.put("command", "ON");
Configuration actionConfig = new Configuration(params);
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId("ItemStateChangeTrigger2").withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction2").withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
Rule rule = RuleBuilder.create("myRule21").withTriggers(triggers).withActions(actions).withName("RuleByJAVA_API").withTags("myRule21").build();
logger.info("Rule created: {}", rule.getUID());
ruleRegistry.add(rule);
ruleEngine.setEnabled(rule.getUID(), true);
ruleRegistry.remove(rule.getUID());
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class AutomationIntegrationTest method createSimpleRule.
/**
* creates a simple rule
*/
private Rule createSimpleRule() {
logger.info("createSimpleRule");
int rand = new Random().nextInt();
Map<String, Object> configs = new HashMap<>();
configs.put("eventSource", "myMotionItem2");
configs.put("eventTopic", "openhab/*");
configs.put("eventTypes", "ItemStateEvent");
Configuration triggerConfig = new Configuration(configs);
configs = new HashMap<>();
configs.put("itemName", "myLampItem2");
configs.put("command", "ON");
Configuration actionConfig = new Configuration(configs);
String triggerUID = "ItemStateChangeTrigger_" + rand;
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerUID).withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction_" + rand).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
Rule rule = RuleBuilder.create("myRule_" + rand).withTriggers(triggers).withActions(actions).withName("RuleByJAVA_API_" + rand).build();
logger.info("Rule created: {}", rule.getUID());
return rule;
}
use of org.openhab.core.automation.Action in project openhab-core by openhab.
the class AutomationIntegrationTest method assertARuleWithGenericConditionWorks.
@Test
public void assertARuleWithGenericConditionWorks() throws ItemNotFoundException {
int random = new Random().nextInt(100000);
logger.info("assert a rule with generic condition works");
// Creation of RULE
Map<String, Object> configs = new HashMap<>();
configs.put("eventSource", "myMotionItem5");
configs.put("eventTopic", "openhab/*");
configs.put("eventTypes", "ItemStateEvent");
Configuration triggerConfig = new Configuration(configs);
configs = new HashMap<>();
configs.put("operator", "matches");
configs.put("right", ".*ON.*");
configs.put("inputproperty", "payload");
Configuration condition1Config = new Configuration(configs);
configs = new HashMap<>();
configs.put("operator", "=");
configs.put("right", "myMotionItem5");
configs.put("inputproperty", "itemName");
Configuration condition2Config = new Configuration(configs);
configs = new HashMap<>();
configs.put("itemName", "myLampItem5");
configs.put("command", "ON");
Configuration actionConfig = new Configuration(configs);
String triggerId = "ItemStateChangeTrigger" + random;
List<Trigger> triggers = List.of(ModuleBuilder.createTrigger().withId(triggerId).withTypeUID("core.GenericEventTrigger").withConfiguration(triggerConfig).build());
List<Condition> conditions = Stream.of(ModuleBuilder.createCondition().withId("ItemStateCondition" + random).withTypeUID("core.GenericCompareCondition").withConfiguration(condition1Config).withInputs(Map.of("input", triggerId + ".event")).build(), ModuleBuilder.createCondition().withId("ItemStateCondition" + (random + 1)).withTypeUID("core.GenericCompareCondition").withConfiguration(condition2Config).withInputs(Map.of("input", triggerId + ".event")).build()).collect(toList());
List<Action> actions = List.of(ModuleBuilder.createAction().withId("ItemPostCommandAction" + random).withTypeUID("core.ItemCommandAction").withConfiguration(actionConfig).build());
Rule rule = RuleBuilder.create("myRule_" + random).withTriggers(triggers).withConditions(conditions).withActions(actions).withName("RuleByJAVA_API" + random).withTags("myRule_" + random).build();
logger.info("Rule created: {}", rule.getUID());
ruleRegistry.add(rule);
ruleEngine.setEnabled(rule.getUID(), true);
// WAIT until Rule modules types are parsed and the rule becomes IDLE
waitForAssert(() -> {
assertThat(ruleRegistry.getAll().isEmpty(), is(false));
Rule rule2 = ruleRegistry.get(rule.getUID());
assertThat(rule2, is(notNullValue()));
RuleStatus ruleStatus2 = ruleEngine.getStatusInfo(rule2.getUID()).getStatus();
assertThat(ruleStatus2, is(RuleStatus.IDLE));
}, 10000, 200);
// TEST RULE
EventPublisher eventPublisher = getService(EventPublisher.class);
ItemRegistry itemRegistry = getService(ItemRegistry.class);
SwitchItem myMotionItem = (SwitchItem) itemRegistry.getItem("myPresenceItem5");
myMotionItem.setState(OnOffType.ON);
EventSubscriber itemEventHandler = new EventSubscriber() {
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemCommandEvent.TYPE);
}
@Override
@Nullable
public EventFilter getEventFilter() {
return null;
}
@Override
public void receive(Event e) {
logger.info("Event: {}", e.getTopic());
if (e.getTopic().contains("myLampItem5")) {
itemEvent = e;
}
}
};
registerService(itemEventHandler);
eventPublisher.post(ItemEventFactory.createStateEvent("myMotionItem5", OnOffType.ON));
waitForAssert(() -> {
assertThat(itemEvent, is(notNullValue()));
}, 3000, 100);
assertThat(itemEvent.getTopic(), is(equalTo("openhab/items/myLampItem5/command")));
assertThat(((ItemCommandEvent) itemEvent).getItemCommand(), is(OnOffType.ON));
}
Aggregations