Search in sources :

Example 1 with Channel

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

the class ThingResource method create.

/**
 * create a new Thing
 *
 * @param thingBean
 * @return Response holding the newly created Thing or error information
 */
@POST
@RolesAllowed({ Role.ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new thing and adds it to the registry.")
@ApiResponses(value = { @ApiResponse(code = 201, message = "Created", response = String.class), @ApiResponse(code = 400, message = "A uid must be provided, if no binding can create a thing of this type."), @ApiResponse(code = 409, message = "A thing with the same uid already exists.") })
public Response create(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @ApiParam(value = "thing data", required = true) ThingDTO thingBean) {
    final Locale locale = LocaleUtil.getLocale(language);
    ThingUID thingUID = thingBean.UID == null ? null : new ThingUID(thingBean.UID);
    ThingTypeUID thingTypeUID = new ThingTypeUID(thingBean.thingTypeUID);
    if (thingUID != null) {
        // check if a thing with this UID already exists
        Thing thing = thingRegistry.get(thingUID);
        if (thing != null) {
            // report a conflict
            return getThingResponse(Status.CONFLICT, thing, locale, "Thing " + thingUID.toString() + " already exists!");
        }
    }
    ThingUID bridgeUID = null;
    if (thingBean.bridgeUID != null) {
        bridgeUID = new ThingUID(thingBean.bridgeUID);
    }
    // turn the ThingDTO's configuration into a Configuration
    Configuration configuration = new Configuration(normalizeConfiguration(thingBean.configuration, thingTypeUID, thingUID));
    normalizeChannels(thingBean, thingUID);
    Thing thing = thingRegistry.createThingOfType(thingTypeUID, thingUID, bridgeUID, thingBean.label, configuration);
    if (thing != null) {
        if (thingBean.properties != null) {
            for (Entry<String, String> entry : thingBean.properties.entrySet()) {
                thing.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (thingBean.channels != null) {
            List<Channel> channels = new ArrayList<>();
            for (ChannelDTO channelDTO : thingBean.channels) {
                channels.add(ChannelDTOMapper.map(channelDTO));
            }
            ThingHelper.addChannelsToThing(thing, channels);
        }
        if (thingBean.location != null) {
            thing.setLocation(thingBean.location);
        }
    } else if (thingUID != null) {
        // if there wasn't any ThingFactory capable of creating the thing,
        // we create the Thing exactly the way we received it, i.e. we
        // cannot take its thing type into account for automatically
        // populating channels and properties.
        thing = ThingDTOMapper.map(thingBean);
    } else {
        return getThingResponse(Status.BAD_REQUEST, thing, locale, "A UID must be provided, since no binding can create the thing!");
    }
    thingRegistry.add(thing);
    return getThingResponse(Status.CREATED, thing, locale, null);
}
Also used : Locale(java.util.Locale) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) ArrayList(java.util.ArrayList) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with Channel

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

the class ThingResource method getLinkedItemsMap.

private Map<String, Set<String>> getLinkedItemsMap(Thing thing) {
    Map<String, Set<String>> linkedItemsMap = new HashMap<>();
    for (Channel channel : thing.getChannels()) {
        Set<String> linkedItems = itemChannelLinkRegistry.getLinkedItemNames(channel.getUID());
        linkedItemsMap.put(channel.getUID().getId(), linkedItems);
    }
    return linkedItemsMap;
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) Channel(org.eclipse.smarthome.core.thing.Channel)

Example 3 with Channel

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

the class WeatherUndergroundHandler method startAutomaticRefresh.

/**
 * Start the job refreshing the weather data
 */
private void startAutomaticRefresh() {
    if (refreshJob == null || refreshJob.isCancelled()) {
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    // Request new weather data to the Weather Underground service
                    updateWeatherData();
                    // Update all channels from the updated weather data
                    for (Channel channel : getThing().getChannels()) {
                        updateChannel(channel.getUID().getId());
                    }
                } catch (Exception e) {
                    logger.debug("Exception occurred during execution: {}", e.getMessage(), e);
                }
            }
        };
        WeatherUndergroundConfiguration config = getConfigAs(WeatherUndergroundConfiguration.class);
        int period = (config.refresh != null) ? config.refresh.intValue() : DEFAULT_REFRESH_PERIOD;
        refreshJob = scheduler.scheduleWithFixedDelay(runnable, 0, period, TimeUnit.MINUTES);
    }
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) WeatherUndergroundConfiguration(org.eclipse.smarthome.binding.weatherunderground.internal.config.WeatherUndergroundConfiguration)

Example 4 with Channel

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

the class CommunicationManager method handleEvent.

