Search in sources :

Example 1 with Module

use of org.eclipse.smarthome.automation.Module in project smarthome by eclipse.

the class ReferenceResolverUtilTest method testModuleInputResolving.

@Test
public void testModuleInputResolving() {
    // test Composite child Module(condition) context
    Module condition = new Condition(null, null, null, compositeChildModuleInputsReferences);
    Map<String, Object> conditionContext = ReferenceResolverUtil.getCompositeChildContext(condition, context);
    Assert.assertEquals(conditionContext, expectedCompositeChildModuleContext);
    // test Composite child Module(action) context
    Module action = new Action(null, null, null, compositeChildModuleInputsReferences);
    Map<String, Object> actionContext = ReferenceResolverUtil.getCompositeChildContext(action, context);
    Assert.assertEquals(actionContext, expectedCompositeChildModuleContext);
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Module(org.eclipse.smarthome.automation.Module) Test(org.junit.Test)

Example 2 with Module

use of org.eclipse.smarthome.automation.Module in project smarthome by eclipse.

the class ReferenceResolverUtilTest method testModuleConfigurationResolving.

@Test
public void testModuleConfigurationResolving() {
    // test trigger configuration..
    Module trigger = new Trigger(null, null, new Configuration(moduleConfiguration));
    ReferenceResolverUtil.updateModuleConfiguration(trigger, context);
    Assert.assertEquals(trigger.getConfiguration(), new Configuration(expectedModuleConfiguration));
    // test condition configuration..
    Module condition = new Condition(null, null, new Configuration(moduleConfiguration), null);
    ReferenceResolverUtil.updateModuleConfiguration(condition, context);
    Assert.assertEquals(condition.getConfiguration(), new Configuration(expectedModuleConfiguration));
    // test action configuration..
    Module action = new Action(null, null, new Configuration(moduleConfiguration), null);
    ReferenceResolverUtil.updateModuleConfiguration(action, context);
    Assert.assertEquals(action.getConfiguration(), new Configuration(expectedModuleConfiguration));
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Configuration(org.eclipse.smarthome.config.core.Configuration) Module(org.eclipse.smarthome.automation.Module) Test(org.junit.Test)

Example 3 with Module

use of org.eclipse.smarthome.automation.Module in project smarthome by eclipse.

the class RuleEngine method setRule.

/**
 * This method tries to initialize the rule. It uses available {@link ModuleHandlerFactory}s to create
 * {@link ModuleHandler}s for all {@link Module}s of the {@link Rule} and to link them. When all the modules have
 * associated module handlers then the {@link Rule} is initialized and it is ready to working. It goes into idle
 * state. Otherwise the Rule stays into not initialized and continue to wait missing handlers, module types or
 * templates.
 *
 * @param rUID a UID of rule which tries to be initialized.
 */
private void setRule(RuntimeRule runtimeRule) {
    if (isDisposed) {
        return;
    }
    String rUID = runtimeRule.getUID();
    setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.INITIALIZING), true);
    if (runtimeRule.getTemplateUID() != null) {
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.TEMPLATE_MISSING_ERROR), true);
        // Template is not available (when a template is resolved it removes tempalteUID configuration
        return;
    // property). The rule must stay NOT_INITIALISED.
    }
    List<Module> modules = runtimeRule.getModules(Module.class);
    for (Module m : modules) {
        updateMapModuleTypeToRule(rUID, m.getTypeUID());
    }
    String errMsgs;
    try {
        validateModuleIDs(modules);
        resolveConfiguration(runtimeRule);
        autoMapConnections(runtimeRule);
        ConnectionValidator.validateConnections(runtimeRule);
    } catch (RuntimeException e) {
        errMsgs = "\n Validation of rule " + rUID + " has failed! " + e.getLocalizedMessage();
        // change state to NOTINITIALIZED
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.CONFIGURATION_ERROR, errMsgs.trim()), true);
        return;
    }
    errMsgs = setModuleHandlers(rUID, modules);
    if (errMsgs == null) {
        register(runtimeRule);
        // change state to IDLE
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.IDLE), true);
        Future f = scheduleTasks.remove(rUID);
        if (f != null) {
            if (!f.isDone()) {
                f.cancel(true);
            }
        }
        if (scheduleTasks.isEmpty()) {
            if (executor != null) {
                executor.shutdown();
                executor = null;
            }
        }
    } else {
        // change state to NOTINITIALIZED
        setRuleStatusInfo(rUID, new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.HANDLER_INITIALIZING_ERROR, errMsgs), true);
        unregister(runtimeRule);
    }
}
Also used : RuleStatusInfo(org.eclipse.smarthome.automation.RuleStatusInfo) Future(java.util.concurrent.Future) Module(org.eclipse.smarthome.automation.Module)

