2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lametrictime.internal.discovery;
15 import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.THING_TYPE_DEVICE;
16 import static org.openhab.binding.lametrictime.internal.config.LaMetricTimeConfiguration.HOST;
18 import java.util.HashMap;
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;
35 * The {@link LaMetricTimeDiscoveryParticipant} is responsible for processing the
36 * results of searched UPnP devices
38 * @author Gregory Moyer - Initial contribution
42 public class LaMetricTimeDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private Logger logger = LoggerFactory.getLogger(LaMetricTimeDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(THING_TYPE_DEVICE);
52 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53 ThingUID uid = getThingUID(device);
58 Map<String, Object> properties = new HashMap<>(1);
59 properties.put(HOST, device.getIdentity().getDescriptorURL().getHost());
61 String friendlyName = device.getDetails().getFriendlyName();
62 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(friendlyName)
65 logger.debug("Created a DiscoveryResult for device '{}' with serial number '{}'",
66 device.getDetails().getModelDetails().getModelName(), device.getDetails().getSerialNumber());
72 public @Nullable ThingUID getThingUID(RemoteDevice device) {
74 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
75 String modelName = device.getDetails().getModelDetails().getModelName();
77 if (!manufacturer.toUpperCase().contains("LAMETRIC")
78 || !modelName.toUpperCase().contains("LAMETRIC TIME")) {
82 String serialNumber = device.getDetails().getSerialNumber();
83 logger.debug("Discovered '{}' model '{}' thing with serial number '{}'",
84 device.getDetails().getFriendlyName(), modelName, serialNumber);
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);