2 * Copyright (c) 2010-2021 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;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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;
40 * The {@link AstroHandlerFactory} is responsible for creating things and thing handlers.
42 * @author Gerhard Riegler - Initial contribution
45 @Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class)
46 public class AstroHandlerFactory extends BaseThingHandlerFactory {
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;
56 public AstroHandlerFactory(final @Reference CronScheduler scheduler,
57 final @Reference TimeZoneProvider timeZoneProvider) {
58 this.scheduler = scheduler;
59 this.timeZoneProvider = timeZoneProvider;
63 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
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);
76 if (thingHandler != null) {
77 ASTRO_THING_HANDLERS.put(thing.getUID().toString(), thingHandler);
83 public void unregisterHandler(Thing thing) {
84 super.unregisterHandler(thing);
85 ASTRO_THING_HANDLERS.remove(thing.getUID().toString());
88 public static @Nullable AstroThingHandler getHandler(String thingUid) {
89 return ASTRO_THING_HANDLERS.get(thingUid);