Search in sources :

Example 6 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class PersistentInbox method notifyListeners.

private void notifyListeners(DiscoveryResult result, EventType type) {
    for (InboxListener listener : this.listeners) {
        try {
            switch(type) {
                case added:
                    listener.thingAdded(this, result);
                    break;
                case removed:
                    listener.thingRemoved(this, result);
                    break;
                case updated:
                    listener.thingUpdated(this, result);
                    break;
            }
        } catch (Exception ex) {
            String errorMessage = String.format("Cannot notify the InboxListener '%s' about a Thing %s event!", listener.getClass().getName(), type.name());
            logger.error(errorMessage, ex);
        }
    }
    // in case of EventType added/updated the listeners might have modified the result in the discoveryResultStorage
    DiscoveryResult resultForEvent = type == EventType.removed ? result : get(result.getThingUID());
    postEvent(resultForEvent, type);
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) InboxListener(org.eclipse.smarthome.config.discovery.inbox.InboxListener)

Example 7 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class InboxConsoleCommandExtension method clearInboxEntries.

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);
    }
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) DiscoveryResultFlag(org.eclipse.smarthome.config.discovery.DiscoveryResultFlag) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID)

Example 8 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class InboxConsoleCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        final String subCommand = args[0];
        switch(subCommand) {
            case SUBCMD_APPROVE:
                if (args.length > 2) {
                    String label = args[2];
                    try {
                        ThingUID thingUID = new ThingUID(args[1]);
                        List<DiscoveryResult> results = inbox.stream().filter(forThingUID(thingUID)).collect(Collectors.toList());
                        if (results.isEmpty()) {
                            console.println("No matching inbox entry could be found.");
                            return;
                        }
                        inbox.approve(thingUID, label);
                    } catch (Exception e) {
                        console.println(e.getMessage());
                    }
                } else {
                    console.println("Specify thing id to approve: inbox approve <thingUID> <label>");
                }
                break;
            case SUBCMD_IGNORE:
                if (args.length > 1) {
                    try {
                        ThingUID thingUID = new ThingUID(args[1]);
                        PersistentInbox persistentInbox = (PersistentInbox) inbox;
                        persistentInbox.setFlag(thingUID, DiscoveryResultFlag.IGNORED);
                    } catch (IllegalArgumentException e) {
                        console.println("'" + args[1] + "' is no valid thing UID.");
                    }
                } else {
                    console.println("Cannot approve thing as managed thing provider is missing.");
                }
                break;
            case SUBCMD_LIST_IGNORED:
                printInboxEntries(console, inbox.stream().filter(withFlag((DiscoveryResultFlag.IGNORED))).collect(Collectors.toList()));
                break;
            case SUBCMD_CLEAR:
                clearInboxEntries(console, inbox.getAll());
                break;
            default:
                break;
        }
    } else {
        printInboxEntries(console, inbox.stream().filter(withFlag((DiscoveryResultFlag.NEW))).collect(Collectors.toList()));
    }
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) PersistentInbox(org.eclipse.smarthome.config.discovery.internal.PersistentInbox)

Example 9 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class ZonePlayerDiscoveryParticipant method createResult.

@Override
public DiscoveryResult createResult(RemoteDevice device) {
    ThingUID uid = getThingUID(device);
    if (uid != null) {
        String roomName = getSonosRoomName(device);
        if (roomName != null) {
            Map<String, Object> properties = new HashMap<>(3);
            String label = "Sonos device";
            try {
                label = device.getDetails().getModelDetails().getModelName();
            } catch (Exception e) {
            // ignore and use default label
            }
            label += " (" + roomName + ")";
            properties.put(ZonePlayerConfiguration.UDN, device.getIdentity().getUdn().getIdentifierString());
            properties.put(SonosBindingConstants.IDENTIFICATION, roomName);
            DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).withRepresentationProperty(ZonePlayerConfiguration.UDN).build();
            logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'", device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
            return result;
        }
    }
    return null;
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) HashMap(java.util.HashMap) ThingUID(org.eclipse.smarthome.core.thing.ThingUID)

Example 10 with DiscoveryResult

use of org.eclipse.smarthome.config.discovery.DiscoveryResult in project smarthome by eclipse.

the class TradfriDiscoveryService method onUpdate.

