]> git.basschouten.com Git - openhab-addons.git/blob
6939d1dd013a7af0dc5c3e28bf9b3e50fa96a3e2
[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.millheat.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.millheat.internal.discovery.MillheatDiscoveryService;
27 import org.openhab.binding.millheat.internal.handler.MillheatAccountHandler;
28 import org.openhab.binding.millheat.internal.handler.MillheatHeaterHandler;
29 import org.openhab.binding.millheat.internal.handler.MillheatHomeHandler;
30 import org.openhab.binding.millheat.internal.handler.MillheatRoomHandler;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.Bridge;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.framework.ServiceRegistration;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44
45 /**
46  * The {@link MillheatHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Arne Seime - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(configurationPid = "binding.millheat", service = ThingHandlerFactory.class)
53 public class MillheatHandlerFactory extends BaseThingHandlerFactory {
54     private HttpClient httpClient;
55     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
56
57     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(Stream
58             .of(MillheatBindingConstants.THING_TYPE_ACCOUNT, MillheatBindingConstants.THING_TYPE_HEATER,
59                     MillheatBindingConstants.THING_TYPE_ROOM, MillheatBindingConstants.THING_TYPE_HOME)
60             .collect(Collectors.toSet()));
61
62     @Activate
63     public MillheatHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
64         this.httpClient = httpClientFactory.getCommonHttpClient();
65     }
66
67     @Override
68     protected @Nullable ThingHandler createHandler(final Thing thing) {
69         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70         if (MillheatBindingConstants.THING_TYPE_HEATER.equals(thingTypeUID)) {
71             return new MillheatHeaterHandler(thing);
72         } else if (MillheatBindingConstants.THING_TYPE_ROOM.equals(thingTypeUID)) {
73             return new MillheatRoomHandler(thing);
74         } else if (MillheatBindingConstants.THING_TYPE_HOME.equals(thingTypeUID)) {
75             return new MillheatHomeHandler(thing);
76         } else if (MillheatBindingConstants.THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
77             final MillheatAccountHandler handler = new MillheatAccountHandler((Bridge) thing, httpClient,
78                     bundleContext);
79             registerDeviceDiscoveryService(handler);
80             return handler;
81         }
82         return null;
83     }
84
85     @Override
86     protected void removeHandler(ThingHandler thingHandler) {
87         if (thingHandler instanceof MillheatAccountHandler) {
88             ThingUID thingUID = thingHandler.getThing().getUID();
89             unregisterDeviceDiscoveryService(thingUID);
90         }
91         super.removeHandler(thingHandler);
92     }
93
94     @Override
95     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
96         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
97     }
98
99     private void registerDeviceDiscoveryService(MillheatAccountHandler bridgeHandler) {
100         MillheatDiscoveryService discoveryService = new MillheatDiscoveryService(bridgeHandler);
101         discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
102                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
103     }
104
105     private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
106         if (discoveryServiceRegs.containsKey(thingUID)) {
107             ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
108             serviceReg.unregister();
109             discoveryServiceRegs.remove(thingUID);
110         }
111     }
112 }