Skip to content

Commit

Permalink
[astro] Handler factory activation simplified (openhab#7675)
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and andrewfg committed Aug 31, 2020
1 parent 1a474b1 commit afde495
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.astro.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
Expand All @@ -21,6 +22,7 @@
* @author Gerhard Riegler - Initial contribution
* @author Amit Kumar Mondal - Made non-Instantiable
*/
@NonNullByDefault
public final class AstroBindingConstants {

/** Constructor */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.scheduler.CronScheduler;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -31,6 +33,7 @@
import org.openhab.binding.astro.internal.handler.MoonHandler;
import org.openhab.binding.astro.internal.handler.SunHandler;
import org.openhab.binding.astro.internal.util.PropertyUtils;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand All @@ -39,22 +42,29 @@
*
* @author Gerhard Riegler - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class)
public class AstroHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
.concat(SunHandler.SUPPORTED_THING_TYPES.stream(), MoonHandler.SUPPORTED_THING_TYPES.stream())
.collect(Collectors.toSet());
private static final Map<String, AstroThingHandler> ASTRO_THING_HANDLERS = new HashMap<>();
private CronScheduler scheduler;
private final CronScheduler scheduler;

@Activate
public AstroHandlerFactory(final @Reference CronScheduler scheduler, final @Reference TimeZoneProvider timeZone) {
this.scheduler = scheduler;
PropertyUtils.setTimeZone(timeZone);
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES.contains(thingTypeUID);
}

@Override
protected ThingHandler createHandler(Thing thing) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
AstroThingHandler thingHandler = null;
if (thingTypeUID.equals(THING_TYPE_SUN)) {
Expand All @@ -74,25 +84,7 @@ public void unregisterHandler(Thing thing) {
ASTRO_THING_HANDLERS.remove(thing.getUID().toString());
}

@Reference
protected void setTimeZoneProvider(TimeZoneProvider timeZone) {
PropertyUtils.setTimeZone(timeZone);
}

protected void unsetTimeZoneProvider(TimeZoneProvider timeZone) {
PropertyUtils.unsetTimeZone();
}

public static AstroThingHandler getHandler(String thingUid) {
return ASTRO_THING_HANDLERS.get(thingUid);
}

@Reference
protected void setCronScheduler(CronScheduler scheduler) {
this.scheduler = scheduler;
}

protected void unsetCronScheduler(CronScheduler scheduler) {
this.scheduler = null;
}
}

0 comments on commit afde495

Please sign in to comment.