Search in sources :

Example 1 with Thing

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

the class AutomaticInboxProcessor method thingAdded.

@Override
public void thingAdded(Inbox inbox, DiscoveryResult result) {
    if (autoIgnore) {
        String value = getRepresentationValue(result);
        if (value != null) {
            Thing thing = thingRegistry.stream().filter(t -> Objects.equals(value, getRepresentationPropertyValueForThing(t))).filter(t -> Objects.equals(t.getThingTypeUID(), result.getThingTypeUID())).findFirst().orElse(null);
            if (thing != null) {
                logger.debug("Auto-ignoring the inbox entry for the representation value {}", value);
                inbox.setFlag(result.getThingUID(), DiscoveryResultFlag.IGNORED);
            }
        }
    }
    if (autoApprove) {
        inbox.approve(result.getThingUID(), result.getLabel());
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) InboxListener(org.eclipse.smarthome.config.discovery.inbox.InboxListener) EventSubscriber(org.eclipse.smarthome.core.events.EventSubscriber) RegistryChangeListener(org.eclipse.smarthome.core.common.registry.RegistryChangeListener) ThingStatusInfoChangedEvent(org.eclipse.smarthome.core.thing.events.ThingStatusInfoChangedEvent) Component(org.osgi.service.component.annotations.Component) Inbox(org.eclipse.smarthome.config.discovery.inbox.Inbox) Nullable(org.eclipse.jdt.annotation.Nullable) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) Configuration(org.eclipse.smarthome.config.core.Configuration) InboxPredicates(org.eclipse.smarthome.config.discovery.inbox.InboxPredicates) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) Logger(org.slf4j.Logger) Collectors(java.util.stream.Collectors) ThingTypeRegistry(org.eclipse.smarthome.core.thing.type.ThingTypeRegistry) Objects(java.util.Objects) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) List(java.util.List) DiscoveryResultFlag(org.eclipse.smarthome.config.discovery.DiscoveryResultFlag) AbstractTypedEventSubscriber(org.eclipse.smarthome.core.events.AbstractTypedEventSubscriber) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) Reference(org.osgi.service.component.annotations.Reference) DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) ThingStatus(org.eclipse.smarthome.core.thing.ThingStatus) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 2 with Thing

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

the class PersistentInbox method add.

@Override
public synchronized boolean add(DiscoveryResult result) throws IllegalStateException {
    if (result != null) {
        ThingUID thingUID = result.getThingUID();
        Thing thing = this.thingRegistry.get(thingUID);
        if (thing == null) {
            DiscoveryResult inboxResult = get(thingUID);
            if (inboxResult == null) {
                discoveryResultStorage.put(result.getThingUID().toString(), result);
                notifyListeners(result, EventType.added);
                logger.info("Added new thing '{}' to inbox.", thingUID);
                return true;
            } else {
                if (inboxResult instanceof DiscoveryResultImpl) {
                    DiscoveryResultImpl resultImpl = (DiscoveryResultImpl) inboxResult;
                    resultImpl.synchronize(result);
                    discoveryResultStorage.put(result.getThingUID().toString(), resultImpl);
                    notifyListeners(resultImpl, EventType.updated);
                    logger.debug("Updated discovery result for '{}'.", thingUID);
                    return true;
                } else {
                    logger.warn("Cannot synchronize result with implementation class '{}'.", inboxResult.getClass().getName());
                }
            }
        } else {
            logger.debug("Discovery result with thing '{}' not added as inbox entry." + " It is already present as thing in the ThingRegistry.", thingUID);
            boolean updated = synchronizeConfiguration(result.getThingTypeUID(), result.getProperties(), thing.getConfiguration());
            if (updated) {
                logger.debug("The configuration for thing '{}' is updated...", thingUID);
                this.managedThingProvider.update(thing);
            }
        }
    }
    return false;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) InboxPredicates.forThingUID(org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 3 with Thing

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

the class PersistentInbox method approve.

@Override
public Thing approve(ThingUID thingUID, String label) {
    if (thingUID == null) {
        throw new IllegalArgumentException("Thing UID must not be null");
    }
    List<DiscoveryResult> results = stream().filter(forThingUID(thingUID)).collect(Collectors.toList());
    if (results.isEmpty()) {
        throw new IllegalArgumentException("No Thing with UID " + thingUID.getAsString() + " in inbox");
    }
    DiscoveryResult result = results.get(0);
    final Map<String, String> properties = new HashMap<>();
    final Map<String, Object> configParams = new HashMap<>();
    getPropsAndConfigParams(result, properties, configParams);
    final Configuration config = new Configuration(configParams);
    ThingTypeUID thingTypeUID = result.getThingTypeUID();
    Thing newThing = ThingFactory.createThing(thingUID, config, properties, result.getBridgeUID(), thingTypeUID, this.thingHandlerFactories);
    if (newThing == null) {
        logger.warn("Cannot create thing. No binding found that supports creating a thing" + " of type {}.", thingTypeUID);
        return null;
    }
    if (label != null && !label.isEmpty()) {
        newThing.setLabel(label);
    } else {
        newThing.setLabel(result.getLabel());
    }
    addThingSafely(newThing);
    return newThing;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) Configuration(org.eclipse.smarthome.config.core.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing)

Example 4 with Thing

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

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

the class ThingResource method getConfigStatus.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{thingUID}/config/status")
@ApiOperation(value = "Gets thing's config status.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 404, message = "Thing not found.") })
public Response getConfigStatus(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID) throws IOException {
    ThingUID thingUIDObject = new ThingUID(thingUID);
    // Check if the Thing exists, 404 if not
    Thing thing = thingRegistry.get(thingUIDObject);
    if (null == thing) {
        logger.info("Received HTTP GET request for thing config status at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingNotFoundResponse(thingUID);
    }
    ConfigStatusInfo info = configStatusService.getConfigStatus(thingUID, LocaleUtil.getLocale(language));
    if (info != null) {
        return Response.ok(null, MediaType.TEXT_PLAIN).entity(info.getConfigStatusMessages()).build();
    }
    return Response.ok(null, MediaType.TEXT_PLAIN).entity(Collections.EMPTY_SET).build();
}
Also used : ConfigStatusInfo(org.eclipse.smarthome.config.core.status.ConfigStatusInfo) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Thing (org.eclipse.smarthome.core.thing.Thing)98 Test (org.junit.Test)43 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)28 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)24 Configuration (org.eclipse.smarthome.config.core.Configuration)19 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)19 JavaTest (org.eclipse.smarthome.test.java.JavaTest)18 ThingHandler (org.eclipse.smarthome.core.thing.binding.ThingHandler)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)13 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Item (org.eclipse.smarthome.core.items.Item)9 Path (javax.ws.rs.Path)8 Nullable (org.eclipse.jdt.annotation.Nullable)8 Locale (java.util.Locale)7 RolesAllowed (javax.annotation.security.RolesAllowed)7 Channel (org.eclipse.smarthome.core.thing.Channel)7 ThingHandlerCallback (org.eclipse.smarthome.core.thing.binding.ThingHandlerCallback)7 Command (org.eclipse.smarthome.core.types.Command)7 List (java.util.List)6