2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.astro.internal;
15 import static org.openhab.binding.astro.internal.AstroBindingConstants.*;
17 import java.util.HashMap;
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;
38 * The {@link AstroHandlerFactory} is responsible for creating things and thing handlers.
40 * @author Gerhard Riegler - Initial contribution
43 @Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class)
44 public class AstroHandlerFactory extends BaseThingHandlerFactory {
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;
52 public AstroHandlerFactory(final @Reference CronScheduler scheduler,
53 final @Reference TimeZoneProvider timeZoneProvider) {
54 this.scheduler = scheduler;
55 this.timeZoneProvider = timeZoneProvider;
59 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
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);
72 if (thingHandler != null) {
73 ASTRO_THING_HANDLERS.put(thing.getUID().toString(), thingHandler);
79 public void unregisterHandler(Thing thing) {
80 super.unregisterHandler(thing);
81 ASTRO_THING_HANDLERS.remove(thing.getUID().toString());
84 public static @Nullable AstroThingHandler getHandler(String thingUid) {
85 return ASTRO_THING_HANDLERS.get(thingUid);