/** * Merges the content of the specified source {@link DiscoveryResult} into this object. * <p> * <i>Hint:</i> The {@link DiscoveryResultFlag} of this object keeps its state. * <p> * This method returns silently if the specified source {@link DiscoveryResult} is {@code null} or its {@code Thing} * type or ID does not fit to this object. * * @param sourceResult the discovery result which is used as source for the merge */ public void synchronize(DiscoveryResult sourceResult) { if ((sourceResult != null) && (sourceResult.getThingUID().equals(this.thingUID))) { this.properties = sourceResult.getProperties(); this.representationProperty = sourceResult.getRepresentationProperty(); this.label = sourceResult.getLabel(); this.timestamp = new Date().getTime(); this.timeToLive = sourceResult.getTimeToLive(); } }
/** * Maps discovery result into discovery result data transfer object. * * @param discoveryResult the discovery result * @return the discovery result data transfer object */ public static DiscoveryResultDTO map(DiscoveryResult discoveryResult) { ThingUID thingUID = discoveryResult.getThingUID(); ThingUID bridgeUID = discoveryResult.getBridgeUID(); return new DiscoveryResultDTO(thingUID.toString(), bridgeUID != null ? bridgeUID.toString() : null, discoveryResult.getThingTypeUID().toString(), discoveryResult.getLabel(), discoveryResult.getFlag(), discoveryResult.getProperties(), discoveryResult.getRepresentationProperty()); }
public static Predicate<DiscoveryResult> withBindingId(@Nullable String bindingId) { return r -> bindingId != null && bindingId.equals(r.getBindingId()); } }
private boolean isResultExpired(DiscoveryResult result, long now) { if (result.getTimeToLive() == DiscoveryResult.TTL_UNLIMITED) { return false; } return (result.getTimestamp() + result.getTimeToLive() * 1000 < now); } }
private @Nullable String getRepresentationValue(DiscoveryResult result) { return result.getRepresentationProperty() != null ? Objects.toString(result.getProperties().get(result.getRepresentationProperty()), null) : null; }
private void printInboxEntries(Console console, List<DiscoveryResult> discoveryResults) { if (discoveryResults.isEmpty()) { console.println("No inbox entries found."); } for (DiscoveryResult discoveryResult : discoveryResults) { ThingTypeUID thingTypeUID = discoveryResult.getThingTypeUID(); ThingUID thingUID = discoveryResult.getThingUID(); String label = discoveryResult.getLabel(); DiscoveryResultFlag flag = discoveryResult.getFlag(); ThingUID bridgeId = discoveryResult.getBridgeUID(); Map<String, Object> properties = discoveryResult.getProperties(); String representationProperty = discoveryResult.getRepresentationProperty(); String timestamp = new Date(discoveryResult.getTimestamp()).toString(); String timeToLive = discoveryResult.getTimeToLive() == DiscoveryResult.TTL_UNLIMITED ? "UNLIMITED" : "" + discoveryResult.getTimeToLive(); console.println(String.format( "%s [%s]: %s [thingId=%s, bridgeId=%s, properties=%s, representationProperty=%s, timestamp=%s, timeToLive=%s]", flag.name(), thingTypeUID, label, thingUID, bridgeId, properties, representationProperty, timestamp, timeToLive)); } }
private String inferKey(DiscoveryResult discoveryResult, String lastSegment) { return "discovery." + discoveryResult.getThingUID().getAsString().replaceAll(":", ".") + "." + lastSegment; }
@Override public @Nullable Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) { HashSet<ThingUID> removedThings = new HashSet<>(); for (DiscoveryResult discoveryResult : getAll()) { Class<?> discoverer = resultDiscovererMap.get(discoveryResult); if (thingTypeUIDs != null && thingTypeUIDs.contains(discoveryResult.getThingTypeUID()) && discoveryResult.getTimestamp() < timestamp && (discoverer == null || source.getClass() == discoverer)) { ThingUID thingUID = discoveryResult.getThingUID(); if (bridgeUID == null || bridgeUID.equals(discoveryResult.getBridgeUID())) { removedThings.add(thingUID); remove(thingUID); logger.debug("Removed {} from inbox because it was older than {}", thingUID, new Date(timestamp)); } } } return removedThings; }
private void approveAllInboxEntries() { for (DiscoveryResult result : inbox.getAll()) { if (result.getFlag().equals(DiscoveryResultFlag.NEW)) { inbox.approve(result.getThingUID(), result.getLabel()); } } }
private boolean matchFilter(DiscoveryResult discoveryResult, InboxFilterCriteria criteria) { if (criteria != null) { String bindingId = criteria.getBindingId(); if ((bindingId != null) && (!bindingId.isEmpty())) { if (!discoveryResult.getBindingId().equals(bindingId)) { return false; } } ThingTypeUID thingTypeUID = criteria.getThingTypeUID(); if (thingTypeUID != null) { if (!discoveryResult.getThingTypeUID().equals(thingTypeUID)) { return false; } } ThingUID thingUID = criteria.getThingUID(); if (thingUID != null) { if (!discoveryResult.getThingUID().equals(thingUID)) { return false; } } DiscoveryResultFlag flag = criteria.getFlag(); if (flag != null) { if (discoveryResult.getFlag() != flag) { return false; } } } return true; }
@Override public synchronized boolean add(DiscoveryResult result) throws IllegalStateException { if (result != null) { ThingUID thingUID = result.getThingUID(); Thing thing = this.thingRegistry.get(thingUID); discoveryResultStorage.put(result.getThingUID().toString(), result); notifyListeners(result, EventType.added); logger.info("Added new thing '{}' to inbox.", thingUID); DiscoveryResultImpl resultImpl = (DiscoveryResultImpl) inboxResult; resultImpl.synchronize(result); discoveryResultStorage.put(result.getThingUID().toString(), resultImpl); notifyListeners(resultImpl, EventType.updated); logger.debug("Updated discovery result for '{}'.", thingUID); + " It is already present as thing in the ThingRegistry.", thingUID); boolean updated = synchronizeConfiguration(result.getThingTypeUID(), result.getProperties(), thing.getConfiguration());
@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 (alwaysAutoApprove || isToBeAutoApproved(result)) { inbox.approve(result.getThingUID(), result.getLabel()); } }
@Override public @Nullable Thing approve(ThingUID thingUID, @Nullable 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; }
@Override public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs) { HashSet<ThingUID> removedThings = new HashSet<>(); for (DiscoveryResult discoveryResult : getAll()) { Class<?> discoverer = resultDiscovererMap.get(discoveryResult); if (thingTypeUIDs.contains(discoveryResult.getThingTypeUID()) && discoveryResult.getTimestamp() < timestamp && (discoverer == null || source.getClass() == discoverer)) { ThingUID thingUID = discoveryResult.getThingUID(); removedThings.add(thingUID); remove(thingUID); logger.debug("Removed {} from inbox because it was older than {}", thingUID, new Date(timestamp)); } } return removedThings; }
@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.getUID().getBindingId(), result.getBindingId())).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()); } }
private List<ThingUID> getResultsForBridge(ThingUID bridgeUID) { List<ThingUID> thingsForBridge = new ArrayList<>(); for (DiscoveryResult result : discoveryResultStorage.getValues()) { if (bridgeUID.equals(result.getBridgeUID())) { thingsForBridge.add(result.getThingUID()); } } return thingsForBridge; }
@Override public void deactivate() { super.deactivate(); inbox.stream().filter(thing -> { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); return BluetoothBindingConstants.SUPPORTED_THING_TYPES.contains(thingTypeUID); }).forEach(thing -> inbox.remove(thing.getThingUID())); }
public static Predicate<DiscoveryResult> withProperty(@Nullable String propertyName, String propertyValue) { return r -> r.getProperties().containsKey(propertyName) && r.getProperties().get(propertyName).equals(propertyValue); }
public static Predicate<DiscoveryResult> forThingTypeUID(@Nullable ThingTypeUID uid) { return r -> uid != null && uid.equals(r.getThingTypeUID()); }
public static Predicate<DiscoveryResult> withRepresentationProperty(@Nullable String propertyName) { return r -> propertyName != null && propertyName.equals(r.getRepresentationProperty()); }