]> git.basschouten.com Git - openhab-addons.git/blob
d1f7b22c06ef6bfafae758c4bae3d68d89b5b13b
[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 = Collections
53             .singleton(THING_TYPE_VACUUMCLEANER);
54
55     private Map<ThingUID, ServiceRegistration<DiscoveryService>> discoveryServiceRegistrations = new HashMap<>();
56
57     @Override
58     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59         return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
60     }
61
62     @Override
63     protected ThingHandler createHandler(Thing thing) {
64         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
65
66         if (thingTypeUID.equals(THING_TYPE_VACUUMCLEANER)) {
67             return new NeatoHandler(thing);
68         } else if (thingTypeUID.equals(BRIDGE_TYPE_NEATOACCOUNT)) {
69             NeatoAccountHandler handler = new NeatoAccountHandler((Bridge) thing);
70             registerAccountDiscoveryService(handler);
71             return handler;
72         }
73
74         return null;
75     }
76
77     @Override
78     protected void removeHandler(@NonNull ThingHandler thingHandler) {
79         ServiceRegistration<DiscoveryService> serviceRegistration = discoveryServiceRegistrations
80                 .get(thingHandler.getThing().getUID());
81
82         if (serviceRegistration != null) {
83             serviceRegistration.unregister();
84         }
85     }
86
87     private void registerAccountDiscoveryService(NeatoAccountHandler handler) {
88         NeatoAccountDiscoveryService discoveryService = new NeatoAccountDiscoveryService(handler);
89
90         ServiceRegistration<DiscoveryService> serviceRegistration = this.bundleContext
91                 .registerService(DiscoveryService.class, discoveryService, null);
92
93         discoveryServiceRegistrations.put(handler.getThing().getUID(), serviceRegistration);
94     }
95 }