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.*;
19 import java.util.Collections;
22 import java.util.TreeMap;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link MagentaTVDiscoveryParticipant} is responsible for discovering new
38 * and removed MagentaTV receivers. It uses the central UpnpDiscoveryService.
40 * @author Markus Michels - Initial contribution
43 @Component(service = UpnpDiscoveryParticipant.class)
44 public class MagentaTVDiscoveryParticipant implements UpnpDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(MagentaTVDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Collections.singleton(THING_TYPE_RECEIVER);
53 * New discovered result.
56 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
57 DiscoveryResult result = null;
59 String modelName = getString(device.getDetails().getModelDetails().getModelName()).toUpperCase();
60 String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
62 logger.trace("Device discovered: {} - {}", manufacturer, modelName);
64 ThingUID uid = getThingUID(device);
66 logger.debug("Discovered a MagentaTV Media Receiver {}, UDN: {}, Model {}.{}",
67 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString(),
68 modelName, device.getDetails().getModelDetails().getModelNumber());
70 Map<String, Object> properties = new TreeMap<>();
71 String descriptorURL = device.getIdentity().getDescriptorURL().toString();
72 String port = substringBefore(substringAfterLast(descriptorURL, ":"), "/");
73 String hex = device.getIdentity().getUdn().getIdentifierString()
74 .substring(device.getIdentity().getUdn().getIdentifierString().length() - 12);
75 String mac = hex.substring(0, 2) + ":" + hex.substring(2, 4) + ":" + hex.substring(4, 6) + ":"
76 + hex.substring(6, 8) + ":" + hex.substring(8, 10) + ":" + hex.substring(10, 12);
77 if (port.equals("49153")) { // MR400 reports the rong
78 port = MR400_DEF_REMOTE_PORT;
80 properties.put(PROPERTY_VENDOR, VENDOR + "(" + manufacturer + ")");
81 properties.put(PROPERTY_MODEL_ID, modelName);
82 properties.put(PROPERTY_HARDWARE_VERSION, device.getDetails().getModelDetails().getModelNumber());
83 properties.put(PROPERTY_MAC_ADDRESS, mac);
84 properties.put(PROPERTY_UDN, device.getIdentity().getUdn().getIdentifierString().toUpperCase());
85 properties.put(PROPERTY_IP, substringBetween(descriptorURL, "http://", ":"));
86 properties.put(PROPERTY_PORT, port);
87 properties.put(PROPERTY_DESC_URL, substringAfterLast(descriptorURL, ":" + port));
89 logger.debug("Create Thing for device {} with UDN {}, Model{}", device.getDetails().getFriendlyName(),
90 device.getIdentity().getUdn().getIdentifierString(), modelName);
91 result = DiscoveryResultBuilder.create(uid).withLabel(device.getDetails().getFriendlyName())
92 .withProperties(properties).withRepresentationProperty(PROPERTY_MAC_ADDRESS).build();
94 } catch (RuntimeException e) {
95 logger.debug("Unable to create thing for device {}/{} - {}", device.getDetails().getFriendlyName(),
96 device.getIdentity().getUdn().getIdentifierString(), e.getMessage());
102 * Get the UID for a device
105 public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
106 if (device != null) {
107 String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
109 String model = device.getDetails().getModelDetails().getModelName().toUpperCase();
110 if (manufacturer.contains(OEM_VENDOR) && ((model.contains(MODEL_MR400) || model.contains(MODEL_MR401B)
111 || model.contains(MODEL_MR601) || model.contains(MODEL_MR201)))) {
112 return new ThingUID(THING_TYPE_RECEIVER, device.getIdentity().getUdn().getIdentifierString());
118 private String getString(@Nullable String value) {
119 return value != null ? value : "";