Search in sources :

Example 1 with ModuleType

use of org.openhab.core.automation.type.ModuleType in project openhab-core by openhab.

the class CommandlineModuleTypeProvider method importModuleTypes.

/**
 * This method is responsible for importing a set of ModuleTypes from a specified file or URL resource.
 *
 * @param parserType is relevant to the format that you need for conversion of the ModuleTypes in text.
 * @param url a specified URL for import.
 * @throws IOException when I/O operation has failed or has been interrupted.
 * @throws ParsingException when parsing of the text fails for some reasons.
 * @see AutomationCommandsPluggable#importModuleTypes(String, URL)
 */
public Set<ModuleType> importModuleTypes(String parserType, URL url) throws IOException, ParsingException {
    Parser<ModuleType> parser = parsers.get(parserType);
    if (parser != null) {
        InputStream is = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        InputStreamReader inputStreamReader = new InputStreamReader(bis);
        try {
            return importData(url, parser, inputStreamReader);
        } finally {
            inputStreamReader.close();
        }
    } else {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.MODULE_TYPE, null, new IllegalArgumentException("Parser " + parserType + " not available")));
    }
}
Also used : ModuleType(org.openhab.core.automation.type.ModuleType) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ParsingException(org.openhab.core.automation.parser.ParsingException) ParsingNestedException(org.openhab.core.automation.parser.ParsingNestedException)

Example 2 with ModuleType

use of org.openhab.core.automation.type.ModuleType in project openhab-core by openhab.

the class CommandlineModuleTypeProvider method importData.

@Override
protected Set<ModuleType> importData(URL url, Parser<ModuleType> parser, InputStreamReader inputStreamReader) throws ParsingException {
    Set<ModuleType> providedObjects = parser.parse(inputStreamReader);
    if (providedObjects != null && !providedObjects.isEmpty()) {
        String uid = null;
        List<String> portfolio = new ArrayList<>();
        synchronized (providerPortfolio) {
            providerPortfolio.put(url, portfolio);
        }
        List<ParsingNestedException> importDataExceptions = new ArrayList<>();
        for (ModuleType providedObject : providedObjects) {
            List<ParsingNestedException> exceptions = new ArrayList<>();
            uid = providedObject.getUID();
            checkExistence(uid, exceptions);
            if (exceptions.isEmpty()) {
                portfolio.add(uid);
                synchronized (providedObjectsHolder) {
                    notifyListeners(providedObjectsHolder.put(uid, providedObject), providedObject);
                }
            } else {
                importDataExceptions.addAll(exceptions);
            }
        }
        if (!importDataExceptions.isEmpty()) {
            throw new ParsingException(importDataExceptions);
        }
    }
    return providedObjects;
}
Also used : ModuleType(org.openhab.core.automation.type.ModuleType) ParsingException(org.openhab.core.automation.parser.ParsingException) ArrayList(java.util.ArrayList) ParsingNestedException(org.openhab.core.automation.parser.ParsingNestedException)

Example 3 with ModuleType

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

Example 4 with ModuleType

use of org.openhab.core.automation.type.ModuleType in project openhab-core by openhab.

the class AnnotatedActionModuleTypeProvider method removeActionProvider.

public void removeActionProvider(AnnotatedActions actionProvider, Map<String, Object> properties) {
    Collection<ModuleInformation> moduleInformations = helper.parseAnnotations(actionProvider);
    String configName = getConfigNameFromService(properties);
    for (ModuleInformation mi : moduleInformations) {
        mi.setConfigName(configName);
        ModuleType oldType = null;
        Set<ModuleInformation> availableModuleConfigs = moduleInformation.get(mi.getUID());
        if (availableModuleConfigs != null) {
            if (availableModuleConfigs.size() > 1) {
                oldType = helper.buildModuleType(mi.getUID(), moduleInformation);
                availableModuleConfigs.remove(mi);
            } else {
                moduleInformation.remove(mi.getUID());
            }
            ModuleType mt = helper.buildModuleType(mi.getUID(), moduleInformation);
            // localize moduletype -> remove from map
            if (mt != null) {
                for (ProviderChangeListener<ModuleType> l : changeListeners) {
                    if (oldType != null) {
                        l.updated(this, oldType, mt);
                    } else {
                        l.removed(this, mt);
                    }
                }
            }
        }
    }
}
Also used : ModuleType(org.openhab.core.automation.type.ModuleType) ModuleInformation(org.openhab.core.automation.module.provider.ModuleInformation)

Example 5 with ModuleType

use of org.openhab.core.automation.type.ModuleType in project openhab-core by openhab.

the class AnnotatedActionModuleTypeProvider method localizeModuleType.

@Nullable
private ModuleType localizeModuleType(String uid, @Nullable Locale locale) {
    Set<ModuleInformation> mis = moduleInformation.get(uid);
    if (mis != null && !mis.isEmpty()) {
        ModuleInformation mi = mis.iterator().next();
        Bundle bundle = FrameworkUtil.getBundle(mi.getActionProvider().getClass());
        ModuleType mt = helper.buildModuleType(uid, moduleInformation);
        ModuleType localizedModuleType = moduleTypeI18nService.getModuleTypePerLocale(mt, locale, bundle);
        return localizedModuleType;
    }
    return null;
}
Also used : ModuleType(org.openhab.core.automation.type.ModuleType) ModuleInformation(org.openhab.core.automation.module.provider.ModuleInformation) Bundle(org.osgi.framework.Bundle) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ModuleType (org.openhab.core.automation.type.ModuleType)28 ArrayList (java.util.ArrayList)13 Nullable (org.eclipse.jdt.annotation.Nullable)7 ActionType (org.openhab.core.automation.type.ActionType)7 ModuleInformation (org.openhab.core.automation.module.provider.ModuleInformation)6 TriggerType (org.openhab.core.automation.type.TriggerType)5 CompositeActionType (org.openhab.core.automation.type.CompositeActionType)4 CompositeConditionType (org.openhab.core.automation.type.CompositeConditionType)4 CompositeTriggerType (org.openhab.core.automation.type.CompositeTriggerType)4 ConditionType (org.openhab.core.automation.type.ConditionType)4 ConfigDescriptionParameter (org.openhab.core.config.core.ConfigDescriptionParameter)4 Locale (java.util.Locale)3 Test (org.junit.jupiter.api.Test)3 ParsingException (org.openhab.core.automation.parser.ParsingException)3 ParsingNestedException (org.openhab.core.automation.parser.ParsingNestedException)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Action (org.openhab.core.automation.Action)2 Trigger (org.openhab.core.automation.Trigger)2