]> git.basschouten.com Git - openhab-addons.git/blob
61e3d965e9a895cca97c1d8f5b13a7317794f6c2
[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.lametrictime.internal.discovery;
14
15 import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.THING_TYPE_DEVICE;
16 import static org.openhab.binding.lametrictime.internal.config.LaMetricTimeConfiguration.HOST;
17
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.RemoteDevice;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link LaMetricTimeDiscoveryParticipant} is responsible for processing the
36  * results of searched UPnP devices
37  *
38  * @author Gregory Moyer - Initial contribution
39  */
40 @Component
41 @NonNullByDefault
42 public class LaMetricTimeDiscoveryParticipant implements UpnpDiscoveryParticipant {
43
44     private Logger logger = LoggerFactory.getLogger(LaMetricTimeDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Set.of(THING_TYPE_DEVICE);
49     }
50
51     @Override
52     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53         ThingUID uid = getThingUID(device);
54         if (uid == null) {
55             return null;
56         }
57
58         Map<String, Object> properties = new HashMap<>(1);
59         properties.put(HOST, device.getIdentity().getDescriptorURL().getHost());
60
61         String friendlyName = device.getDetails().getFriendlyName();
62         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(friendlyName)
63                 .build();
64
65         logger.debug("Created a DiscoveryResult for device '{}' with serial number '{}'",
66                 device.getDetails().getModelDetails().getModelName(), device.getDetails().getSerialNumber());
67
68         return result;
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(RemoteDevice device) {
73         try {
74             String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
75             String modelName = device.getDetails().getModelDetails().getModelName();
76
77             if (!manufacturer.toUpperCase().contains("LAMETRIC")
78                     || !modelName.toUpperCase().contains("LAMETRIC TIME")) {
79                 return null;
80             }
81
82             String serialNumber = device.getDetails().getSerialNumber();
83             logger.debug("Discovered '{}' model '{}' thing with serial number '{}'",
84                     device.getDetails().getFriendlyName(), modelName, serialNumber);
85
86             return new ThingUID(THING_TYPE_DEVICE, serialNumber);
87         } catch (Exception e) {
88             // device was not what we expected
89             logger.debug("Discovery hit an unexpected error", e);
90             return null;
91         }
92     }
93 }