2 * Copyright (c) 2010-2020 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
42 @Component(immediate = true)
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 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
76 logger.trace("Created a DiscoveryResult {} for printer on host '{}' name '{}'", result, inetAddress,
80 logger.debug("Found unsupported HP Printer ID: {}", ty);
83 logger.trace("Ignore non HP device {}", ty);
89 private String getUIDName(ServiceInfo service) {
90 return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
94 public String getServiceType() {
95 return "_printer._tcp.local.";
99 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
100 return HPPrinterBindingConstants.SUPPORTED_THING_TYPES_UIDS;
104 public @Nullable ThingUID getThingUID(ServiceInfo service) {
105 if (service.getType() != null) {
106 logger.trace("ServiceInfo: {}", service);
107 if (service.getType().equals(getServiceType())) {
108 String uidName = getUIDName(service);
109 ThingTypeUID mdl = findThingType();
111 return new ThingUID(mdl, uidName);
118 private InetAddress getIpAddress(ServiceInfo service) {
119 InetAddress address = null;
120 for (InetAddress addr : service.getInet4Addresses()) {
121 logger.debug("Get IP address for device {}", addr);
124 // Fallback for Inet6addresses
125 for (InetAddress addr : service.getInet6Addresses()) {
126 logger.debug("Get IP address for device {}", addr);
132 private ThingTypeUID findThingType() {
133 return THING_PRINTER;