private <T extends Type> void handleEvent(String itemName, T type, String source, Function<@Nullable String, @Nullable List<Class<? extends T>>> acceptedTypesFunction, ProfileAction<T> action) {
    final Item item = getItem(itemName);
    if (item == null) {
        logger.debug("Received an event for item {} which does not exist", itemName);
        return;
    }
    itemChannelLinkRegistry.stream().filter(link -> {
        // all links for the item
        return link.getItemName().equals(itemName);
    }).filter(link -> {
        // make sure the command event is not sent back to its source
        return !link.getLinkedUID().toString().equals(source);
    }).forEach(link -> {
        ChannelUID channelUID = link.getLinkedUID();
        Thing thing = getThing(channelUID.getThingUID());
        if (thing != null) {
            Channel channel = thing.getChannel(channelUID.getId());
            if (channel != null) {
                @Nullable T convertedType = toAcceptedType(type, channel, acceptedTypesFunction);
                if (convertedType != null) {
                    if (thing.getHandler() != null) {
                        Profile profile = getProfile(link, item, thing);
                        action.handle(profile, thing, convertedType);
                    }
                } else {
                    logger.debug("Received event '{}' ({}) could not be converted to any type accepted by item '{}' ({})", type, type.getClass().getSimpleName(), itemName, item.getType());
                }
            } else {
                logger.debug("Received  event '{}' for non-existing channel '{}', not forwarding it to the handler", type, channelUID);
            }
        } else {
            logger.debug("Received  event '{}' for non-existing thing '{}', not forwarding it to the handler", type, channelUID.getThingUID());
        }
    });
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) Arrays(java.util.Arrays) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) ProfileFactory(org.eclipse.smarthome.core.thing.profiles.ProfileFactory) LoggerFactory(org.slf4j.LoggerFactory) Command(org.eclipse.smarthome.core.types.Command) ItemChannelLinkConfigDescriptionProvider(org.eclipse.smarthome.core.thing.internal.link.ItemChannelLinkConfigDescriptionProvider) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) UID(org.eclipse.smarthome.core.thing.UID) State(org.eclipse.smarthome.core.types.State) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ItemStateConverter(org.eclipse.smarthome.core.items.ItemStateConverter) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ThingEventFactory(org.eclipse.smarthome.core.thing.events.ThingEventFactory) List(java.util.List) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) Entry(java.util.Map.Entry) NonNull(org.eclipse.jdt.annotation.NonNull) SystemProfileFactory(org.eclipse.smarthome.core.thing.internal.profiles.SystemProfileFactory) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ItemCommandEvent(org.eclipse.smarthome.core.items.events.ItemCommandEvent) EventFilter(org.eclipse.smarthome.core.events.EventFilter) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) Function(java.util.function.Function) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) HashSet(java.util.HashSet) ProfileCallbackImpl(org.eclipse.smarthome.core.thing.internal.profiles.ProfileCallbackImpl) Component(org.osgi.service.component.annotations.Component) SafeCaller(org.eclipse.smarthome.core.common.SafeCaller) Type(org.eclipse.smarthome.core.types.Type) ProfileCallback(org.eclipse.smarthome.core.thing.profiles.ProfileCallback) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Logger(org.slf4j.Logger) Profile(org.eclipse.smarthome.core.thing.profiles.Profile) Item(org.eclipse.smarthome.core.items.Item) ItemStateEvent(org.eclipse.smarthome.core.items.events.ItemStateEvent) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ProfileAdvisor(org.eclipse.smarthome.core.thing.profiles.ProfileAdvisor) ChannelTriggeredEvent(org.eclipse.smarthome.core.thing.events.ChannelTriggeredEvent) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) Event(org.eclipse.smarthome.core.events.Event) ProfileTypeUID(org.eclipse.smarthome.core.thing.profiles.ProfileTypeUID) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) Item(org.eclipse.smarthome.core.items.Item) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) Channel(org.eclipse.smarthome.core.thing.Channel) Thing(org.eclipse.smarthome.core.thing.Thing) Nullable(org.eclipse.jdt.annotation.Nullable) StateProfile(org.eclipse.smarthome.core.thing.profiles.StateProfile) TriggerProfile(org.eclipse.smarthome.core.thing.profiles.TriggerProfile) Profile(org.eclipse.smarthome.core.thing.profiles.Profile)

Example 5 with Channel

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

the class ThingDTOMapper method map.

/**
 * Maps thing into thing data transfer object (DTO).
 *
 * @param thing the thing
 * @return the thing DTO object
 */
public static ThingDTO map(Thing thing) {
    List<ChannelDTO> channelDTOs = new ArrayList<>();
    for (Channel channel : thing.getChannels()) {
        ChannelDTO channelDTO = ChannelDTOMapper.map(channel);
        channelDTOs.add(channelDTO);
    }
    String thingTypeUID = thing.getThingTypeUID().getAsString();
    String thingUID = thing.getUID().toString();
    final ThingUID bridgeUID = thing.getBridgeUID();
    return new ThingDTO(thingTypeUID, thingUID, thing.getLabel(), bridgeUID != null ? bridgeUID.toString() : null, channelDTOs, toMap(thing.getConfiguration()), thing.getProperties(), thing.getLocation());
}
Also used : Channel(org.eclipse.smarthome.core.thing.Channel) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ArrayList(java.util.ArrayList)

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