]> git.basschouten.com Git - openhab-addons.git/blob
9e0d3c02b0e6cce617713f9b149b11a614d0b101
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 import java.util.concurrent.ConcurrentHashMap;
20
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;
46
47 import io.reactivex.annotations.NonNull;
48
49 /**
50  * The {@link ShellyHandlerFactory} is responsible for creating things and thing handlers.
51  *
52  * @author Markus Michels - Initial contribution
53  */
54 @NonNullByDefault
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;
66
67     /**
68      * Activate the bundle: save properties
69      *
70      * @param componentContext
71      * @param configProperties set of properties from cfg (use same names as in
72      *            thing config)
73      */
74     @Activate
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);
83
84         localIP = bindingConfig.localIP;
85         if (localIP.isEmpty()) {
86             localIP = ShellyUtils.getString(networkAddressService.getPrimaryIpv4HostAddress());
87         }
88         if (localIP.isEmpty()) {
89             logger.warn("{}", messages.get("message.init.noipaddress"));
90         }
91
92         this.httpClient = httpClientFactory.getCommonHttpClient();
93         httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
94         if (httpPort == -1) {
95             httpPort = 8080;
96         }
97         logger.debug("Using OH HTTP port {}", httpPort);
98
99         this.coapServer = new ShellyCoapServer();
100     }
101
102     @Override
103     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
105     }
106
107     @Override
108     protected @Nullable ThingHandler createHandler(Thing thing) {
109         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110         String thingType = thingTypeUID.getId();
111         ShellyBaseHandler handler = null;
112
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,
117                     httpClient);
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);
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 }