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