]> git.basschouten.com Git - openhab-addons.git/blob
ebc0a4a5ba5e79fb8114bd6e06a3ec323443c2f0
[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.hpprinter.internal;
14
15 import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.THING_PRINTER;
16
17 import java.net.InetAddress;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.jmdns.ServiceInfo;
23
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;
34
35 /**
36  * The {@link HPPrinterDiscoveryParticipant} class discovers HP Printers over
37  * mDNS.
38  *
39  * @author Stewart Cossey - Initial contribution
40  */
41 @NonNullByDefault
42 @Component
43 public class HPPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {
44
45     private final Logger logger = LoggerFactory.getLogger(HPPrinterDiscoveryParticipant.class);
46
47     @Override
48     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
49         if (!service.hasData()) {
50             logger.trace("Service has no data.");
51             return null;
52         }
53
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");
58             if (rp == null) {
59                 return null;
60             }
61
62             logger.debug("Found HP Printer ID: {}", ty);
63             ThingUID uid = getThingUID(service);
64             if (uid != null) {
65                 Map<String, Object> properties = new HashMap<>(2);
66                 InetAddress ip = getIpAddress(service);
67                 if (ip == null) {
68                     return null;
69                 }
70                 String inetAddress = ip.toString().substring(1); // trim leading slash
71                 String label = service.getName();
72
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,
78                         label);
79                 return result;
80             } else {
81                 logger.debug("Found unsupported HP Printer ID: {}", ty);
82             }
83         } else {
84             logger.trace("Ignore non HP device {}", ty);
85         }
86
87         return null;
88     }
89
90     private String getUIDName(ServiceInfo service) {
91         return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
92     }
93
94     @Override
95     public String getServiceType() {
96         return "_printer._tcp.local.";
97     }
98
99     @Override
100     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
101         return HPPrinterBindingConstants.SUPPORTED_THING_TYPES_UIDS;
102     }
103
104     @Override
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();
111
112                 return new ThingUID(mdl, uidName);
113             }
114         }
115         return null;
116     }
117
118     @Nullable
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);
123             return addr;
124         }
125         // Fallback for Inet6addresses
126         for (InetAddress addr : service.getInet6Addresses()) {
127             logger.debug("Get IP address for device {}", addr);
128             return addr;
129         }
130         return address;
131     }
132
133     private ThingTypeUID findThingType() {
134         return THING_PRINTER;
135     }
136 }