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.neohub.internal;
15 import static org.openhab.binding.neohub.internal.NeoHubBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.io.net.http.WebSocketFactory;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.framework.ServiceRegistration;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link NeoHubHandlerFactory} creates things and thing handlers
41 * @author Andrew Fiddian-Green - Initial contribution
44 @Component(configurationPid = "binding.neohub", service = ThingHandlerFactory.class)
45 public class NeoHubHandlerFactory extends BaseThingHandlerFactory {
47 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_NEOHUB, THING_TYPE_NEOSTAT,
48 THING_TYPE_NEOPLUG, THING_TYPE_NEOCONTACT, THING_TYPE_NEOTEMPERATURESENSOR);
50 private final WebSocketFactory webSocketFactory;
51 private final Map<ThingUID, ServiceRegistration<?>> discoServices = new HashMap<>();
54 public NeoHubHandlerFactory(final @Reference WebSocketFactory webSocketFactory) {
55 this.webSocketFactory = webSocketFactory;
59 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
64 protected @Nullable ThingHandler createHandler(Thing thing) {
65 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
67 if ((thingTypeUID.equals(THING_TYPE_NEOHUB)) && (thing instanceof Bridge bridge)) {
68 NeoHubHandler handler = new NeoHubHandler(bridge, webSocketFactory);
69 createDiscoveryService(handler);
73 if (thingTypeUID.equals(THING_TYPE_NEOSTAT)) {
74 return new NeoStatHandler(thing);
77 if (thingTypeUID.equals(THING_TYPE_NEOPLUG)) {
78 return new NeoPlugHandler(thing);
81 if (thingTypeUID.equals(THING_TYPE_NEOCONTACT)) {
82 return new NeoContactHandler(thing);
85 if (thingTypeUID.equals(THING_TYPE_NEOTEMPERATURESENSOR)) {
86 return new NeoTemperatureSensorHandler(thing);
93 protected synchronized void removeHandler(ThingHandler handler) {
94 if (handler instanceof NeoHubHandler neoHubHandler) {
95 destroyDiscoveryService(neoHubHandler);
100 * create a discovery service so that a newly created hub will find the
101 * respective things tht are inside it
103 private synchronized void createDiscoveryService(NeoHubHandler handler) {
104 // create a new discovery service
105 NeoHubDiscoveryService ds = new NeoHubDiscoveryService(handler);
107 // activate the discovery service
110 // register the discovery service
111 ServiceRegistration<?> serviceReg = bundleContext.registerService(DiscoveryService.class.getName(), ds,
115 * store service registration in a list so we can destroy it when the respective
118 discoServices.put(handler.getThing().getUID(), serviceReg);
122 * destroy the discovery service
124 private synchronized void destroyDiscoveryService(NeoHubHandler handler) {
125 // fetch the respective thing's service registration from our list
126 ServiceRegistration<?> serviceReg = discoServices.remove(handler.getThing().getUID());
128 if (serviceReg != null) {
129 // retrieve the respective discovery service
130 NeoHubDiscoveryService disco = (NeoHubDiscoveryService) bundleContext.getService(serviceReg.getReference());
132 // and unregister the service
133 serviceReg.unregister();
135 // deactivate the service