]> git.basschouten.com Git - openhab-addons.git/blob
bc28e69eeb9c71d3e1218e3008afcec4f4d9197d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.magentatv.internal.discovery;
14
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.*;
18
19 import java.util.Collections;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23
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;
35
36 /**
37  * The {@link MagentaTVDiscoveryParticipant} is responsible for discovering new
38  * and removed MagentaTV receivers. It uses the central UpnpDiscoveryService.
39  *
40  * @author Markus Michels - Initial contribution
41  */
42 @NonNullByDefault
43 @Component(service = UpnpDiscoveryParticipant.class)
44 public class MagentaTVDiscoveryParticipant implements UpnpDiscoveryParticipant {
45     private final Logger logger = LoggerFactory.getLogger(MagentaTVDiscoveryParticipant.class);
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return Collections.singleton(THING_TYPE_RECEIVER);
50     }
51
52     /**
53      * New discovered result.
54      */
55     @Override
56     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
57         DiscoveryResult result = null;
58         try {
59             String modelName = getString(device.getDetails().getModelDetails().getModelName()).toUpperCase();
60             String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
61                     .toUpperCase();
62             logger.trace("Device discovered: {} - {}", manufacturer, modelName);
63
64             ThingUID uid = getThingUID(device);
65             if (uid != null) {
66                 logger.debug("Discovered a MagentaTV Media Receiver {}, UDN: {}, Model {}.{}",
67                         device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString(),
68                         modelName, device.getDetails().getModelDetails().getModelNumber());
69
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;
79                 }
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));
88
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();
93             }
94         } catch (RuntimeException e) {
95             logger.debug("Unable to create thing for device {}/{} - {}", device.getDetails().getFriendlyName(),
96                     device.getIdentity().getUdn().getIdentifierString(), e.getMessage());
97         }
98         return result;
99     }
100
101     /**
102      * Get the UID for a device
103      */
104     @Override
105     public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
106         if (device != null) {
107             String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
108                     .toUpperCase();
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());
113             }
114         }
115         return null;
116     }
117
118     private String getString(@Nullable String value) {
119         return value != null ? value : "";
120     }
121 }