2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.millheat.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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;
46 * The {@link MillheatHandlerFactory} is responsible for creating things and thing
49 * @author Arne Seime - Initial contribution
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<>();
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()));
63 public MillheatHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
64 this.httpClient = httpClientFactory.getCommonHttpClient();
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,
79 registerDeviceDiscoveryService(handler);
86 protected void removeHandler(ThingHandler thingHandler) {
87 if (thingHandler instanceof MillheatAccountHandler) {
88 ThingUID thingUID = thingHandler.getThing().getUID();
89 unregisterDeviceDiscoveryService(thingUID);
91 super.removeHandler(thingHandler);
95 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
96 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
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<>()));
105 private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
106 if (discoveryServiceRegs.containsKey(thingUID)) {
107 ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
108 serviceReg.unregister();
109 discoveryServiceRegs.remove(thingUID);