Search in sources :

Example 21 with Channel

use of org.eclipse.smarthome.core.thing.Channel in project smarthome by eclipse.

the class AstroThingHandler method triggerEvent.

/**
 * Emits an event for the given channel.
 */
public void triggerEvent(String channelId, String event) {
    final Channel channel = getThing().getChannel(channelId);
    if (channel == null) {
        logger.warn("Event {} in thing {} does not exist, please recreate the thing", event, getThing().getUID());
        return;
    }
    triggerChannel(channel.getUID(), event);
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel)

Example 22 with Channel

use of org.eclipse.smarthome.core.thing.Channel in project smarthome by eclipse.

the class ConnectedBluetoothHandler method activateChannel.

protected void activateChannel(@Nullable BluetoothCharacteristic characteristic, ChannelTypeUID channelTypeUID, @Nullable String name) {
    if (characteristic != null) {
        String channelId = name != null ? name : characteristic.getGattCharacteristic().name();
        if (channelId == null) {
            // use the type id as a fallback
            channelId = channelTypeUID.getId();
        }
        if (getThing().getChannel(channelId) == null) {
            // the channel does not exist yet, so let's add it
            ThingBuilder updatedThing = editThing();
            Channel channel = ChannelBuilder.create(new ChannelUID(getThing().getUID(), channelId), "Number").withType(channelTypeUID).build();
            updatedThing.withChannel(channel);
            updateThing(updatedThing.build());
            logger.debug("Added channel '{}' to Thing '{}'", channelId, getThing().getUID());
        }
        deviceCharacteristics.add(characteristic);
        device.enableNotifications(characteristic);
        if (isLinked(channelId)) {
            device.readCharacteristic(characteristic);
        }
    } else {
        logger.debug("Characteristic is null - not activating any channel.");
    }
}
Also used : ThingBuilder(org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel)

Example 23 with Channel

use of org.eclipse.smarthome.core.thing.Channel 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 24 with Channel

use of org.eclipse.smarthome.core.thing.Channel 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)

Example 25 with Channel

use of org.eclipse.smarthome.core.thing.Channel in project smarthome by eclipse.

the class NtpOSGiTest method initialize.

private void initialize(Configuration configuration, String channelID, String acceptedItemType, Configuration channelConfiguration) {
    configuration.put(NtpBindingConstants.PROPERTY_NTP_SERVER_PORT, TEST_PORT);
    ThingUID ntpUid = new ThingUID(NtpBindingConstants.THING_TYPE_NTP, TEST_THING_ID);
    ChannelUID channelUID = new ChannelUID(ntpUid, channelID);
    Channel channel = ChannelBuilder.create(channelUID, acceptedItemType).withType(channelTypeUID).withConfiguration(channelConfiguration).withLabel("label").withKind(ChannelKind.STATE).build();
    ntpThing = ThingBuilder.create(NtpBindingConstants.THING_TYPE_NTP, ntpUid).withConfiguration(configuration).withChannel(channel).build();
    managedThingProvider.add(ntpThing);
    // Wait for the NTP thing to be added to the ManagedThingProvider.
    ntpHandler = waitForAssert(() -> {
        final ThingHandler thingHandler = ntpThing.getHandler();
        assertThat(thingHandler, is(instanceOf(NtpHandler.class)));
        return (NtpHandler) thingHandler;
    }, DFL_TIMEOUT * 3, DFL_SLEEP_TIME);
    if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_STRING)) {
        testItem = new StringItem(TEST_ITEM_NAME);
    } else if (acceptedItemType.equals(ACCEPTED_ITEM_TYPE_DATE_TIME)) {
        testItem = new DateTimeItem(TEST_ITEM_NAME);
    }
    itemRegistry.add(testItem);
    // Wait for the item , linked to the NTP thing to be added to the
    // ManagedThingProvider.
    final ManagedItemChannelLinkProvider itemChannelLinkProvider = waitForAssert(() -> {
        final ManagedItemChannelLinkProvider tmp = getService(ManagedItemChannelLinkProvider.class);
        assertNotNull(tmp);
        return tmp;
    });
    itemChannelLinkProvider.add(new ItemChannelLink(TEST_ITEM_NAME, channelUID));
}
Also used : ManagedItemChannelLinkProvider(org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ThingHandler(org.eclipse.smarthome.core.thing.binding.ThingHandler) NtpHandler(org.eclipse.smarthome.binding.ntp.handler.NtpHandler) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) StringItem(org.eclipse.smarthome.core.library.items.StringItem)

Aggregations

Channel (org.eclipse.smarthome.core.thing.Channel)36 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)16 ArrayList (java.util.ArrayList)8 Thing (org.eclipse.smarthome.core.thing.Thing)7 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)6 Nullable (org.eclipse.jdt.annotation.Nullable)5 Configuration (org.eclipse.smarthome.config.core.Configuration)5 Item (org.eclipse.smarthome.core.items.Item)4 ThingBuilder (org.eclipse.smarthome.core.thing.binding.builder.ThingBuilder)4 ItemChannelLink (org.eclipse.smarthome.core.thing.link.ItemChannelLink)4 List (java.util.List)3 Locale (java.util.Locale)3 NonNull (org.eclipse.jdt.annotation.NonNull)3 AstroChannelConfig (org.eclipse.smarthome.binding.astro.internal.config.AstroChannelConfig)3 ItemChannelLinkRegistry (org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry)3 ProfileTypeUID (org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID)3 ChannelType (org.eclipse.smarthome.core.thing.type.ChannelType)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Calendar (java.util.Calendar)2