]> git.basschouten.com Git - openhab-addons.git/blob
ce2fa1798efa8c0c69d392dbf6ff5c9c8a9e8adc
[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.neato.internal;
14
15 import static org.openhab.binding.neato.internal.NeatoBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.openhab.binding.neato.internal.discovery.NeatoAccountDiscoveryService;
26 import org.openhab.binding.neato.internal.handler.NeatoAccountHandler;
27 import org.openhab.binding.neato.internal.handler.NeatoHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Component;
38
39 /**
40  * The {@link NeatoHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Patrik Wimnell - Initial contribution
44  * @author Jeff Lauterbach - Adding Bridge thing type
45  */
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neato")
47 public class NeatoHandlerFactory extends BaseThingHandlerFactory {
48
49     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
50             .unmodifiableSet(Stream.of(BRIDGE_TYPE_NEATOACCOUNT, THING_TYPE_VACUUMCLEANER).collect(Collectors.toSet()));
51
52     public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPE_UIDS = Set.of(THING_TYPE_VACUUMCLEANER);
53
54     private Map<ThingUID, ServiceRegistration<DiscoveryService>> discoveryServiceRegistrations = new HashMap<>();
55
56     @Override
57     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
58         return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
59     }
60
61     @Override
62     protected ThingHandler createHandler(Thing thing) {
63         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
64
65         if (thingTypeUID.equals(THING_TYPE_VACUUMCLEANER)) {
66             return new NeatoHandler(thing);
67         } else if (thingTypeUID.equals(BRIDGE_TYPE_NEATOACCOUNT)) {
68             NeatoAccountHandler handler = new NeatoAccountHandler((Bridge) thing);
69             registerAccountDiscoveryService(handler);
70             return handler;
71         }
72
73         return null;
74     }
75
76     @Override
77     protected void removeHandler(@NonNull ThingHandler thingHandler) {
78         ServiceRegistration<DiscoveryService> serviceRegistration = discoveryServiceRegistrations
79                 .get(thingHandler.getThing().getUID());
80
81         if (serviceRegistration != null) {
82             serviceRegistration.unregister();
83         }
84     }
85
86     private void registerAccountDiscoveryService(NeatoAccountHandler handler) {
87         NeatoAccountDiscoveryService discoveryService = new NeatoAccountDiscoveryService(handler);
88
89         ServiceRegistration<DiscoveryService> serviceRegistration = this.bundleContext
90                 .registerService(DiscoveryService.class, discoveryService, null);
91
92         discoveryServiceRegistrations.put(handler.getThing().getUID(), serviceRegistration);
93     }
94 }