2 * Copyright (c) 2010-2021 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.ipp.internal.discovery;
15 import java.net.InetAddress;
16 import java.util.Collections;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
23 import org.openhab.binding.ipp.internal.IppBindingConstants;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
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;
34 * discovers ipp printers announced by mDNS
36 * @author Tobias Bräutigam - Initial contribution
39 public class IppPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {
41 private final Logger logger = LoggerFactory.getLogger(IppPrinterDiscoveryParticipant.class);
44 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45 return Collections.singleton(IppBindingConstants.PRINTER_THING_TYPE);
49 public String getServiceType() {
50 return "_ipp._tcp.local.";
54 public ThingUID getThingUID(ServiceInfo service) {
55 if (service != null) {
56 logger.trace("ServiceInfo: {}", service);
57 if (service.getType() != null) {
58 if (service.getType().equals(getServiceType())) {
59 String uidName = getUIDName(service);
60 return new ThingUID(IppBindingConstants.PRINTER_THING_TYPE, uidName);
67 private String getUIDName(ServiceInfo service) {
68 return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
71 private InetAddress getIpAddress(ServiceInfo service) {
72 InetAddress address = null;
73 for (InetAddress addr : service.getInet4Addresses()) {
76 // Fallback for Inet6addresses
77 for (InetAddress addr : service.getInet6Addresses()) {
84 public DiscoveryResult createResult(ServiceInfo service) {
85 DiscoveryResult result = null;
86 String rp = service.getPropertyString("rp");
90 ThingUID uid = getThingUID(service);
92 Map<String, Object> properties = new HashMap<>(2);
93 // remove the domain from the name
94 InetAddress ip = getIpAddress(service);
98 String inetAddress = ip.toString().substring(1); // trim leading slash
100 String label = service.getName();
102 int port = service.getPort();
104 properties.put(IppBindingConstants.PRINTER_PARAMETER_URL, "http://" + inetAddress + ":" + port + "/" + rp);
105 properties.put(IppBindingConstants.PRINTER_PARAMETER_NAME, label);
107 result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
108 logger.debug("Created a DiscoveryResult {} for ipp printer on host '{}' name '{}'", result,
109 properties.get(IppBindingConstants.PRINTER_PARAMETER_URL), label);