]> git.basschouten.com Git - openhab-addons.git/blob
666155497ae9252825b697d851987b0349bf51f7
[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.konnected.internal.discovery;
14
15 import static org.openhab.binding.konnected.internal.KonnectedBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.DeviceDetails;
27 import org.jupnp.model.meta.ModelDetails;
28 import org.jupnp.model.meta.RemoteDevice;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
32 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link KonnectedUPnPServer} is responsible for discovering new
41  * Konnectedmodules modules. It uses the central {@link UpnpDiscoveryService}.
42  *
43  * @author Zachary Christainsen - Initial contribution
44  *
45  */
46 @NonNullByDefault
47 @Component(service = UpnpDiscoveryParticipant.class)
48 public class KonnectedUPnPServer implements UpnpDiscoveryParticipant {
49     private Logger logger = LoggerFactory.getLogger(KonnectedUPnPServer.class);
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
51             .unmodifiableSet(Stream.of(THING_TYPE_PROMODULE, THING_TYPE_WIFIMODULE).collect(Collectors.toSet()));
52
53     @Override
54     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
55         return SUPPORTED_THING_TYPES_UIDS;
56     }
57
58     @Override
59     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
60         ThingUID uid = getThingUID(device);
61         if (uid != null) {
62             Map<String, Object> properties = new HashMap<>();
63             properties.put(BASE_URL, device.getDetails().getBaseURL());
64             properties.put(MAC_ADDR, device.getDetails().getSerialNumber());
65             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
66                     .withLabel(device.getDetails().getFriendlyName()).withRepresentationProperty(MAC_ADDR).build();
67             return result;
68         } else {
69             return null;
70         }
71     }
72
73     @Override
74     public @Nullable ThingUID getThingUID(RemoteDevice device) {
75         DeviceDetails details = device.getDetails();
76         if (details != null) {
77             ModelDetails modelDetails = details.getModelDetails();
78             if (modelDetails != null) {
79                 String modelName = modelDetails.getModelName();
80                 logger.debug("Model Details: {} Url: {} UDN: {}  Model Number: {}", modelName, details.getBaseURL(),
81                         details.getSerialNumber(), modelDetails.getModelNumber());
82                 if (modelName != null) {
83                     if (modelName.startsWith("Konnected Pro")) {
84                         return new ThingUID(THING_TYPE_PROMODULE, details.getSerialNumber());
85                     }
86                     if (modelName.startsWith("Konnected")) {
87                         return new ThingUID(THING_TYPE_WIFIMODULE, details.getSerialNumber());
88                     }
89                 }
90             }
91         }
92         return null;
93     }
94 }