]> git.basschouten.com Git - openhab-addons.git/blob
d31796854323bf3d224031c8d37323793e4031fb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal;
14
15 import static org.openhab.binding.insteon.internal.InsteonBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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;
42
43 /**
44  * The {@link InsteonHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Rob Nielsen - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(configurationPid = "binding.insteon", service = ThingHandlerFactory.class)
51 public class InsteonHandlerFactory extends BaseThingHandlerFactory {
52
53     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
54             .unmodifiableSet(Stream.of(DEVICE_THING_TYPE, NETWORK_THING_TYPE).collect(Collectors.toSet()));
55
56     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57     private final Map<ThingUID, ServiceRegistration<?>> serviceRegs = new HashMap<>();
58
59     private @Nullable SerialPortManager serialPortManager;
60
61     @Reference
62     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
63         this.serialPortManager = serialPortManager;
64     }
65
66     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
67         this.serialPortManager = null;
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         if (NETWORK_THING_TYPE.equals(thingTypeUID)) {
80             InsteonNetworkHandler insteonNetworkHandler = new InsteonNetworkHandler((Bridge) thing, serialPortManager);
81             registerServices(insteonNetworkHandler);
82
83             return insteonNetworkHandler;
84         } else if (DEVICE_THING_TYPE.equals(thingTypeUID)) {
85             return new InsteonDeviceHandler(thing);
86         }
87
88         return null;
89     }
90
91     @Override
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();
98             }
99
100             ServiceRegistration<?> discoveryServiceRegs = this.discoveryServiceRegs.remove(uid);
101             if (discoveryServiceRegs != null) {
102                 discoveryServiceRegs.unregister();
103             }
104         }
105     }
106
107     private synchronized void registerServices(InsteonNetworkHandler handler) {
108         this.serviceRegs.put(handler.getThing().getUID(),
109                 bundleContext.registerService(InsteonNetworkHandler.class.getName(), handler, new Hashtable<>()));
110
111         InsteonDeviceDiscoveryService discoveryService = new InsteonDeviceDiscoveryService(handler);
112         this.discoveryServiceRegs.put(handler.getThing().getUID(),
113                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
114     }
115 }