]> git.basschouten.com Git - openhab-addons.git/blob
ef1d0d45fab7b1731a75eeb285517146ded839b4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.kaleidescape.internal.discovery;
14
15 import static org.openhab.binding.kaleidescape.internal.KaleidescapeBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jupnp.model.meta.RemoteDevice;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link KaleidescapeDiscoveryParticipant} class discovers Strato/Encore line components automatically via UPnP.
35  *
36  * @author Michael Lobstein - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 @Component(immediate = true)
41 public class KaleidescapeDiscoveryParticipant implements UpnpDiscoveryParticipant {
42     private final Logger logger = LoggerFactory.getLogger(KaleidescapeDiscoveryParticipant.class);
43
44     private static final String MANUFACTURER = "Kaleidescape";
45
46     // Component Types
47     private static final String ALTO = "Alto";
48     private static final String STRATO = "Strato";
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52         return Set.of(THING_TYPE_ALTO, THING_TYPE_STRATO);
53     }
54
55     @Override
56     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
57         final ThingUID uid = getThingUID(device);
58         if (uid != null) {
59             final Map<String, Object> properties = new HashMap<>(3);
60             final String label;
61
62             if (device.getDetails().getFriendlyName() != null && !device.getDetails().getFriendlyName().isBlank()) {
63                 label = device.getDetails().getFriendlyName();
64             } else {
65                 label = device.getDetails().getModelDetails().getModelName();
66             }
67
68             properties.put(PROPERTY_UUID, uid.getId());
69             properties.put(PROPERTY_HOST_NAME, device.getIdentity().getDescriptorURL().getHost());
70             properties.put(PROPERTY_PORT_NUM, DEFAULT_API_PORT);
71
72             final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
73                     .withRepresentationProperty(PROPERTY_UUID).withLabel(label).build();
74
75             logger.debug("Created a DiscoveryResult for device '{}' with UID '{}'", label, uid.getId());
76             return result;
77         } else {
78             return null;
79         }
80     }
81
82     @Override
83     public @Nullable ThingUID getThingUID(RemoteDevice device) {
84         if (device.getDetails().getManufacturerDetails().getManufacturer() != null
85                 && device.getDetails().getModelDetails().getModelName() != null
86                 && device.getDetails().getManufacturerDetails().getManufacturer().startsWith(MANUFACTURER)) {
87             final String modelName = device.getDetails().getModelDetails().getModelName();
88             final String id = device.getIdentity().getUdn().getIdentifierString().replace(":", EMPTY);
89
90             logger.debug("Kaleidescape {} with id {} found at {}", modelName, id,
91                     device.getIdentity().getDescriptorURL().getHost());
92
93             if (id.isBlank()) {
94                 logger.debug("Invalid UDN for Kaleidescape device: {}", device.toString());
95                 return null;
96             }
97
98             if (modelName.contains(ALTO)) {
99                 return new ThingUID(THING_TYPE_ALTO, id);
100             } else if (modelName.contains(STRATO)) {
101                 return new ThingUID(THING_TYPE_STRATO, id);
102             }
103         }
104         return null;
105     }
106 }