Example 4 with Module

use of org.eclipse.smarthome.automation.Module in project smarthome by eclipse.

the class RuleEngine method normalizeModuleConfigurations.

private <T extends Module> void normalizeModuleConfigurations(List<@NonNull T> modules) {
    for (Module module : modules) {
        Configuration config = module.getConfiguration();
        if (config != null) {
            String type = module.getTypeUID();
            ModuleType mt = mtRegistry.get(type);
            if (mt != null) {
                List<ConfigDescriptionParameter> configDescriptions = mt.getConfigurationDescriptions();
                Map<String, ConfigDescriptionParameter> mapConfigDescriptions = getConfigDescriptionMap(configDescriptions);
                normalizeConfiguration(config, mapConfigDescriptions);
            }
        }
    }
}
Also used : ModuleType(org.eclipse.smarthome.automation.type.ModuleType) Configuration(org.eclipse.smarthome.config.core.Configuration) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter) Module(org.eclipse.smarthome.automation.Module)

Example 5 with Module

use of org.eclipse.smarthome.automation.Module in project smarthome by eclipse.

the class RuleResource method setModuleConfigParam.

@PUT
@Path("/{ruleUID}/{moduleCategory}/{id}/config/{param}")
@ApiOperation(value = "Sets the module's configuration parameter value.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Rule corresponding to the given UID does not found or does not have a module with such Category and ID.") })
@Consumes(MediaType.TEXT_PLAIN)
public Response setModuleConfigParam(@PathParam("ruleUID") @ApiParam(value = "ruleUID", required = true) String ruleUID, @PathParam("moduleCategory") @ApiParam(value = "moduleCategory", required = true) String moduleCategory, @PathParam("id") @ApiParam(value = "id", required = true) String id, @PathParam("param") @ApiParam(value = "param", required = true) String param, @ApiParam(value = "value", required = true) String value) {
    Rule rule = ruleRegistry.get(ruleUID);
    if (rule != null) {
        Module module = getModule(rule, moduleCategory, id);
        if (module != null) {
            Configuration configuration = module.getConfiguration();
            configuration.put(param, ConfigUtil.normalizeType(value));
            module.setConfiguration(configuration);
            ruleRegistry.update(rule);
            return Response.ok(null, MediaType.TEXT_PLAIN).build();
        }
    }
    return Response.status(Status.NOT_FOUND).build();
}
Also used : Configuration(org.eclipse.smarthome.config.core.Configuration) Rule(org.eclipse.smarthome.automation.Rule) Module(org.eclipse.smarthome.automation.Module) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Module (org.eclipse.smarthome.automation.Module)6 Configuration (org.eclipse.smarthome.config.core.Configuration)3 Action (org.eclipse.smarthome.automation.Action)2 Condition (org.eclipse.smarthome.automation.Condition)2 Test (org.junit.Test)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Future (java.util.concurrent.Future)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Rule (org.eclipse.smarthome.automation.Rule)1 RuleStatusInfo (org.eclipse.smarthome.automation.RuleStatusInfo)1 Trigger (org.eclipse.smarthome.automation.Trigger)1 BaseModuleHandlerFactory (org.eclipse.smarthome.automation.handler.BaseModuleHandlerFactory)1 ModuleHandler (org.eclipse.smarthome.automation.handler.ModuleHandler)1 ModuleHandlerFactory (org.eclipse.smarthome.automation.handler.ModuleHandlerFactory)1 ModuleType (org.eclipse.smarthome.automation.type.ModuleType)1 ConfigDescriptionParameter (org.eclipse.smarthome.config.core.ConfigDescriptionParameter)1