2 * Copyright (c) 2010-2024 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;
15 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.substringAfterLast;
17 import java.util.HashMap;
19 import java.util.TreeMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.magentatv.internal.handler.MagentaTVHandler;
24 import org.osgi.service.component.annotations.Component;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * The {@link MagentaTVDeviceManager} class manages the device table (shared between HandlerFactory and Thing handlers).
31 * @author Markus Michels - Initial contribution
34 @Component(service = MagentaTVDeviceManager.class)
35 public class MagentaTVDeviceManager {
36 private final Logger logger = LoggerFactory.getLogger(MagentaTVDeviceManager.class);
38 protected class MagentaTVDevice {
39 protected String udn = "";
40 protected String mac = "";
41 protected String deviceId = "";
42 protected String ipAddress = "";
43 protected Map<String, String> properties = new HashMap<>();
44 protected @Nullable MagentaTVHandler thingHandler;
47 private final Map<String, MagentaTVDevice> deviceList = new HashMap<>();
49 public void registerDevice(String udn, String deviceId, String ipAddress, MagentaTVHandler handler) {
50 logger.trace("Register new device, UDN={}, deviceId={}, ipAddress={}", udn, deviceId, ipAddress);
51 addNewDevice(udn, deviceId, ipAddress, "", new TreeMap<String, String>(), handler);
54 private void addNewDevice(String udn, String deviceId, String ipAddress, String macAddress,
55 Map<String, String> discoveryProperties, @Nullable MagentaTVHandler handler) {
57 if (macAddress.isEmpty()) { // build MAC from UDN
58 mac = substringAfterLast(udn, "-");
63 boolean newDev = false;
64 synchronized (deviceList) {
66 if (deviceList.containsKey(udn.toUpperCase())) {
67 dev = deviceList.get(udn.toUpperCase());
69 dev = new MagentaTVDevice();
72 dev.udn = udn.toUpperCase();
73 dev.mac = mac.toUpperCase();
74 if (!deviceId.isEmpty()) {
75 dev.deviceId = deviceId.toUpperCase();
77 dev.ipAddress = ipAddress;
78 dev.properties = discoveryProperties;
79 dev.thingHandler = handler;
81 deviceList.put(dev.udn, dev);
84 logger.debug("New device {}: (UDN={} ,deviceId={}, ipAddress={}, macAddress={}), now {} devices.",
85 newDev ? "added" : "updated", udn, deviceId, ipAddress, mac, deviceList.size());
89 * Remove a device from the table
93 public void removeDevice(String deviceId) {
94 MagentaTVDevice dev = lookupDevice(deviceId);
96 synchronized (deviceList) {
97 logger.trace("Device with UDN {} removed from table, new site={}", dev.udn, deviceList.size());
98 deviceList.remove(dev.udn);
104 * Lookup a device in the table by an id (this could be the UDN, the MAC
105 * address, the IP address or a unique device ID)
110 public @Nullable MagentaTVDevice lookupDevice(String uniqueId) {
111 MagentaTVDevice dev = null;
112 logger.trace("Lookup device, uniqueId={}", uniqueId);
114 for (String key : deviceList.keySet()) {
115 synchronized (deviceList) {
116 if (deviceList.containsKey(key)) {
117 dev = deviceList.get(key);
118 logger.trace("Devies[{}]: deviceId={}, UDN={}, ipAddress={}, macAddress={}", i++, dev.deviceId,
119 dev.udn, dev.ipAddress, dev.mac);
120 if (dev.udn.equalsIgnoreCase(uniqueId) || dev.ipAddress.equalsIgnoreCase(uniqueId)
121 || dev.deviceId.equalsIgnoreCase(uniqueId) || dev.mac.equalsIgnoreCase(uniqueId)) {
127 logger.debug("Device with id {} was not found in table ({} entries", uniqueId, deviceList.size());
132 * returned the discovered properties
134 * @param udn Unique ID from UPnP discovery
135 * @return property map with discovered properties
137 public @Nullable Map<String, String> getDiscoveredProperties(String udn) {
138 if (deviceList.containsKey(udn.toUpperCase())) {
139 MagentaTVDevice dev = deviceList.get(udn.toUpperCase());
140 return dev.properties;
142 if (!deviceList.isEmpty()) {
143 logger.debug("getDiscoveredProperties(): Unknown UDN: {}", udn);
148 public int numberOfDevices() {
149 return deviceList.size();