2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.shelly.internal;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
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;
49 import io.reactivex.annotations.NonNull;
52 * The {@link ShellyHandlerFactory} is responsible for creating things and thing handlers.
54 * @author Markus Michels - Initial contribution
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;
70 * Activate the bundle: save properties
72 * @param componentContext
73 * @param configProperties set of properties from cfg (use same names as in
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);
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"));
90 this.httpClient = httpClientFactory.getCommonHttpClient();
91 httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
95 logger.debug("Using OH HTTP port {}", httpPort);
97 this.coapServer = new ShellyCoapServer();
99 // Save bindingConfig & pass it to all registered listeners
100 bindingConfig.updateFromProperties(configProperties);
104 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
105 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
109 protected @Nullable ThingHandler createHandler(Thing thing) {
110 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
111 String thingType = thingTypeUID.getId();
112 ShellyBaseHandler handler = null;
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,
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);
131 if (handler != null) {
132 deviceListeners.add(handler);
136 logger.debug("Unable to create Thing Handler instance!");
141 * Remove handler of things.
144 protected synchronized void removeHandler(@NonNull ThingHandler thingHandler) {
145 if (thingHandler instanceof ShellyBaseHandler) {
146 deviceListeners.remove(thingHandler);
151 * Dispatch event to registered devices.
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
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)) {
169 public ShellyBindingConfiguration getBindingConfig() {
170 return bindingConfig;