]> git.basschouten.com Git - openhab-addons.git/blob
fab8220b8bbf28bd133b3681d3dc6cf103a690e6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.astro.internal;
14
15 import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
26 import org.openhab.binding.astro.internal.handler.MoonHandler;
27 import org.openhab.binding.astro.internal.handler.SunHandler;
28 import org.openhab.core.i18n.TimeZoneProvider;
29 import org.openhab.core.scheduler.CronScheduler;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38
39 /**
40  * The {@link AstroHandlerFactory} is responsible for creating things and thing handlers.
41  *
42  * @author Gerhard Riegler - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class)
46 public class AstroHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
49             .concat(SunHandler.SUPPORTED_THING_TYPES.stream(), MoonHandler.SUPPORTED_THING_TYPES.stream())
50             .collect(Collectors.toSet());
51     private static final Map<String, AstroThingHandler> ASTRO_THING_HANDLERS = new HashMap<>();
52     private final CronScheduler scheduler;
53     private final TimeZoneProvider timeZoneProvider;
54
55     @Activate
56     public AstroHandlerFactory(final @Reference CronScheduler scheduler,
57             final @Reference TimeZoneProvider timeZoneProvider) {
58         this.scheduler = scheduler;
59         this.timeZoneProvider = timeZoneProvider;
60     }
61
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
65     }
66
67     @Override
68     protected @Nullable ThingHandler createHandler(Thing thing) {
69         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70         AstroThingHandler thingHandler = null;
71         if (thingTypeUID.equals(THING_TYPE_SUN)) {
72             thingHandler = new SunHandler(thing, scheduler, timeZoneProvider);
73         } else if (thingTypeUID.equals(THING_TYPE_MOON)) {
74             thingHandler = new MoonHandler(thing, scheduler, timeZoneProvider);
75         }
76         if (thingHandler != null) {
77             ASTRO_THING_HANDLERS.put(thing.getUID().toString(), thingHandler);
78         }
79         return thingHandler;
80     }
81
82     @Override
83     public void unregisterHandler(Thing thing) {
84         super.unregisterHandler(thing);
85         ASTRO_THING_HANDLERS.remove(thing.getUID().toString());
86     }
87
88     public static @Nullable AstroThingHandler getHandler(String thingUid) {
89         return ASTRO_THING_HANDLERS.get(thingUid);
90     }
91 }