@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; }
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC) protected void addInboxAutoApprovePredicate(InboxAutoApprovePredicate inboxAutoApprovePredicate) { inboxAutoApprovePredicates.add(inboxAutoApprovePredicate); for (DiscoveryResult result : inbox.getAll()) { if (result.getFlag().equals(DiscoveryResultFlag.NEW) && inboxAutoApprovePredicate.test(result)) { inbox.approve(result.getThingUID(), result.getLabel()); } } }
/** * 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(); } }
@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; }
@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()); } }
/** * 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(); } }
@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()); } }
private void approveAllInboxEntries() { for (DiscoveryResult result : inbox.getAll()) { if (result.getFlag().equals(DiscoveryResultFlag.NEW)) { inbox.approve(result.getThingUID(), result.getLabel()); } } }
private void clearInboxEntries(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(); console.println(String.format("REMOVED [%s]: %s [label=%s, thingId=%s, bridgeId=%s, properties=%s]", flag.name(), thingTypeUID, label, thingUID, bridgeId, properties)); inbox.remove(thingUID); } }
private void autoApproveInboxEntries() { for (DiscoveryResult result : inbox.getAll()) { if (result.getFlag().equals(DiscoveryResultFlag.NEW)) { if (alwaysAutoApprove || isToBeAutoApproved(result)) { inbox.approve(result.getThingUID(), result.getLabel()); } } } }
/** * Notifies the registered {@link DiscoveryListener}s about a discovered device. * * @param discoveryResult Holds the information needed to identify the discovered device. */ protected void thingDiscovered(final DiscoveryResult discoveryResult) { final DiscoveryResult discoveryResultNew; if (this.i18nProvider != null && this.localeProvider != null) { Bundle bundle = FrameworkUtil.getBundle(this.getClass()); String defaultLabel = discoveryResult.getLabel(); String key = I18nUtil.stripConstantOr(defaultLabel, () -> inferKey(discoveryResult, "label")); String label = this.i18nProvider.getText(bundle, key, defaultLabel, this.localeProvider.getLocale()); discoveryResultNew = new DiscoveryResultImpl(discoveryResult.getThingTypeUID(), discoveryResult.getThingUID(), discoveryResult.getBridgeUID(), discoveryResult.getProperties(), discoveryResult.getRepresentationProperty(), label, discoveryResult.getTimeToLive()); } else { discoveryResultNew = discoveryResult; } for (DiscoveryListener discoveryListener : discoveryListeners) { try { discoveryListener.thingDiscovered(this, discoveryResultNew); } catch (Exception e) { logger.error("An error occurred while calling the discovery listener {}.", discoveryListener.getClass().getName(), e); } } synchronized (cachedResults) { cachedResults.put(discoveryResultNew.getThingUID(), discoveryResultNew); } }
private void clearInboxEntries(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(); console.println(String.format("REMOVED [%s]: %s [label=%s, thingId=%s, bridgeId=%s, properties=%s]", flag.name(), thingTypeUID, label, thingUID, bridgeId, properties)); inbox.remove(thingUID); } }
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 DiscoveryResult createDiscoveryResultWithUsbProperties(DiscoveryResult result, UsbSerialDeviceInformation usbSerialDeviceInformation) { Map<String, Object> resultProperties = new HashMap<>(result.getProperties()); resultProperties.put(THING_PROPERTY_USB_VENDOR_ID, usbSerialDeviceInformation.getVendorId()); resultProperties.put(THING_PROPERTY_USB_PRODUCT_ID, usbSerialDeviceInformation.getProductId()); return DiscoveryResultBuilder.create(result.getThingUID()).withProperties(resultProperties) .withBridge(result.getBridgeUID()).withTTL(result.getTimeToLive()).withLabel(result.getLabel()) .withRepresentationProperty(result.getRepresentationProperty()).build(); } }
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)); } }
/** * Notifies the registered {@link DiscoveryListener}s about a discovered device. * * @param discoveryResult * Holds the information needed to identify the discovered device. */ protected void thingDiscovered(DiscoveryResult discoveryResult) { if (this.i18nProvider != null && this.localeProvider != null) { Bundle bundle = FrameworkUtil.getBundle(this.getClass()); String defaultLabel = discoveryResult.getLabel(); String key = I18nUtil.isConstant(defaultLabel) ? I18nUtil.stripConstant(defaultLabel) : inferKey(discoveryResult, "label"); String label = this.i18nProvider.getText(bundle, key, defaultLabel, this.localeProvider.getLocale()); discoveryResult = new DiscoveryResultImpl(discoveryResult.getThingTypeUID(), discoveryResult.getThingUID(), discoveryResult.getBridgeUID(), discoveryResult.getProperties(), discoveryResult.getRepresentationProperty(), label, discoveryResult.getTimeToLive()); } for (DiscoveryListener discoveryListener : discoveryListeners) { try { discoveryListener.thingDiscovered(this, discoveryResult); } catch (Exception e) { logger.error("An error occurred while calling the discovery listener {}.", discoveryListener.getClass().getName(), e); } } synchronized (cachedResults) { cachedResults.put(discoveryResult.getThingUID(), discoveryResult); } }
/** * 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()); }
/** * 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()); }