2 * Copyright (c) 2010-2021 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.*;
19 import java.util.concurrent.ConcurrentHashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
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.provider.ShellyTranslationProvider;
31 import org.openhab.binding.shelly.internal.util.ShellyUtils;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.net.HttpServiceUtil;
34 import org.openhab.core.net.NetworkAddressService;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.ComponentContext;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import io.reactivex.annotations.NonNull;
50 * The {@link ShellyHandlerFactory} is responsible for creating things and thing handlers.
52 * @author Markus Michels - Initial contribution
55 @Component(service = { ThingHandlerFactory.class, ShellyHandlerFactory.class }, configurationPid = "binding.shelly")
56 public class ShellyHandlerFactory extends BaseThingHandlerFactory {
57 private final Logger logger = LoggerFactory.getLogger(ShellyHandlerFactory.class);
58 private final HttpClient httpClient;
59 private final ShellyTranslationProvider messages;
60 private final ShellyCoapServer coapServer;
61 private final Set<ShellyBaseHandler> deviceListeners = ConcurrentHashMap.newKeySet();
62 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ShellyBindingConstants.SUPPORTED_THING_TYPES_UIDS;
63 private ShellyBindingConfiguration bindingConfig = new ShellyBindingConfiguration();
64 private String localIP = "";
65 private int httpPort = -1;
68 * Activate the bundle: save properties
70 * @param componentContext
71 * @param configProperties set of properties from cfg (use same names as in
75 public ShellyHandlerFactory(@Reference NetworkAddressService networkAddressService,
76 @Reference ShellyTranslationProvider translationProvider, @Reference HttpClientFactory httpClientFactory,
77 ComponentContext componentContext, Map<String, Object> configProperties) {
78 logger.debug("Activate Shelly HandlerFactory");
79 super.activate(componentContext);
80 messages = translationProvider;
81 // Save bindingConfig & pass it to all registered listeners
82 bindingConfig.updateFromProperties(configProperties);
84 localIP = bindingConfig.localIP;
85 if (localIP.isEmpty()) {
86 localIP = ShellyUtils.getString(networkAddressService.getPrimaryIpv4HostAddress());
88 if (localIP.isEmpty()) {
89 logger.warn("{}", messages.get("message.init.noipaddress"));
92 this.httpClient = httpClientFactory.getCommonHttpClient();
93 httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
97 logger.debug("Using OH HTTP port {}", httpPort);
99 this.coapServer = new ShellyCoapServer();
103 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
108 protected @Nullable ThingHandler createHandler(Thing thing) {
109 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110 String thingType = thingTypeUID.getId();
111 ShellyBaseHandler handler = null;
113 if (thingType.equals(THING_TYPE_SHELLYPROTECTED_STR)) {
114 logger.debug("{}: Create new thing of type {} using ShellyProtectedHandler", thing.getLabel(),
115 thingTypeUID.toString());
116 handler = new ShellyProtectedHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort,
118 } else if (thingType.equals(THING_TYPE_SHELLYBULB_STR) || thingType.equals(THING_TYPE_SHELLYDUO_STR)
119 || thingType.equals(THING_TYPE_SHELLYRGBW2_COLOR_STR)
120 || thingType.equals(THING_TYPE_SHELLYRGBW2_WHITE_STR)
121 || thingType.equals(THING_TYPE_SHELLYDUORGBW_STR)) {
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;