]> git.basschouten.com Git - openhab-addons.git/blob
3fb0b7a0e028da84c96e50d143f6a908752a2fb0
[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;
14
15 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.substringAfterLast;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.TreeMap;
20
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;
27
28 /**
29  * The {@link MagentaTVDeviceManager} class manages the device table (shared between HandlerFactory and Thing handlers).
30  *
31  * @author Markus Michels - Initial contribution
32  */
33 @NonNullByDefault
34 @Component(service = MagentaTVDeviceManager.class)
35 public class MagentaTVDeviceManager {
36     private final Logger logger = LoggerFactory.getLogger(MagentaTVDeviceManager.class);
37
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;
45     }
46
47     private final Map<String, MagentaTVDevice> deviceList = new HashMap<>();
48
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);
52     }
53
54     private void addNewDevice(String udn, String deviceId, String ipAddress, String macAddress,
55             Map<String, String> discoveryProperties, @Nullable MagentaTVHandler handler) {
56         String mac = "";
57         if (macAddress.isEmpty()) { // build MAC from UDN
58             mac = substringAfterLast(udn, "-");
59         } else {
60             mac = macAddress;
61         }
62
63         boolean newDev = false;
64         synchronized (deviceList) {
65             MagentaTVDevice dev;
66             if (deviceList.containsKey(udn.toUpperCase())) {
67                 dev = deviceList.get(udn.toUpperCase());
68             } else {
69                 dev = new MagentaTVDevice();
70                 newDev = true;
71             }
72             dev.udn = udn.toUpperCase();
73             dev.mac = mac.toUpperCase();
74             if (!deviceId.isEmpty()) {
75                 dev.deviceId = deviceId.toUpperCase();
76             }
77             dev.ipAddress = ipAddress;
78             dev.properties = discoveryProperties;
79             dev.thingHandler = handler;
80             if (newDev) {
81                 deviceList.put(dev.udn, dev);
82             }
83         }
84         logger.debug("New device {}: (UDN={} ,deviceId={}, ipAddress={}, macAddress={}), now {} devices.",
85                 newDev ? "added" : "updated", udn, deviceId, ipAddress, mac, deviceList.size());
86     }
87
88     /**
89      * Remove a device from the table
90      *
91      * @param deviceId
92      */
93     public void removeDevice(String deviceId) {
94         MagentaTVDevice dev = lookupDevice(deviceId);
95         if (dev != null) {
96             synchronized (deviceList) {
97                 logger.trace("Device with UDN {} removed from table, new site={}", dev.udn, deviceList.size());
98                 deviceList.remove(dev.udn);
99             }
100         }
101     }
102
103     /**
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)
106      *
107      * @param uniqueId
108      * @return
109      */
110     public @Nullable MagentaTVDevice lookupDevice(String uniqueId) {
111         MagentaTVDevice dev = null;
112         logger.trace("Lookup device, uniqueId={}", uniqueId);
113         int i = 0;
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)) {
122                         return dev;
123                     }
124                 }
125             }
126         }
127         logger.debug("Device with id {} was not found in table ({} entries", uniqueId, deviceList.size());
128         return null;
129     }
130
131     /**
132      * returned the discovered properties
133      *
134      * @param udn Unique ID from UPnP discovery
135      * @return property map with discovered properties
136      */
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;
141         }
142         if (!deviceList.isEmpty()) {
143             logger.debug("getDiscoveredProperties(): Unknown UDN: {}", udn);
144         }
145         return null;
146     }
147
148     public int numberOfDevices() {
149         return deviceList.size();
150     }
151 }