2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.magentatv.internal.discovery;
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.*;
16 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.*;
17 import static org.openhab.core.thing.Thing.*;
21 import java.util.TreeMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.RemoteDevice;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link MagentaTVDiscoveryParticipant} is responsible for discovering new
37 * and removed MagentaTV receivers. It uses the central UpnpDiscoveryService.
39 * @author Markus Michels - Initial contribution
42 @Component(service = UpnpDiscoveryParticipant.class)
43 public class MagentaTVDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(MagentaTVDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(THING_TYPE_RECEIVER);
52 * New discovered result.
55 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
56 DiscoveryResult result = null;
58 String modelName = getString(device.getDetails().getModelDetails().getModelName()).toUpperCase();
59 String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
61 logger.trace("Device discovered: {} - {}", manufacturer, modelName);
63 ThingUID uid = getThingUID(device);
65 logger.debug("Discovered a MagentaTV Media Receiver {}, UDN: {}, Model {}.{}",
66 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString(),
67 modelName, device.getDetails().getModelDetails().getModelNumber());
69 Map<String, Object> properties = new TreeMap<>();
70 String descriptorURL = device.getIdentity().getDescriptorURL().toString();
71 String port = substringBefore(substringAfterLast(descriptorURL, ":"), "/");
72 String hex = device.getIdentity().getUdn().getIdentifierString()
73 .substring(device.getIdentity().getUdn().getIdentifierString().length() - 12);
74 String mac = hex.substring(0, 2) + ":" + hex.substring(2, 4) + ":" + hex.substring(4, 6) + ":"
75 + hex.substring(6, 8) + ":" + hex.substring(8, 10) + ":" + hex.substring(10, 12);
76 if ("49153".equals(port)) { // MR400 reports the rong
77 port = MR400_DEF_REMOTE_PORT;
79 properties.put(PROPERTY_VENDOR, VENDOR + "(" + manufacturer + ")");
80 properties.put(PROPERTY_MODEL_ID, modelName);
81 properties.put(PROPERTY_HARDWARE_VERSION, device.getDetails().getModelDetails().getModelNumber());
82 properties.put(PROPERTY_MAC_ADDRESS, mac);
83 properties.put(PROPERTY_UDN, device.getIdentity().getUdn().getIdentifierString().toUpperCase());
84 properties.put(PROPERTY_IP, substringBetween(descriptorURL, "http://", ":"));
85 properties.put(PROPERTY_PORT, port);
86 properties.put(PROPERTY_DESC_URL, substringAfterLast(descriptorURL, ":" + port));
88 logger.debug("Create Thing for device {} with UDN {}, Model{}", device.getDetails().getFriendlyName(),
89 device.getIdentity().getUdn().getIdentifierString(), modelName);
90 result = DiscoveryResultBuilder.create(uid).withLabel(device.getDetails().getFriendlyName())
91 .withProperties(properties).withRepresentationProperty(PROPERTY_MAC_ADDRESS).build();
93 } catch (RuntimeException e) {
94 logger.debug("Unable to create thing for device {}/{} - {}", device.getDetails().getFriendlyName(),
95 device.getIdentity().getUdn().getIdentifierString(), e.getMessage());
101 * Get the UID for a device
104 public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
105 if (device != null) {
106 String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
108 String model = device.getDetails().getModelDetails().getModelName().toUpperCase();
109 if (manufacturer.contains(OEM_VENDOR) && ((model.contains(MODEL_MR400) || model.contains(MODEL_MR401B)
110 || model.contains(MODEL_MR601) || model.contains(MODEL_MR201)))) {
111 return new ThingUID(THING_TYPE_RECEIVER, device.getIdentity().getUdn().getIdentifierString());
117 private String getString(@Nullable String value) {
118 return value != null ? value : "";