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