]> git.basschouten.com Git - openhab-addons.git/blob
761e1a6046c4f6d904feee26ad08e57be1a5b5b9
[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.Map;
20 import java.util.Set;
21 import java.util.TreeMap;
22
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;
34
35 /**
36  * The {@link MagentaTVDiscoveryParticipant} is responsible for discovering new
37  * and removed MagentaTV receivers. It uses the central UpnpDiscoveryService.
38  *
39  * @author Markus Michels - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = UpnpDiscoveryParticipant.class)
43 public class MagentaTVDiscoveryParticipant implements UpnpDiscoveryParticipant {
44     private final Logger logger = LoggerFactory.getLogger(MagentaTVDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(THING_TYPE_RECEIVER);
49     }
50
51     /**
52      * New discovered result.
53      */
54     @Override
55     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
56         DiscoveryResult result = null;
57         try {
58             String modelName = getString(device.getDetails().getModelDetails().getModelName()).toUpperCase();
59             String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
60                     .toUpperCase();
61             logger.trace("Device discovered: {} - {}", manufacturer, modelName);
62
63             ThingUID uid = getThingUID(device);
64             if (uid != null) {
65                 logger.debug("Discovered a MagentaTV Media Receiver {}, UDN: {}, Model {}.{}",
66                         device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString(),
67                         modelName, device.getDetails().getModelDetails().getModelNumber());
68
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;
78                 }
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));
87
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();
92             }
93         } catch (RuntimeException e) {
94             logger.debug("Unable to create thing for device {}/{} - {}", device.getDetails().getFriendlyName(),
95                     device.getIdentity().getUdn().getIdentifierString(), e.getMessage());
96         }
97         return result;
98     }
99
100     /**
101      * Get the UID for a device
102      */
103     @Override
104     public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
105         if (device != null) {
106             String manufacturer = getString(device.getDetails().getManufacturerDetails().getManufacturer())
107                     .toUpperCase();
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());
112             }
113         }
114         return null;
115     }
116
117     private String getString(@Nullable String value) {
118         return value != null ? value : "";
119     }
120 }