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