Search in sources :

Example 1 with WrappedModule

use of org.openhab.core.automation.internal.ruleengine.WrappedModule in project openhab-core by openhab.

the class RuleEngineImpl method setModuleHandlers.

/**
 * This method links modules to corresponding module handlers.
 *
 * @param rUID id of rule containing these modules
 * @param modules list of modules
 * @return null when all modules are connected or list of RuleErrors for missing handlers.
 */
@Nullable
private <T extends WrappedModule<?, ?>> String setModuleHandlers(String rUID, List<T> modules) {
    StringBuilder sb = null;
    for (T mm : modules) {
        final Module m = mm.unwrap();
        try {
            ModuleHandler moduleHandler = getModuleHandler(m, rUID);
            if (moduleHandler != null) {
                if (mm instanceof WrappedAction) {
                    ((WrappedAction) mm).setModuleHandler((ActionHandler) moduleHandler);
                } else if (mm instanceof WrappedCondition) {
                    ((WrappedCondition) mm).setModuleHandler((ConditionHandler) moduleHandler);
                } else if (mm instanceof WrappedTrigger) {
                    ((WrappedTrigger) mm).setModuleHandler((TriggerHandler) moduleHandler);
                }
            } else {
                if (sb == null) {
                    sb = new StringBuilder();
                }
                String message = "Missing handler '" + m.getTypeUID() + "' for module '" + m.getId() + "'";
                sb.append(message).append("\n");
                logger.trace(message);
            }
        } catch (Throwable t) {
            if (sb == null) {
                sb = new StringBuilder();
            }
            String message = "Getting handler '" + m.getTypeUID() + "' for module '" + m.getId() + "' failed: " + t.getMessage();
            sb.append(message).append("\n");
            logger.trace(message);
        }
    }
    return sb != null ? sb.toString() : null;
}
Also used : ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) WrappedCondition(org.openhab.core.automation.internal.ruleengine.WrappedCondition) ConditionHandler(org.openhab.core.automation.handler.ConditionHandler) WrappedTrigger(org.openhab.core.automation.internal.ruleengine.WrappedTrigger) Module(org.openhab.core.automation.Module) WrappedModule(org.openhab.core.automation.internal.ruleengine.WrappedModule) WrappedAction(org.openhab.core.automation.internal.ruleengine.WrappedAction) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with WrappedModule

use of org.openhab.core.automation.internal.ruleengine.WrappedModule in project openhab-core by openhab.

the class RuleEngineImpl method validateModuleIDs.

/**
 * Validates IDs of modules. The module ids must be alphanumeric with only underscores and dashes.
 *
 * @param rule the rule to validate
 * @throws IllegalArgumentException when a module id contains illegal characters
 */
private void validateModuleIDs(WrappedRule rule) {
    for (final WrappedModule<?, ?> mm : rule.getModules()) {
        final Module m = mm.unwrap();
        String mId = m.getId();
        if (!mId.matches("[A-Za-z0-9_-]*")) {
            rule.setStatusInfo(new RuleStatusInfo(RuleStatus.UNINITIALIZED, RuleStatusDetail.INVALID_RULE, "It is null or not fit to the pattern: [A-Za-z0-9_-]*"));
            throw new IllegalArgumentException("Invalid module uid: " + mId + ". It is null or not fit to the pattern: [A-Za-z0-9_-]*");
        }
    }
}
Also used : RuleStatusInfo(org.openhab.core.automation.RuleStatusInfo) Module(org.openhab.core.automation.Module) WrappedModule(org.openhab.core.automation.internal.ruleengine.WrappedModule)

Example 3 with WrappedModule

use of org.openhab.core.automation.internal.ruleengine.WrappedModule in project openhab-core by openhab.

the class RuleEngineImpl method removeModuleHandlers.

/**
 * Unlink module handlers from their modules. The method is called when the rule containing these modules goes into
 * {@link RuleStatus#UNINITIALIZED} state.
 *
 * @param modules list of modules which should be disconnected.
 */
private <T extends WrappedModule<?, ?>> void removeModuleHandlers(List<T> modules, String ruleUID) {
    for (T mm : modules) {
        final Module m = mm.unwrap();
        ModuleHandler handler = mm.getModuleHandler();
        if (handler != null) {
            ModuleHandlerFactory factory = getModuleHandlerFactory(m.getTypeUID());
            if (factory != null) {
                factory.ungetHandler(m, ruleUID, handler);
            }
            mm.setModuleHandler(null);
        }
    }
}
Also used : ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) ModuleHandlerFactory(org.openhab.core.automation.handler.ModuleHandlerFactory) CompositeModuleHandlerFactory(org.openhab.core.automation.internal.composite.CompositeModuleHandlerFactory) Module(org.openhab.core.automation.Module) WrappedModule(org.openhab.core.automation.internal.ruleengine.WrappedModule)

Aggregations

Module (org.openhab.core.automation.Module)3 WrappedModule (org.openhab.core.automation.internal.ruleengine.WrappedModule)3 ModuleHandler (org.openhab.core.automation.handler.ModuleHandler)2 Nullable (org.eclipse.jdt.annotation.Nullable)1 RuleStatusInfo (org.openhab.core.automation.RuleStatusInfo)1 ConditionHandler (org.openhab.core.automation.handler.ConditionHandler)1 ModuleHandlerFactory (org.openhab.core.automation.handler.ModuleHandlerFactory)1 CompositeModuleHandlerFactory (org.openhab.core.automation.internal.composite.CompositeModuleHandlerFactory)1 WrappedAction (org.openhab.core.automation.internal.ruleengine.WrappedAction)1 WrappedCondition (org.openhab.core.automation.internal.ruleengine.WrappedCondition)1 WrappedTrigger (org.openhab.core.automation.internal.ruleengine.WrappedTrigger)1