@Override
public void onUpdate(String instanceId, JsonObject data) {
    ThingUID bridge = handler.getThing().getUID();
    try {
        if (data.has(INSTANCE_ID)) {
            int id = data.get(INSTANCE_ID).getAsInt();
            String type = data.get(TYPE).getAsString();
            JsonObject deviceInfo = data.get(DEVICE).getAsJsonObject();
            String model = deviceInfo.get(DEVICE_MODEL).getAsString();
            ThingUID thingId = null;
            if (TYPE_LIGHT.equals(type) && data.has(LIGHT)) {
                JsonObject state = data.get(LIGHT).getAsJsonArray().get(0).getAsJsonObject();
                // Color temperature light:
                // We do not always receive a COLOR attribute, even the light supports it - but the gateway does not
                // seem to have this information, if the bulb is unreachable. We therefore also check against
                // concrete model names.
                // Color light:
                // As the protocol does not distinguishes between color and full-color lights,
                // we check if the "CWS" identifier is given in the model name
                ThingTypeUID thingType = null;
                if (model != null && model.contains(COLOR_MODELS_IDENTIFIER)) {
                    thingType = THING_TYPE_COLOR_LIGHT;
                }
                if (// 
                thingType == null && (state.has(COLOR) || (model != null && Arrays.asList(COLOR_TEMP_MODELS).contains(model)))) {
                    thingType = THING_TYPE_COLOR_TEMP_LIGHT;
                }
                if (thingType == null) {
                    thingType = THING_TYPE_DIMMABLE_LIGHT;
                }
                thingId = new ThingUID(thingType, bridge, Integer.toString(id));
            } else if (TYPE_SWITCH.equals(type) && data.has(SWITCH)) {
                // Remote control and wireless dimmer
                // As protocol does not distinguishes between remote control and wireless dimmer,
                // we check for the whole model name
                ThingTypeUID thingType = (model != null && REMOTE_CONTROLLER_MODEL.equals(model)) ? THING_TYPE_REMOTE_CONTROL : THING_TYPE_DIMMER;
                thingId = new ThingUID(thingType, bridge, Integer.toString(id));
            } else if (TYPE_SENSOR.equals(type) && data.has(SENSOR)) {
                // Motion sensor
                thingId = new ThingUID(THING_TYPE_MOTION_SENSOR, bridge, Integer.toString(id));
            }
            if (thingId == null) {
                // we didn't identify any device, so let's quit
                return;
            }
            String label = data.get(NAME).getAsString();
            Map<String, Object> properties = new HashMap<>(1);
            properties.put("id", id);
            properties.put(PROPERTY_MODEL_ID, model);
            if (deviceInfo.get(DEVICE_VENDOR) != null) {
                properties.put(PROPERTY_VENDOR, deviceInfo.get(DEVICE_VENDOR).getAsString());
            }
            if (deviceInfo.get(DEVICE_FIRMWARE) != null) {
                properties.put(PROPERTY_FIRMWARE_VERSION, deviceInfo.get(DEVICE_FIRMWARE).getAsString());
            }
            logger.debug("Adding device {} to inbox", thingId);
            DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingId).withBridge(bridge).withLabel(label).withProperties(properties).withRepresentationProperty("id").build();
            thingDiscovered(discoveryResult);
        }
    } catch (JsonSyntaxException e) {
        logger.debug("JSON error during discovery: {}", e.getMessage());
    }
}
Also used : DiscoveryResult(org.eclipse.smarthome.config.discovery.DiscoveryResult) JsonSyntaxException(com.google.gson.JsonSyntaxException) HashMap(java.util.HashMap) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) JsonObject(com.google.gson.JsonObject) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) JsonObject(com.google.gson.JsonObject)

Aggregations

DiscoveryResult (org.eclipse.smarthome.config.discovery.DiscoveryResult)42 ThingUID (org.eclipse.smarthome.core.thing.ThingUID)29 HashMap (java.util.HashMap)16 Test (org.junit.Test)14 ThingTypeUID (org.eclipse.smarthome.core.thing.ThingTypeUID)12 Collection (java.util.Collection)4 DiscoveryListener (org.eclipse.smarthome.config.discovery.DiscoveryListener)4 DiscoveryService (org.eclipse.smarthome.config.discovery.DiscoveryService)4 ThingStatusInfo (org.eclipse.smarthome.core.thing.ThingStatusInfo)4 DiscoveryResultFlag (org.eclipse.smarthome.config.discovery.DiscoveryResultFlag)3 InboxPredicates.forThingUID (org.eclipse.smarthome.config.discovery.inbox.InboxPredicates.forThingUID)3 Thing (org.eclipse.smarthome.core.thing.Thing)3 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)3 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Collectors (java.util.stream.Collectors)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Configuration (org.eclipse.smarthome.config.core.Configuration)2