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