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);
}
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;
}
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);
}
}
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());
}
});
}
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());
}
Aggregations