Search in sources :

Example 1 with AstroChannelConfig

use of org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig in project smarthome by eclipse.

the class AstroThingHandler method publishChannelIfLinked.

/**
 * Publishes the channel with data if it's linked.
 */
public void publishChannelIfLinked(ChannelUID channelUID) {
    if (isLinked(channelUID.getId()) && getPlanet() != null) {
        final Channel channel = getThing().getChannel(channelUID.getId());
        if (channel == null) {
            logger.error("Cannot find channel for {}", channelUID);
            return;
        }
        try {
            AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
            updateState(channelUID, PropertyUtils.getState(channelUID, config, getPlanet()));
        } catch (Exception ex) {
            logger.error("Can't update state for channel {} : {}", channelUID, ex.getMessage(), ex);
        }
    }
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig) ParseException(java.text.ParseException)

Example 2 with AstroChannelConfig

use of org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig in project smarthome by eclipse.

the class Job method scheduleRange.

/**
 * Schedules {@link Channel} events
 *
 * @param thingUID the {@link Thing} UID
 * @param astroHandler the {@link AstroThingHandler} instance
 * @param range the {@link Range} instance
 * @param channelId the channel ID
 */
public static void scheduleRange(String thingUID, AstroThingHandler astroHandler, Range range, String channelId) {
    boolean thingNull = checkNull(thingUID, "Thing UID is null");
    boolean astroHandlerNull = checkNull(astroHandler, "AstroThingHandler is null");
    boolean rangeNull = checkNull(range, "Range is null");
    boolean channelIdNull = checkNull(channelId, "Channel ID is null");
    if (thingNull || astroHandlerNull || rangeNull || channelIdNull) {
        return;
    }
    Calendar start = range.getStart();
    Calendar end = range.getEnd();
    // depending on the location you might not have a valid range for day/night, so skip the events:
    if (start == null || end == null) {
        return;
    }
    final Channel channel = astroHandler.getThing().getChannel(channelId);
    if (channel == null) {
        LOGGER.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
        return;
    }
    AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
    Calendar configStart = applyConfig(start, config);
    Calendar configEnd = applyConfig(end, config);
    if (truncatedEquals(configStart, configEnd, SECOND)) {
        scheduleEvent(thingUID, astroHandler, configStart, asList(EVENT_START, EVENT_END), channelId, true);
    } else {
        scheduleEvent(thingUID, astroHandler, configStart, EVENT_START, channelId, true);
        scheduleEvent(thingUID, astroHandler, configEnd, EVENT_END, channelId, true);
    }
}
Also used : Calendar(java.util.Calendar) Channel(org.eclipse.smarthome.core.thing.Channel) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig)

Example 3 with AstroChannelConfig

use of org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig in project smarthome by eclipse.

the class Job method scheduleEvent.

/**
 * Schedules an {@link EventJob} instance
 *
 * @param thingUID the Thing UID
 * @param astroHandler the {@link ThingHandler} instance
 * @param eventAt the {@link Calendar} instance denoting scheduled instant
 * @param events the event IDs to schedule
 * @param channelId the channel ID
 */
public static void scheduleEvent(String thingUID, AstroThingHandler astroHandler, Calendar eventAt, List<String> events, String channelId, boolean configAlreadyApplied) {
    boolean thingNull = checkNull(thingUID, "Thing UID is null");
    boolean astroHandlerNull = checkNull(astroHandler, "AstroThingHandler is null");
    boolean eventAtNull = checkNull(eventAt, "Scheduled Instant is null");
    boolean eventsNull = checkNull(events, "Events list is null");
    boolean channelIdNull = checkNull(channelId, "Channel ID is null");
    if (thingNull || astroHandlerNull || eventAtNull || eventsNull || channelIdNull || events.isEmpty()) {
        return;
    }
    final Calendar instant;
    if (!configAlreadyApplied) {
        final Channel channel = astroHandler.getThing().getChannel(channelId);
        if (channel == null) {
            LOGGER.warn("Cannot find channel '{}' for thing '{}'.", channelId, astroHandler.getThing().getUID());
            return;
        }
        AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class);
        instant = applyConfig(eventAt, config);
    } else {
        instant = eventAt;
    }
    List<Job> jobs = events.stream().map(e -> new EventJob(thingUID, channelId, e)).collect(toList());
    schedule(thingUID, astroHandler, new CompositeJob(thingUID, jobs), instant);
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) SECOND(java.util.Calendar.SECOND) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig) Logger(org.slf4j.Logger) DateTimeUtils(org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils) MethodHandles(java.lang.invoke.MethodHandles) Planet(org.eclipse.smarthome.binding.astro.internal.model.Planet) LoggerFactory(org.slf4j.LoggerFactory) AstroThingHandler(org.eclipse.smarthome.binding.astro.handler.AstroThingHandler) Collections.singletonList(java.util.Collections.singletonList) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Calendar(java.util.Calendar) Range(org.eclipse.smarthome.binding.astro.internal.model.Range) Arrays.asList(java.util.Arrays.asList) Thing(org.eclipse.smarthome.core.thing.Thing) Objects.isNull(java.util.Objects.isNull) DateUtils.truncatedEquals(org.apache.commons.lang.time.DateUtils.truncatedEquals) AstroBindingConstants(org.eclipse.smarthome.binding.astro.AstroBindingConstants) SunPhaseName(org.eclipse.smarthome.binding.astro.internal.model.SunPhaseName) Calendar(java.util.Calendar) Channel(org.eclipse.smarthome.core.thing.Channel) AstroChannelConfig(org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig)

Aggregations

AstroChannelConfig (org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig)3 Channel (org.eclipse.smarthome.core.thing.Channel)3 Calendar (java.util.Calendar)2 MethodHandles (java.lang.invoke.MethodHandles)1 ParseException (java.text.ParseException)1 Arrays.asList (java.util.Arrays.asList)1 SECOND (java.util.Calendar.SECOND)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Objects.isNull (java.util.Objects.isNull)1 Collectors.toList (java.util.stream.Collectors.toList)1 DateUtils.truncatedEquals (org.apache.commons.lang.time.DateUtils.truncatedEquals)1 AstroBindingConstants (org.eclipse.smarthome.binding.astro.AstroBindingConstants)1 AstroThingHandler (org.eclipse.smarthome.binding.astro.handler.AstroThingHandler)1 Planet (org.eclipse.smarthome.binding.astro.internal.model.Planet)1 Range (org.eclipse.smarthome.binding.astro.internal.model.Range)1 SunPhaseName (org.eclipse.smarthome.binding.astro.internal.model.SunPhaseName)1 DateTimeUtils (org.eclipse.smarthome.binding.astro.internal.util.DateTimeUtils)1 Thing (org.eclipse.smarthome.core.thing.Thing)1 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)1