]> git.basschouten.com Git - openhab-addons.git/blob
5ae1f96538dff86013b71c51d18b1c03c6bc52be
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.ipp.internal.discovery;
14
15 import java.net.InetAddress;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
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;
32
33 /**
34  * discovers ipp printers announced by mDNS
35  *
36  * @author Tobias Bräutigam - Initial contribution
37  */
38 @Component
39 public class IppPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {
40
41     private final Logger logger = LoggerFactory.getLogger(IppPrinterDiscoveryParticipant.class);
42
43     @Override
44     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
45         return Collections.singleton(IppBindingConstants.PRINTER_THING_TYPE);
46     }
47
48     @Override
49     public String getServiceType() {
50         return "_ipp._tcp.local.";
51     }
52
53     @Override
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);
61                 }
62             }
63         }
64         return null;
65     }
66
67     private String getUIDName(ServiceInfo service) {
68         return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
69     }
70
71     private InetAddress getIpAddress(ServiceInfo service) {
72         InetAddress address = null;
73         for (InetAddress addr : service.getInet4Addresses()) {
74             return addr;
75         }
76         // Fallback for Inet6addresses
77         for (InetAddress addr : service.getInet6Addresses()) {
78             return addr;
79         }
80         return address;
81     }
82
83     @Override
84     public DiscoveryResult createResult(ServiceInfo service) {
85         DiscoveryResult result = null;
86         String rp = service.getPropertyString("rp");
87         if (rp == null) {
88             return null;
89         }
90         ThingUID uid = getThingUID(service);
91         if (uid != null) {
92             Map<String, Object> properties = new HashMap<>(2);
93             // remove the domain from the name
94             InetAddress ip = getIpAddress(service);
95             if (ip == null) {
96                 return null;
97             }
98             String inetAddress = ip.toString().substring(1); // trim leading slash
99
100             String label = service.getName();
101
102             int port = service.getPort();
103
104             properties.put(IppBindingConstants.PRINTER_PARAMETER_URL, "http://" + inetAddress + ":" + port + "/" + rp);
105             properties.put(IppBindingConstants.PRINTER_PARAMETER_NAME, label);
106
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);
110         }
111         return result;
112     }
113 }