]> git.basschouten.com Git - openhab-addons.git/blob
26855346c74f5e2fbb2e62d7f8cc84992263ce43
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.amazonechocontrol.internal;
14
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Hashtable;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.amazonechocontrol.internal.discovery.AmazonEchoDiscovery;
28 import org.openhab.binding.amazonechocontrol.internal.discovery.SmartHomeDevicesDiscovery;
29 import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
30 import org.openhab.binding.amazonechocontrol.internal.handler.EchoHandler;
31 import org.openhab.binding.amazonechocontrol.internal.handler.FlashBriefingProfileHandler;
32 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.storage.Storage;
36 import org.openhab.core.storage.StorageService;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
42 import org.openhab.core.thing.binding.ThingHandler;
43 import org.openhab.core.thing.binding.ThingHandlerFactory;
44 import org.osgi.framework.ServiceRegistration;
45 import org.osgi.service.component.ComponentContext;
46 import org.osgi.service.component.annotations.Activate;
47 import org.osgi.service.component.annotations.Component;
48 import org.osgi.service.component.annotations.Reference;
49 import org.osgi.service.http.HttpService;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.gson.Gson;
54
55 /**
56  * The {@link AmazonEchoControlHandlerFactory} is responsible for creating things and thing
57  * handlers.
58  *
59  * @author Michael Geramb - Initial contribution
60  */
61 @Component(service = { ThingHandlerFactory.class,
62         AmazonEchoControlHandlerFactory.class }, configurationPid = "binding.amazonechocontrol")
63 @NonNullByDefault
64 public class AmazonEchoControlHandlerFactory extends BaseThingHandlerFactory {
65     private final Logger logger = LoggerFactory.getLogger(AmazonEchoControlHandlerFactory.class);
66     private final Map<ThingUID, List<ServiceRegistration<?>>> discoveryServiceRegistrations = new HashMap<>();
67
68     private final Set<AccountHandler> accountHandlers = new HashSet<>();
69     private final HttpService httpService;
70     private final StorageService storageService;
71     private final BindingServlet bindingServlet;
72     private final Gson gson;
73
74     @Activate
75     public AmazonEchoControlHandlerFactory(@Reference HttpService httpService,
76             @Reference StorageService storageService) {
77         this.storageService = storageService;
78         this.httpService = httpService;
79         this.gson = new Gson();
80         this.bindingServlet = new BindingServlet(httpService);
81     }
82
83     @Override
84     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
85         return SUPPORTED_ECHO_THING_TYPES_UIDS.contains(thingTypeUID)
86                 || SUPPORTED_SMART_HOME_THING_TYPES_UIDS.contains(thingTypeUID);
87     }
88
89     @Override
90     protected void deactivate(ComponentContext componentContext) {
91         bindingServlet.dispose();
92         super.deactivate(componentContext);
93     }
94
95     @Override
96     protected @Nullable ThingHandler createHandler(Thing thing) {
97         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
98
99         if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
100             Storage<String> storage = storageService.getStorage(thing.getUID().toString(),
101                     String.class.getClassLoader());
102             AccountHandler bridgeHandler = new AccountHandler((Bridge) thing, httpService, storage, gson);
103             accountHandlers.add(bridgeHandler);
104             registerDiscoveryService(bridgeHandler);
105             bindingServlet.addAccountThing(thing);
106             return bridgeHandler;
107         } else if (thingTypeUID.equals(THING_TYPE_FLASH_BRIEFING_PROFILE)) {
108             Storage<String> storage = storageService.getStorage(thing.getUID().toString(),
109                     String.class.getClassLoader());
110             return new FlashBriefingProfileHandler(thing, storage);
111         } else if (SUPPORTED_ECHO_THING_TYPES_UIDS.contains(thingTypeUID)) {
112             return new EchoHandler(thing, gson);
113         } else if (SUPPORTED_SMART_HOME_THING_TYPES_UIDS.contains(thingTypeUID)) {
114             return new SmartHomeDeviceHandler(thing, gson);
115         }
116         return null;
117     }
118
119     private synchronized void registerDiscoveryService(AccountHandler bridgeHandler) {
120         List<ServiceRegistration<?>> discoveryServiceRegistration = discoveryServiceRegistrations
121                 .computeIfAbsent(bridgeHandler.getThing().getUID(), k -> new ArrayList<>());
122         SmartHomeDevicesDiscovery smartHomeDevicesDiscovery = new SmartHomeDevicesDiscovery(bridgeHandler);
123         smartHomeDevicesDiscovery.activate();
124         discoveryServiceRegistration.add(bundleContext.registerService(DiscoveryService.class.getName(),
125                 smartHomeDevicesDiscovery, new Hashtable<>()));
126
127         AmazonEchoDiscovery discoveryService = new AmazonEchoDiscovery(bridgeHandler);
128         discoveryService.activate();
129         discoveryServiceRegistration.add(
130                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
131     }
132
133     @Override
134     protected synchronized void removeHandler(ThingHandler thingHandler) {
135         if (thingHandler instanceof AccountHandler) {
136             accountHandlers.remove(thingHandler);
137             BindingServlet bindingServlet = this.bindingServlet;
138             bindingServlet.removeAccountThing(thingHandler.getThing());
139
140             List<ServiceRegistration<?>> discoveryServiceRegistration = discoveryServiceRegistrations
141                     .remove(thingHandler.getThing().getUID());
142             if (discoveryServiceRegistration != null) {
143                 discoveryServiceRegistration.forEach(serviceReg -> {
144                     AbstractDiscoveryService service = (AbstractDiscoveryService) bundleContext
145                             .getService(serviceReg.getReference());
146                     serviceReg.unregister();
147                     if (service != null) {
148                         if (service instanceof AmazonEchoDiscovery) {
149                             ((AmazonEchoDiscovery) service).deactivate();
150                         } else if (service instanceof SmartHomeDevicesDiscovery) {
151                             ((SmartHomeDevicesDiscovery) service).deactivate();
152                         } else {
153                             logger.warn("Found unknown discovery-service instance: {}", service);
154                         }
155                     }
156                 });
157             }
158         }
159     }
160
161     public Set<AccountHandler> getAccountHandlers() {
162         return accountHandlers;
163     }
164 }