]> git.basschouten.com Git - openhab-addons.git/blob
c0fd592637b833f21877f3c8d2f5330fe58550c1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 static org.openhab.binding.ipp.internal.IppBindingConstants.*;
16
17 import java.net.InetAddress;
18 import java.util.Map;
19 import java.util.Set;
20
21 import javax.jmdns.ServiceInfo;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
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;
33
34 /**
35  * discovers ipp printers announced by mDNS
36  *
37  * @author Tobias Bräutigam - Initial contribution
38  */
39 @Component
40 @NonNullByDefault
41 public class IppPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {
42
43     private final Logger logger = LoggerFactory.getLogger(IppPrinterDiscoveryParticipant.class);
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return Set.of(PRINTER_THING_TYPE);
48     }
49
50     @Override
51     public String getServiceType() {
52         return "_ipp._tcp.local.";
53     }
54
55     @Override
56     public @Nullable ThingUID getThingUID(ServiceInfo service) {
57         logger.trace("ServiceInfo: {}", service);
58         if (getServiceType().equals(service.getType())) {
59             String uidName = getUIDName(service);
60             return new ThingUID(PRINTER_THING_TYPE, uidName);
61         }
62         return null;
63     }
64
65     private String getUIDName(ServiceInfo service) {
66         return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
67     }
68
69     private @Nullable InetAddress getIpAddress(ServiceInfo service) {
70         for (InetAddress addr : service.getInet4Addresses()) {
71             return addr;
72         }
73         // Fallback for Inet6addresses
74         for (InetAddress addr : service.getInet6Addresses()) {
75             return addr;
76         }
77         return null;
78     }
79
80     @Override
81     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
82         String rp = service.getPropertyString("rp");
83         if (rp == null) {
84             return null;
85         }
86         ThingUID uid = getThingUID(service);
87         if (uid != null) {
88             // remove the domain from the name
89             InetAddress ip = getIpAddress(service);
90             if (ip == null) {
91                 return null;
92             }
93             String inetAddress = ip.toString().substring(1); // trim leading slash
94             String label = service.getName();
95             int port = service.getPort();
96             String uuid = service.getPropertyString("UUID");
97
98             Map<String, Object> properties = Map.of( //
99                     PRINTER_PARAMETER_URL, "http://" + inetAddress + ":" + port + "/" + rp, //
100                     PRINTER_PARAMETER_NAME, label, //
101                     PRINTER_PARAMETER_UUID, uuid //
102             );
103
104             return DiscoveryResultBuilder.create(uid).withProperties(properties)
105                     .withRepresentationProperty(PRINTER_PARAMETER_UUID).withLabel(label).build();
106         }
107         return null;
108     }
109 }