]> git.basschouten.com Git - openhab-addons.git/blob
4f5bb53a78e8cece8241ce3bcbd722f9b64bb048
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.HashMap;
18 import java.util.Map;
19 import java.util.Set;
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.ShellyManagerInterface;
29 import org.openhab.binding.shelly.internal.handler.ShellyProtectedHandler;
30 import org.openhab.binding.shelly.internal.handler.ShellyRelayHandler;
31 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
32 import org.openhab.binding.shelly.internal.handler.ShellyThingTable;
33 import org.openhab.binding.shelly.internal.provider.ShellyTranslationProvider;
34 import org.openhab.binding.shelly.internal.util.ShellyUtils;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.net.HttpServiceUtil;
37 import org.openhab.core.net.NetworkAddressService;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.service.component.ComponentContext;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Reference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import io.reactivex.annotations.NonNull;
51
52 /**
53  * The {@link ShellyHandlerFactory} is responsible for creating things and thing handlers.
54  *
55  * @author Markus Michels - Initial contribution
56  */
57 @NonNullByDefault
58 @Component(service = { ThingHandlerFactory.class, ShellyHandlerFactory.class }, configurationPid = "binding.shelly")
59 public class ShellyHandlerFactory extends BaseThingHandlerFactory {
60     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ShellyBindingConstants.SUPPORTED_THING_TYPES_UIDS;
61
62     private final Logger logger = LoggerFactory.getLogger(ShellyHandlerFactory.class);
63     private final HttpClient httpClient;
64     private final ShellyTranslationProvider messages;
65     private final ShellyCoapServer coapServer;
66
67     private final ShellyThingTable thingTable;
68     private ShellyBindingConfiguration bindingConfig = new ShellyBindingConfiguration();
69     private String localIP = "";
70     private int httpPort = -1;
71
72     /**
73      * Activate the bundle: save properties
74      *
75      * @param componentContext
76      * @param configProperties set of properties from cfg (use same names as in
77      *            thing config)
78      */
79     @Activate
80     public ShellyHandlerFactory(@Reference NetworkAddressService networkAddressService,
81             @Reference ShellyTranslationProvider translationProvider, @Reference ShellyThingTable thingTable,
82             @Reference HttpClientFactory httpClientFactory, ComponentContext componentContext,
83             Map<String, Object> configProperties) {
84         logger.debug("Activate Shelly HandlerFactory");
85         super.activate(componentContext);
86         messages = translationProvider;
87         // Save bindingConfig & pass it to all registered listeners
88         bindingConfig.updateFromProperties(configProperties);
89
90         localIP = bindingConfig.localIP;
91         if (localIP.isEmpty()) {
92             localIP = ShellyUtils.getString(networkAddressService.getPrimaryIpv4HostAddress());
93         }
94         if (localIP.isEmpty()) {
95             logger.warn("{}", messages.get("message.init.noipaddress"));
96         }
97
98         this.httpClient = httpClientFactory.getCommonHttpClient();
99         httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
100         if (httpPort == -1) {
101             httpPort = 8080;
102         }
103         logger.debug("Using OH HTTP port {}", httpPort);
104
105         this.thingTable = thingTable;
106         this.coapServer = new ShellyCoapServer();
107
108         // Promote Shelly Manager usage
109         logger.info("{}", messages.get("status.managerstarted", localIP, httpPort));
110     }
111
112     @Override
113     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
114         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
115     }
116
117     @Override
118     protected @Nullable ThingHandler createHandler(Thing thing) {
119         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
120         String thingType = thingTypeUID.getId();
121         ShellyBaseHandler handler = null;
122
123         if (thingType.equals(THING_TYPE_SHELLYPROTECTED_STR)) {
124             logger.debug("{}: Create new thing of type {} using ShellyProtectedHandler", thing.getLabel(),
125                     thingTypeUID.toString());
126             handler = new ShellyProtectedHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort,
127                     httpClient);
128         } else if (thingType.equals(THING_TYPE_SHELLYBULB_STR) || thingType.equals(THING_TYPE_SHELLYDUO_STR)
129                 || thingType.equals(THING_TYPE_SHELLYRGBW2_COLOR_STR)
130                 || thingType.equals(THING_TYPE_SHELLYRGBW2_WHITE_STR)
131                 || thingType.equals(THING_TYPE_SHELLYDUORGBW_STR)) {
132             logger.debug("{}: Create new thing of type {} using ShellyLightHandler", thing.getLabel(),
133                     thingTypeUID.toString());
134             handler = new ShellyLightHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort, httpClient);
135         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
136             logger.debug("{}: Create new thing of type {} using ShellyRelayHandler", thing.getLabel(),
137                     thingTypeUID.toString());
138             handler = new ShellyRelayHandler(thing, messages, bindingConfig, coapServer, localIP, httpPort, httpClient);
139         }
140
141         if (handler != null) {
142             String uid = thing.getUID().getAsString();
143             thingTable.addThing(uid, handler);
144             logger.debug("Thing handler for uid {} added, total things = {}", uid, thingTable.size());
145             return handler;
146         }
147
148         logger.debug("Unable to create Thing Handler instance!");
149         return null;
150     }
151
152     public Map<String, ShellyManagerInterface> getThingHandlers() {
153         Map<String, ShellyManagerInterface> table = new HashMap<>();
154         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.getTable().entrySet()) {
155             table.put(entry.getKey(), (ShellyManagerInterface) entry.getValue());
156         }
157         return table;
158     }
159
160     /**
161      * Remove handler of things.
162      */
163     @Override
164     protected synchronized void removeHandler(@NonNull ThingHandler thingHandler) {
165         if (thingHandler instanceof ShellyBaseHandler) {
166             String uid = thingHandler.getThing().getUID().getAsString();
167             thingTable.removeThing(uid);
168         }
169     }
170
171     /**
172      * Dispatch event to registered devices.
173      *
174      * @param deviceName
175      * @param componentIndex Index of component, e.g. 2 for relay2
176      * @param eventType Type of event, e.g. light
177      * @param parameters Input parameters from URL, e.g. on sensor reports
178      */
179     public void onEvent(String ipAddress, String deviceName, String componentIndex, String eventType,
180             Map<String, String> parameters) {
181         logger.trace("{}: Dispatch event to thing handler", deviceName);
182         for (Map.Entry<String, ShellyThingInterface> listener : thingTable.getTable().entrySet()) {
183             ShellyBaseHandler thingHandler = (ShellyBaseHandler) listener.getValue();
184             if (thingHandler.onEvent(ipAddress, deviceName, componentIndex, eventType, parameters)) {
185                 // event processed
186                 return;
187             }
188         }
189     }
190
191     public ShellyBindingConfiguration getBindingConfig() {
192         return bindingConfig;
193     }
194 }