Search in sources :

Example 1 with ModuleHandler

use of org.openhab.core.automation.handler.ModuleHandler in project openhab-core by openhab.

the class ScriptedPrivateModuleHandlerFactory method internalCreate.

@Override
@Nullable
protected ModuleHandler internalCreate(Module module, String ruleUID) {
    ModuleHandler moduleHandler = null;
    ScriptedHandler scriptedHandler = null;
    try {
        scriptedHandler = privateTypes.get(module.getConfiguration().get(PRIV_ID));
    } catch (Exception e) {
        logger.warn("ScriptedHandler {} for ruleUID {} not found", module.getConfiguration().get(PRIV_ID), ruleUID);
    }
    if (scriptedHandler != null) {
        moduleHandler = getModuleHandler(module, scriptedHandler);
    }
    return moduleHandler;
}
Also used : ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) ScriptedHandler(org.openhab.core.automation.module.script.rulesupport.shared.ScriptedHandler) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with ModuleHandler

use of org.openhab.core.automation.handler.ModuleHandler 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 3 with ModuleHandler

use of org.openhab.core.automation.handler.ModuleHandler in project openhab-core by openhab.

the class EphemerisModuleHandlerFactoryTest method testFactoryCreatesModuleHandlerForWeekdayCondition.

@Test
public void testFactoryCreatesModuleHandlerForWeekdayCondition() {
    when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.WEEKDAY_MODULE_TYPE_ID);
    when(moduleMock.getConfiguration()).thenReturn(new Configuration());
    ModuleHandler handler = factory.internalCreate(moduleMock, "My first rule");
    assertThat(handler, is(notNullValue()));
    assertThat(handler, instanceOf(EphemerisConditionHandler.class));
    when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("offset", 5)));
    handler = factory.internalCreate(moduleMock, "My second rule");
    assertThat(handler, is(notNullValue()));
    assertThat(handler, instanceOf(EphemerisConditionHandler.class));
}
Also used : ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) Configuration(org.openhab.core.config.core.Configuration) EphemerisConditionHandler(org.openhab.core.automation.internal.module.handler.EphemerisConditionHandler) Test(org.junit.jupiter.api.Test)

Example 4 with ModuleHandler

use of org.openhab.core.automation.handler.ModuleHandler in project openhab-core by openhab.

the class EphemerisModuleHandlerFactoryTest method testFactoryCreatesModuleHandlerForDaysetCondition.

@Test
public void testFactoryCreatesModuleHandlerForDaysetCondition() {
    when(moduleMock.getTypeUID()).thenReturn(EphemerisConditionHandler.DAYSET_MODULE_TYPE_ID);
    when(moduleMock.getConfiguration()).thenReturn(new Configuration(Map.of("dayset", "school")));
    ModuleHandler handler = factory.internalCreate(moduleMock, "My second rule");
    assertThat(handler, is(notNullValue()));
    assertThat(handler, instanceOf(EphemerisConditionHandler.class));
}
Also used : ModuleHandler(org.openhab.core.automation.handler.ModuleHandler) Configuration(org.openhab.core.config.core.Configuration) EphemerisConditionHandler(org.openhab.core.automation.internal.module.handler.EphemerisConditionHandler) Test(org.junit.jupiter.api.Test)

Example 5 with ModuleHandler

use of org.openhab.core.automation.handler.ModuleHandler 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)

Aggregations

ModuleHandler (org.openhab.core.automation.handler.ModuleHandler)10 Test (org.junit.jupiter.api.Test)4 EphemerisConditionHandler (org.openhab.core.automation.internal.module.handler.EphemerisConditionHandler)4 Configuration (org.openhab.core.config.core.Configuration)4 Nullable (org.eclipse.jdt.annotation.Nullable)3 Module (org.openhab.core.automation.Module)2 ModuleHandlerFactory (org.openhab.core.automation.handler.ModuleHandlerFactory)2 WrappedModule (org.openhab.core.automation.internal.ruleengine.WrappedModule)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Action (org.openhab.core.automation.Action)1 Trigger (org.openhab.core.automation.Trigger)1 ActionHandler (org.openhab.core.automation.handler.ActionHandler)1 BaseModuleHandlerFactory (org.openhab.core.automation.handler.BaseModuleHandlerFactory)1 ConditionHandler (org.openhab.core.automation.handler.ConditionHandler)1 TriggerHandler (org.openhab.core.automation.handler.TriggerHandler)1 ModuleImpl (org.openhab.core.automation.internal.ModuleImpl)1 CompositeModuleHandlerFactory (org.openhab.core.automation.internal.composite.CompositeModuleHandlerFactory)1 WrappedAction (org.openhab.core.automation.internal.ruleengine.WrappedAction)1