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