]> git.basschouten.com Git - openhab-addons.git/blob
7d13afe28860a3a5a27310f2b36edca09e1e7ff3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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                 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
75                         .build();
76                 logger.trace("Created a DiscoveryResult {} for printer on host '{}' name '{}'", result, inetAddress,
77                         label);
78                 return result;
79             } else {
80                 logger.debug("Found unsupported HP Printer ID: {}", ty);
81             }
82         } else {
83             logger.trace("Ignore non HP device {}", ty);
84         }
85
86         return null;
87     }
88
89     private String getUIDName(ServiceInfo service) {
90         return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
91     }
92
93     @Override
94     public String getServiceType() {
95         return "_printer._tcp.local.";
96     }
97
98     @Override
99     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
100         return HPPrinterBindingConstants.SUPPORTED_THING_TYPES_UIDS;
101     }
102
103     @Override
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();
110
111                 return new ThingUID(mdl, uidName);
112             }
113         }
114         return null;
115     }
116
117     @Nullable
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);
122             return addr;
123         }
124         // Fallback for Inet6addresses
125         for (InetAddress addr : service.getInet6Addresses()) {
126             logger.debug("Get IP address for device {}", addr);
127             return addr;
128         }
129         return address;
130     }
131
132     private ThingTypeUID findThingType() {
133         return THING_PRINTER;
134     }
135 }