]> git.basschouten.com Git - openhab-addons.git/blob
d5c4e32900f2e360d586a693375d3068ec12a8d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.discovery.ShellyThingCreator.*;
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.api1.Shelly1CoapServer;
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 Shelly1CoapServer coapServer;
66     private final ShellyThingTable thingTable;
67     private ShellyBindingConfiguration bindingConfig = new ShellyBindingConfiguration();
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 ShellyTranslationProvider translationProvider, @Reference ShellyThingTable thingTable,
79             @Reference HttpClientFactory httpClientFactory, ComponentContext componentContext,
80             Map<String, Object> configProperties) {
81         super.activate(componentContext);
82         this.messages = translationProvider;
83         this.thingTable = thingTable;
84
85         bindingConfig.updateFromProperties(configProperties);
86         String localIP = bindingConfig.localIP;
87         if (localIP.isEmpty()) {
88             localIP = ShellyUtils.getString(networkAddressService.getPrimaryIpv4HostAddress());
89         }
90         if (localIP.isEmpty()) {
91             logger.warn("{}", messages.get("message.init.noipaddress"));
92         }
93
94         this.httpClient = httpClientFactory.getCommonHttpClient();
95         int httpPort = HttpServiceUtil.getHttpServicePort(componentContext.getBundleContext());
96         if (httpPort == -1) {
97             httpPort = 8080;
98         }
99         logger.debug("Using OH HTTP port {}", httpPort);
100         bindingConfig.localIP = localIP;
101         bindingConfig.httpPort = httpPort;
102
103         this.coapServer = new Shelly1CoapServer();
104     }
105
106     @Override
107     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
108         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
109     }
110
111     @Override
112     protected @Nullable ThingHandler createHandler(Thing thing) {
113         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
114         String thingType = thingTypeUID.getId();
115         ShellyBaseHandler handler = null;
116
117         if (thingType.equals(THING_TYPE_SHELLYPROTECTED_STR)) {
118             logger.debug("{}: Create new thing of type {} using ShellyProtectedHandler", thing.getLabel(),
119                     thingTypeUID.toString());
120             handler = new ShellyProtectedHandler(thing, messages, bindingConfig, thingTable, coapServer, httpClient);
121         } else if (thingType.equals(THING_TYPE_SHELLYBULB_STR) || thingType.equals(THING_TYPE_SHELLYDUO_STR)
122                 || thingType.equals(THING_TYPE_SHELLYRGBW2_COLOR_STR)
123                 || thingType.equals(THING_TYPE_SHELLYRGBW2_WHITE_STR)
124                 || thingType.equals(THING_TYPE_SHELLYRGBW2_WHITE_STR) || thingType.equals(THING_TYPE_SHELLYDUORGBW_STR)
125                 || thingType.equals(THING_TYPE_SHELLYVINTAGE_STR)) {
126             logger.debug("{}: Create new thing of type {} using ShellyLightHandler", thing.getLabel(),
127                     thingTypeUID.toString());
128             handler = new ShellyLightHandler(thing, messages, bindingConfig, thingTable, coapServer, httpClient);
129         } else if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
130             logger.debug("{}: Create new thing of type {} using ShellyRelayHandler", thing.getLabel(),
131                     thingTypeUID.toString());
132             handler = new ShellyRelayHandler(thing, messages, bindingConfig, thingTable, coapServer, httpClient);
133         }
134
135         if (handler != null) {
136             String uid = thing.getUID().getAsString();
137             thingTable.addThing(uid, handler);
138             logger.debug("Thing handler for uid {} added, total things = {}", uid, thingTable.size());
139             return handler;
140         }
141
142         logger.debug("Unable to create Thing Handler instance!");
143         return null;
144     }
145
146     public Map<String, ShellyManagerInterface> getThingHandlers() {
147         Map<String, ShellyManagerInterface> table = new HashMap<>();
148         for (Map.Entry<String, ShellyThingInterface> entry : thingTable.getTable().entrySet()) {
149             table.put(entry.getKey(), (ShellyManagerInterface) entry.getValue());
150         }
151         return table;
152     }
153
154     /**
155      * Remove handler of things.
156      */
157     @Override
158     protected synchronized void removeHandler(@NonNull ThingHandler thingHandler) {
159         if (thingHandler instanceof ShellyBaseHandler) {
160             String uid = thingHandler.getThing().getUID().getAsString();
161             thingTable.removeThing(uid);
162         }
163     }
164
165     /**
166      * Dispatch event to registered devices.
167      *
168      * @param deviceName
169      * @param componentIndex Index of component, e.g. 2 for relay2
170      * @param eventType Type of event, e.g. light
171      * @param parameters Input parameters from URL, e.g. on sensor reports
172      */
173     public void onEvent(String ipAddress, String deviceName, String componentIndex, String eventType,
174             Map<String, String> parameters) {
175         logger.trace("{}: Dispatch event to thing handler", deviceName);
176         for (Map.Entry<String, ShellyThingInterface> listener : thingTable.getTable().entrySet()) {
177             ShellyBaseHandler thingHandler = (ShellyBaseHandler) listener.getValue();
178             if (thingHandler.onEvent(ipAddress, deviceName, componentIndex, eventType, parameters)) {
179                 // event processed
180                 return;
181             }
182         }
183     }
184
185     public ShellyBindingConfiguration getBindingConfig() {
186         return bindingConfig;
187     }
188 }