]> git.basschouten.com Git - openhab-addons.git/blob
3946a892322aa9a7ba558e0e029f20556accdd2e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.binding.shelly.internal;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.jetty.client.HttpClient;
23 import org.eclipse.jetty.util.ConcurrentHashSet;
24 import org.openhab.binding.shelly.internal.coap.ShellyCoapServer;
25 import org.openhab.binding.shelly.internal.config.ShellyBindingConfiguration;
26 import org.openhab.binding.shelly.internal.handler.ShellyBaseHandler;
27 import org.openhab.binding.shelly.internal.handler.ShellyLightHandler;
28 import org.openhab.binding.shelly.internal.handler.ShellyProtectedHandler;
29 import org.openhab.binding.shelly.internal.handler.ShellyRelayHandler;
30 import org.openhab.binding.shelly.internal.util.ShellyTranslationProvider;
31 import org.openhab.binding.shelly.internal.util.ShellyUtils;
32 import org.openhab.core.i18n.LocaleProvider;
33 import org.openhab.core.i18n.TranslationProvider;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.net.HttpServiceUtil;
36 import org.openhab.core.net.NetworkAddressService;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.service.component.ComponentContext;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import io.reactivex.annotations.NonNull;
50
51 /**
52  * The {@link ShellyHandlerFactory} is responsible for creating things and thing handlers.
53  *
54  * @author Markus Michels - Initial contribution
55  */
56 @NonNullByDefault
57 @Component(service = { ThingHandlerFactory.class, ShellyHandlerFactory.class }, configurationPid = "binding.shelly")
58 public class ShellyHandlerFactory extends BaseThingHandlerFactory {
59     private final Logger logger = LoggerFactory.getLogger(ShellyHandlerFactory.class);
60     private final HttpClient httpClient;
61     private final ShellyTranslationProvider messages;
62     private final ShellyCoapServer coapServer;
63     private final Set<ShellyBaseHandler> deviceListeners = new ConcurrentHashSet<>();
64     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ShellyBindingConstants.SUPPORTED_THING_TYPES_UIDS;
65     private ShellyBindingConfiguration bindingConfig = new ShellyBindingConfiguration();
66     private String localIP = "";
67     private int httpPort = -1;
68
69     /**
70      * Activate the bundle: save properties
71      *
72      * @param componentContext
73      * @param configProperties set of properties from cfg (use same names as in
74      *            thing config)
75      */
76     @Activate
77     public ShellyHandlerFactory(@Reference NetworkAddressService networkAddressService,
78             @Reference LocaleProvider localeProvider, @Reference TranslationProvider i18nProvider,
79             @Reference HttpClientFactory httpClientFactory, ComponentContext componentContext,
80             Map<String, Object> configProperties) {
81         logger.debug("Activate Shelly HandlerFactory");
82         super.activate(componentContext);
83
84         messages = new ShellyTranslationProvider(bundleContext.getBundle(), i18nProvider, localeProvider);
85         localIP = ShellyUtils.getString(networkAddressService.getPrimaryIpv4HostAddress());
86         if (localIP.isEmpty()) {
87             logger.warn("{}", messages.get("message.init.noipaddress"));
88         }
89
90         this.httpClient = httpClientFactory.getCommonHttpClient();
91         httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
92         if (httpPort == -1) {
93             httpPort = 8080;
94         }
95         logger.debug("Using OH HTTP port {}", httpPort);
96
97         this.coapServer = new ShellyCoapServer();
98
99         // Save bindingConfig & pass it to all registered listeners
100         bindingConfig.updateFromProperties(configProperties);
101     }
102
103     @Override
104     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
105         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
106     }
107
108     @Override
109     protected @Nullable ThingHandler createHandler(Thing thing) {
110         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
111         String thingType = thingTypeUID.getId();
112         ShellyBaseHandler handler = null;
113
114         if (thingType.equals(THING_TYPE_SHELLYPROTECTED_STR)) {
115             logger.debug("{}: Create new thing of type {} using ShellyProtectedHandler", thing.getLabel(),
116                     thingTypeUID.toString());
117             handler = new ShellyProtectedHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort,
118                     httpClient);
119         } else if (thingType.equals(THING_TYPE_SHELLYBULB.getId()) || thingType.equals(THING_TYPE_SHELLYDUO.getId())
120                 || thingType.equals(THING_TYPE_SHELLYRGBW2_COLOR.getId())
121                 || thingType.equals(THING_TYPE_SHELLYRGBW2_WHITE.getId())) {
122             logger.debug("{}: Create new thing of type {} using ShellyLightHandler", thing.getLabel(),
123                     thingTypeUID.toString());
124             handler = new ShellyLightHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort, httpClient);
125         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
126             logger.debug("{}: Create new thing of type {} using ShellyRelayHandler", thing.getLabel(),
127                     thingTypeUID.toString());
128             handler = new ShellyRelayHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort, httpClient);
129         }
130
131         if (handler != null) {
132             deviceListeners.add(handler);
133             return handler;
134         }
135
136         logger.debug("Unable to create Thing Handler instance!");
137         return null;
138     }
139
140     /**
141      * Remove handler of things.
142      */
143     @Override
144     protected synchronized void removeHandler(@NonNull ThingHandler thingHandler) {
145         if (thingHandler instanceof ShellyBaseHandler) {
146             deviceListeners.remove(thingHandler);
147         }
148     }
149
150     /**
151      * Dispatch event to registered devices.
152      *
153      * @param deviceName
154      * @param componentIndex Index of component, e.g. 2 for relay2
155      * @param eventType Type of event, e.g. light
156      * @param parameters Input parameters from URL, e.g. on sensor reports
157      */
158     public void onEvent(String ipAddress, String deviceName, String componentIndex, String eventType,
159             Map<String, String> parameters) {
160         logger.trace("{}: Dispatch event to thing handler", deviceName);
161         for (ShellyBaseHandler listener : deviceListeners) {
162             if (listener.onEvent(ipAddress, deviceName, componentIndex, eventType, parameters)) {
163                 // event processed
164                 return;
165             }
166         }
167     }
168
169     public ShellyBindingConfiguration getBindingConfig() {
170         return bindingConfig;
171     }
172 }