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