]> git.basschouten.com Git - openhab-addons.git/blob
52249bf27322f7837059e77ddc42a841de55f85b
[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             return DiscoveryResultBuilder.create(uid).withProperties(properties)
66                     .withLabel(device.getDetails().getFriendlyName()).withRepresentationProperty(MAC_ADDR).build();
67         } else {
68             return null;
69         }
70     }
71
72     @Override
73     public @Nullable ThingUID getThingUID(RemoteDevice device) {
74         DeviceDetails details = device.getDetails();
75         if (details != null) {
76             ModelDetails modelDetails = details.getModelDetails();
77             if (modelDetails != null) {
78                 String modelName = modelDetails.getModelName();
79                 logger.debug("Model Details: {} Url: {} UDN: {}  Model Number: {}", modelName, details.getBaseURL(),
80                         details.getSerialNumber(), modelDetails.getModelNumber());
81                 if (modelName != null) {
82                     if (modelName.startsWith("Konnected Pro")) {
83                         return new ThingUID(THING_TYPE_PROMODULE, details.getSerialNumber());
84                     }
85                     if (modelName.startsWith("Konnected")) {
86                         return new ThingUID(THING_TYPE_WIFIMODULE, details.getSerialNumber());
87                     }
88                 }
89             }
90         }
91         return null;
92     }
93 }