]> git.basschouten.com Git - openhab-addons.git/blob
3ec1275996e918724a7a83b6cd1abaf60b03279c
[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.harmonyhub.internal;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.List;
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.openhab.binding.harmonyhub.internal.discovery.HarmonyDeviceDiscoveryService;
31 import org.openhab.binding.harmonyhub.internal.handler.HarmonyDeviceHandler;
32 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.openhab.core.thing.type.ChannelGroupType;
43 import org.openhab.core.thing.type.ChannelGroupTypeProvider;
44 import org.openhab.core.thing.type.ChannelGroupTypeUID;
45 import org.openhab.core.thing.type.ChannelType;
46 import org.openhab.core.thing.type.ChannelTypeProvider;
47 import org.openhab.core.thing.type.ChannelTypeUID;
48 import org.osgi.framework.ServiceRegistration;
49 import org.osgi.service.component.annotations.Activate;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
52
53 /**
54  * The {@link HarmonyHubHandlerFactory} is responsible for creating things and thing
55  * handlers.
56  *
57  * @author Dan Cunningham - Initial contribution
58  * @author Wouter Born - Add null annotations
59  */
60 @NonNullByDefault
61 @Component(service = { ThingHandlerFactory.class, ChannelTypeProvider.class,
62         ChannelGroupTypeProvider.class }, configurationPid = "binding.harmonyhub")
63 public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
64         implements ChannelTypeProvider, ChannelGroupTypeProvider {
65
66     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
67             .concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
68                     HarmonyDeviceHandler.SUPPORTED_THING_TYPES_UIDS.stream())
69             .collect(Collectors.toSet());
70
71     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
72     private final HttpClient httpClient;
73
74     private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
75     private final List<ChannelGroupType> channelGroupTypes = new CopyOnWriteArrayList<>();
76
77     @Activate
78     public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
79         this.httpClient = httpClientFactory.getCommonHttpClient();
80     }
81
82     @Override
83     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
85     }
86
87     @Override
88     protected @Nullable ThingHandler createHandler(Thing thing) {
89         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
90
91         if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_HUB_THING_TYPE)) {
92             HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, this);
93             registerHarmonyDeviceDiscoveryService(harmonyHubHandler);
94             return harmonyHubHandler;
95         }
96
97         if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_DEVICE_THING_TYPE)) {
98             return new HarmonyDeviceHandler(thing, this);
99         }
100
101         return null;
102     }
103
104     @Override
105     protected synchronized void removeHandler(ThingHandler thingHandler) {
106         if (thingHandler instanceof HarmonyHubHandler) {
107             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
108             if (serviceReg != null) {
109                 serviceReg.unregister();
110             }
111         }
112     }
113
114     /**
115      * Adds HarmonyHubHandler to the discovery service to find Harmony Devices
116      *
117      * @param harmonyHubHandler
118      */
119     private synchronized void registerHarmonyDeviceDiscoveryService(HarmonyHubHandler harmonyHubHandler) {
120         HarmonyDeviceDiscoveryService discoveryService = new HarmonyDeviceDiscoveryService(harmonyHubHandler);
121         this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(),
122                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
123     }
124
125     @Override
126     public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
127         return channelTypes;
128     }
129
130     @Override
131     public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
132         for (ChannelType channelType : channelTypes) {
133             if (channelType.getUID().equals(channelTypeUID)) {
134                 return channelType;
135             }
136         }
137         return null;
138     }
139
140     @Override
141     public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
142             @Nullable Locale locale) {
143         for (ChannelGroupType channelGroupType : channelGroupTypes) {
144             if (channelGroupType.getUID().equals(channelGroupTypeUID)) {
145                 return channelGroupType;
146             }
147         }
148         return null;
149     }
150
151     @Override
152     public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
153         return channelGroupTypes;
154     }
155
156     public HttpClient getHttpClient() {
157         return this.httpClient;
158     }
159
160     public void addChannelType(ChannelType type) {
161         channelTypes.add(type);
162     }
163
164     public void removeChannelType(ChannelType type) {
165         channelTypes.remove(type);
166     }
167
168     public void removeChannelTypesForThing(ThingUID uid) {
169         List<ChannelType> removes = new ArrayList<>();
170         for (ChannelType c : channelTypes) {
171             if (c.getUID().getAsString().startsWith(uid.getAsString())) {
172                 removes.add(c);
173             }
174         }
175         channelTypes.removeAll(removes);
176     }
177 }