2 * Copyright (c) 2010-2024 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.insteon.internal;
15 import static org.openhab.binding.insteon.internal.InsteonBindingConstants.*;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.insteon.internal.discovery.InsteonDeviceDiscoveryService;
28 import org.openhab.binding.insteon.internal.handler.InsteonDeviceHandler;
29 import org.openhab.binding.insteon.internal.handler.InsteonNetworkHandler;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.io.transport.serial.SerialPortManager;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerFactory;
39 import org.osgi.framework.ServiceRegistration;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
44 * The {@link InsteonHandlerFactory} is responsible for creating things and thing
47 * @author Rob Nielsen - Initial contribution
50 @Component(configurationPid = "binding.insteon", service = ThingHandlerFactory.class)
51 public class InsteonHandlerFactory extends BaseThingHandlerFactory {
53 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
54 .unmodifiableSet(Stream.of(DEVICE_THING_TYPE, NETWORK_THING_TYPE).collect(Collectors.toSet()));
56 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57 private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
59 private @Nullable SerialPortManager serialPortManager;
62 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
63 this.serialPortManager = serialPortManager;
66 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
67 this.serialPortManager = null;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (NETWORK_THING_TYPE.equals(thingTypeUID)) {
80 InsteonNetworkHandler insteonNetworkHandler = new InsteonNetworkHandler((Bridge) thing, serialPortManager);
81 registerServices(insteonNetworkHandler);
83 return insteonNetworkHandler;
84 } else if (DEVICE_THING_TYPE.equals(thingTypeUID)) {
85 return new InsteonDeviceHandler(thing);
92 protected synchronized void removeHandler(ThingHandler thingHandler) {
93 if (thingHandler instanceof InsteonNetworkHandler) {
94 ThingUID uid = thingHandler.getThing().getUID();
95 ServiceRegistration<?> serviceRegs = this.serviceRegs.remove(uid);
96 if (serviceRegs != null) {
97 serviceRegs.unregister();
100 ServiceRegistration<?> discoveryServiceRegs = this.discoveryServiceRegs.remove(uid);
101 if (discoveryServiceRegs != null) {
102 discoveryServiceRegs.unregister();
107 private synchronized void registerServices(InsteonNetworkHandler handler) {
108 this.serviceRegs.put(handler.getThing().getUID(),
109 bundleContext.registerService(InsteonNetworkHandler.class.getName(), handler, new Hashtable<>()));
111 InsteonDeviceDiscoveryService discoveryService = new InsteonDeviceDiscoveryService(handler);
112 this.discoveryServiceRegs.put(handler.getThing().getUID(),
113 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));