use of org.eclipse.smarthome.automation.Action in project smarthome by eclipse.
the class WelcomeHomeRulesProvider method createLightsRule.
/**
* This method creates a rule from scratch by using trigger, condition, action, configDescriptions and
* configuration, tags.
*
* @return the created rule
*/
private Rule createLightsRule() {
// initialize the trigger
String triggerId = "LightsSwitchOnRuleTrigger";
List<Trigger> triggers = new ArrayList<Trigger>();
triggers.add(new Trigger(triggerId, LightsTriggerType.UID, null));
// initialize the condition - here the tricky part is the referring into the condition input - trigger output.
// The syntax is a similar to the JUEL syntax.
Configuration config = new Configuration();
config.put(StateConditionType.CONFIG_STATE, "on");
List<Condition> conditions = new ArrayList<Condition>();
Map<String, String> inputs = new HashMap<String, String>();
inputs.put(StateConditionType.INPUT_CURRENT_STATE, triggerId + "." + StateConditionType.INPUT_CURRENT_STATE);
conditions.add(new Condition("LightsStateCondition", StateConditionType.UID, config, inputs));
// initialize the action - here the tricky part is the referring into the action configuration parameter - the
// template configuration parameter. The syntax is a similar to the JUEL syntax.
config = new Configuration();
config.put(WelcomeHomeActionType.CONFIG_DEVICE, "Lights");
config.put(WelcomeHomeActionType.CONFIG_RESULT, "Lights are switched on");
List<Action> actions = new ArrayList<Action>();
actions.add(new Action("LightsSwitchOnAction", WelcomeHomeActionType.UID, config, null));
// initialize the configDescriptions
List<ConfigDescriptionParameter> configDescriptions = new ArrayList<ConfigDescriptionParameter>();
final ConfigDescriptionParameter device = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_UNIT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Device").withDescription("Device description").build();
final ConfigDescriptionParameter result = ConfigDescriptionParameterBuilder.create(WelcomeHomeRulesProvider.CONFIG_EXPECTED_RESULT, Type.TEXT).withRequired(true).withReadOnly(true).withMultiple(false).withLabel("Result").withDescription("Result description").build();
configDescriptions.add(device);
configDescriptions.add(result);
// initialize the configuration
config = new Configuration();
config.put(CONFIG_UNIT, "Lights");
config.put(CONFIG_EXPECTED_RESULT, "The lights are switched on.");
// create the rule
Rule lightsSwitchOn = new Rule(L_UID);
lightsSwitchOn.setTriggers(triggers);
lightsSwitchOn.setConfigurationDescriptions(configDescriptions);
lightsSwitchOn.setConditions(conditions);
lightsSwitchOn.setActions(actions);
// initialize the tags
Set<String> tags = new HashSet<String>();
tags.add("lights");
// set the tags
lightsSwitchOn.setTags(tags);
return lightsSwitchOn;
}
use of org.eclipse.smarthome.automation.Action in project smarthome by eclipse.
the class RuleTemplateRegistry method copyActions.
private List<Action> copyActions(List<Action> actions) {
List<Action> res = new ArrayList<Action>();
if (actions != null) {
for (Action a : actions) {
Configuration c = new Configuration();
c.setProperties(a.getConfiguration().getProperties());
Action action = new Action(a.getId(), a.getTypeUID(), c, a.getInputs());
action.setLabel(a.getLabel());
action.setDescription(a.getDescription());
res.add(action);
}
}
return res;
}
use of org.eclipse.smarthome.automation.Action in project smarthome by eclipse.
the class ModuleTypeRegistryImpl method copyActions.
private static List<Action> copyActions(List<Action> actions) {
List<Action> res = new ArrayList<Action>();
if (actions != null) {
for (Action a : actions) {
Configuration c = new Configuration();
c.setProperties(a.getConfiguration().getProperties());
Action action = new Action(a.getId(), a.getTypeUID(), c, a.getInputs());
action.setLabel(a.getLabel());
action.setDescription(a.getDescription());
res.add(action);
}
}
return res;
}
use of org.eclipse.smarthome.automation.Action in project smarthome by eclipse.
the class ConnectionValidator method checkConnection.
/**
* The method validates the connection between outputs of list of triggers and actions to the action's input. It
* checks if the input is unconnected and compatibility of data types of the input and connected output. Throws
* exception if they are incompatible.
*
* @param connection that should be validated
* @param input that should be validated
* @param triggers is a list with triggers of the rule on which the action belongs
* @param actions is a list with actions of the rule on which the action belongs
* @throws IllegalArgumentException when validation fails.
*/
private static void checkConnection(Connection connection, Input input, List<Trigger> triggers, List<Action> actions) {
Map<String, Action> actionsMap = new HashMap<String, Action>();
for (Action a : actions) {
actionsMap.put(a.getId(), a);
}
String moduleId = connection.getOuputModuleId();
Action action = actionsMap.get(moduleId);
String msg = " Invalid Connection \"" + connection.getInputName() + "\" : ";
if (moduleId != null && action != null) {
String typeUID = action.getTypeUID();
ActionType actionType = (ActionType) mtRegistry.get(typeUID);
if (actionType == null) {
throw new IllegalArgumentException(msg + " Action Type with UID \"" + typeUID + "\" does not exist!");
}
checkCompatibility(msg, connection, input, actionType.getOutputs());
} else {
checkConnection(connection, input, triggers);
}
}
use of org.eclipse.smarthome.automation.Action in project smarthome by eclipse.
the class RunRuleModuleTest method createOuterRule.
private Rule createOuterRule() {
final Configuration outerRuleTriggerConfig = new Configuration(Collections.unmodifiableMap(Stream.of(new SimpleEntry<>("eventSource", "ruleTrigger"), new SimpleEntry<>("eventTopic", "smarthome/*"), new SimpleEntry<>("eventTypes", "ItemStateEvent")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
final Configuration outerRuleActionConfig = new Configuration(Collections.unmodifiableMap(Stream.of(new SimpleEntry<>("ruleUIDs", "[exampleSceneRule]")).collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))));
final Rule outerRule = new Rule("sceneActivationRule");
outerRule.setTriggers(Arrays.asList(new Trigger[] { new Trigger("ItemStateChangeTrigger2", "core.GenericEventTrigger", outerRuleTriggerConfig) }));
outerRule.setActions(Arrays.asList(new Action[] { new Action("RunRuleAction1", "core.RunRuleAction", outerRuleActionConfig, null) }));
outerRule.setName("scene activator");
return outerRule;
}
Aggregations