]> git.basschouten.com Git - openhab-addons.git/blob
6dcd52c87c09387ce1c121f1776226383dacce0a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.automation;
14
15 import java.util.Arrays;
16 import java.util.Collection;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.automation.Action;
21 import org.openhab.core.automation.Module;
22 import org.openhab.core.automation.RuleRegistry;
23 import org.openhab.core.automation.handler.BaseModuleHandlerFactory;
24 import org.openhab.core.automation.handler.ModuleHandler;
25 import org.openhab.core.automation.handler.ModuleHandlerFactory;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This factory is responsible for rule and http related module types.
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
39 @Component(service = ModuleHandlerFactory.class)
40 public class RulesHandlerFactory extends BaseModuleHandlerFactory {
41
42     private final Logger logger = LoggerFactory.getLogger(RulesHandlerFactory.class);
43
44     private static final Collection<String> TYPES = Arrays
45             .asList(new String[] { RemoveRuleActionHandler.MODULE_TYPE_ID, HttpActionHandler.MODULE_TYPE_ID });
46
47     @Reference
48     protected @NonNullByDefault({}) RuleRegistry ruleRegistry;
49
50     @Reference
51     protected @NonNullByDefault({}) HttpClientFactory httpFactory;
52
53     @Override
54     @Deactivate
55     public void deactivate() {
56         super.deactivate();
57     }
58
59     @Override
60     public Collection<String> getTypes() {
61         return TYPES;
62     }
63
64     @Override
65     protected @Nullable ModuleHandler internalCreate(Module module, String ruleUID) {
66         logger.trace("create {} -> {}", module.getId(), module.getTypeUID());
67         String moduleTypeUID = module.getTypeUID();
68         if (RemoveRuleActionHandler.MODULE_TYPE_ID.equals(moduleTypeUID) && module instanceof Action) {
69             return new RemoveRuleActionHandler((Action) module, ruleRegistry);
70         } else if (HttpActionHandler.MODULE_TYPE_ID.equals(moduleTypeUID) && module instanceof Action) {
71             return new HttpActionHandler((Action) module, httpFactory);
72         } else {
73             logger.error("The module handler type '{}' is not supported.", moduleTypeUID);
74         }
75         return null;
76     }
77 }