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.hpprinter.internal;
15 import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.THING_PRINTER;
17 import java.net.InetAddress;
18 import java.util.HashMap;
22 import javax.jmdns.ServiceInfo;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
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;
36 * The {@link HPPrinterDiscoveryParticipant} class discovers HP Printers over
39 * @author Stewart Cossey - Initial contribution
43 public class HPPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {
45 private final Logger logger = LoggerFactory.getLogger(HPPrinterDiscoveryParticipant.class);
48 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
49 if (!service.hasData()) {
50 logger.trace("Service has no data.");
54 // We only care about HP Printers
55 String ty = service.getPropertyString("ty");
56 if (ty != null && ty.toLowerCase().startsWith("hp")) {
57 String rp = service.getPropertyString("rp");
62 logger.debug("Found HP Printer ID: {}", ty);
63 ThingUID uid = getThingUID(service);
65 Map<String, Object> properties = new HashMap<>(2);
66 InetAddress ip = getIpAddress(service);
70 String inetAddress = ip.toString().substring(1); // trim leading slash
71 String label = service.getName();
73 properties.put(HPPrinterConfiguration.IP_ADDRESS, inetAddress);
74 properties.put(HPPrinterConfiguration.UUID, service.getPropertyString("UUID"));
75 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
76 .withRepresentationProperty(HPPrinterConfiguration.UUID).withLabel(label).build();
77 logger.trace("Created a DiscoveryResult {} for printer on host '{}' name '{}'", result, inetAddress,
81 logger.debug("Found unsupported HP Printer ID: {}", ty);
84 logger.trace("Ignore non HP device {}", ty);
90 private String getUIDName(ServiceInfo service) {
91 return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
95 public String getServiceType() {
96 return "_printer._tcp.local.";
100 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
101 return HPPrinterBindingConstants.SUPPORTED_THING_TYPES_UIDS;
105 public @Nullable ThingUID getThingUID(ServiceInfo service) {
106 if (service.getType() != null) {
107 logger.trace("ServiceInfo: {}", service);
108 if (service.getType().equals(getServiceType())) {
109 String uidName = getUIDName(service);
110 ThingTypeUID mdl = findThingType();
112 return new ThingUID(mdl, uidName);
119 private InetAddress getIpAddress(ServiceInfo service) {
120 InetAddress address = null;
121 for (InetAddress addr : service.getInet4Addresses()) {
122 logger.debug("Get IP address for device {}", addr);
125 // Fallback for Inet6addresses
126 for (InetAddress addr : service.getInet6Addresses()) {
127 logger.debug("Get IP address for device {}", addr);
133 private ThingTypeUID findThingType() {
134 return THING_PRINTER;