Skip to content

Commit

Permalink
Added representation property
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp committed Jan 13, 2022
1 parent a4c579b commit 0d8df7c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.binding.ipp.internal;

import java.util.Collections;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -39,7 +38,8 @@ public class IppBindingConstants {

public static final String PRINTER_PARAMETER_URL = "url";
public static final String PRINTER_PARAMETER_NAME = "name";
public static final String PRINTER_PARAMETER_UUID = "uuid";
public static final String PRINTER_PARAMETER_REFRESH_INTERVAL = "refresh";

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(PRINTER_THING_TYPE);
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(PRINTER_THING_TYPE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
*/
package org.openhab.binding.ipp.internal.discovery;

import static org.openhab.binding.ipp.internal.IppBindingConstants.*;

import java.net.InetAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.jmdns.ServiceInfo;

import org.openhab.binding.ipp.internal.IppBindingConstants;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
Expand All @@ -36,13 +37,14 @@
* @author Tobias Bräutigam - Initial contribution
*/
@Component
@NonNullByDefault
public class IppPrinterDiscoveryParticipant implements MDNSDiscoveryParticipant {

private final Logger logger = LoggerFactory.getLogger(IppPrinterDiscoveryParticipant.class);

@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return Collections.singleton(IppBindingConstants.PRINTER_THING_TYPE);
return Set.of(PRINTER_THING_TYPE);
}

@Override
Expand All @@ -51,15 +53,11 @@ public String getServiceType() {
}

@Override
public ThingUID getThingUID(ServiceInfo service) {
if (service != null) {
logger.trace("ServiceInfo: {}", service);
if (service.getType() != null) {
if (service.getType().equals(getServiceType())) {
String uidName = getUIDName(service);
return new ThingUID(IppBindingConstants.PRINTER_THING_TYPE, uidName);
}
}
public @Nullable ThingUID getThingUID(ServiceInfo service) {
logger.trace("ServiceInfo: {}", service);
if (getServiceType().equals(service.getType())) {
String uidName = getUIDName(service);
return new ThingUID(PRINTER_THING_TYPE, uidName);
}
return null;
}
Expand All @@ -68,46 +66,44 @@ private String getUIDName(ServiceInfo service) {
return service.getName().replaceAll("[^A-Za-z0-9_]", "_");
}

private InetAddress getIpAddress(ServiceInfo service) {
InetAddress address = null;
private @Nullable InetAddress getIpAddress(ServiceInfo service) {
for (InetAddress addr : service.getInet4Addresses()) {
return addr;
}
// Fallback for Inet6addresses
for (InetAddress addr : service.getInet6Addresses()) {
return addr;
}
return address;
return null;
}

@Override
public DiscoveryResult createResult(ServiceInfo service) {
DiscoveryResult result = null;
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
String rp = service.getPropertyString("rp");
if (rp == null) {
return null;
}
ThingUID uid = getThingUID(service);
if (uid != null) {
Map<String, Object> properties = new HashMap<>(2);
// remove the domain from the name
InetAddress ip = getIpAddress(service);
if (ip == null) {
return null;
}
String inetAddress = ip.toString().substring(1); // trim leading slash

String label = service.getName();

int port = service.getPort();
String uuid = service.getPropertyString("UUID");

properties.put(IppBindingConstants.PRINTER_PARAMETER_URL, "http://" + inetAddress + ":" + port + "/" + rp);
properties.put(IppBindingConstants.PRINTER_PARAMETER_NAME, label);
Map<String, Object> properties = Map.of( //
PRINTER_PARAMETER_URL, "http://" + inetAddress + ":" + port + "/" + rp, //
PRINTER_PARAMETER_NAME, label, //
PRINTER_PARAMETER_UUID, uuid //
);

result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label).build();
logger.debug("Created a DiscoveryResult {} for ipp printer on host '{}' name '{}'", result,
properties.get(IppBindingConstants.PRINTER_PARAMETER_URL), label);
return DiscoveryResultBuilder.create(uid).withProperties(properties)
.withRepresentationProperty(PRINTER_PARAMETER_UUID).withLabel(label).build();
}
return result;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.ipp.internal;
package org.openhab.binding.ipp.internal.factory;

import static org.openhab.binding.ipp.internal.IppBindingConstants.PRINTER_THING_TYPE;
import static org.openhab.binding.ipp.internal.IppBindingConstants.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.ipp.internal.handler.IppPrinterHandler;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.config.discovery.DiscoveryServiceRegistry;
Expand All @@ -23,6 +25,7 @@
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
Expand All @@ -35,49 +38,47 @@
* @author Tobias Braeutigam - Initial contribution
*/
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.ipp")
@NonNullByDefault
public class IppHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(IppHandlerFactory.class);

private DiscoveryServiceRegistry discoveryServiceRegistry;
private final DiscoveryServiceRegistry discoveryServiceRegistry;

@Reference
protected void setDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
@Activate
public IppHandlerFactory(final @Reference DiscoveryServiceRegistry discoveryServiceRegistry) {
this.discoveryServiceRegistry = discoveryServiceRegistry;
}

protected void unsetDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
this.discoveryServiceRegistry = null;
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return IppBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
}

@Override
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
ThingUID bridgeUID) {
public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
@Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
logger.trace("createThing({},{},{},{})", thingTypeUID, configuration, thingUID, bridgeUID);
if (IppBindingConstants.PRINTER_THING_TYPE.equals(thingTypeUID)) {
if (PRINTER_THING_TYPE.equals(thingTypeUID)) {
ThingUID deviceUID = getIppPrinterUID(thingTypeUID, thingUID, configuration);
logger.debug("creating thing {} from deviceUID: {}", thingTypeUID, deviceUID);
return super.createThing(thingTypeUID, configuration, deviceUID, null);
}
throw new IllegalArgumentException("The thing type {} " + thingTypeUID + " is not supported by the binding.");
throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
}

private ThingUID getIppPrinterUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
private ThingUID getIppPrinterUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
Configuration configuration) {
if (thingUID == null) {
String name = (String) configuration.get(IppBindingConstants.PRINTER_PARAMETER_NAME);
thingUID = new ThingUID(thingTypeUID, name);
String name = (String) configuration.get(PRINTER_PARAMETER_NAME);
return new ThingUID(thingTypeUID, name);
}
return thingUID;
}

@Override
protected ThingHandler createHandler(Thing thing) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (thingTypeUID.equals(PRINTER_THING_TYPE)) {
if (PRINTER_THING_TYPE.equals(thingTypeUID)) {
return new IppPrinterHandler(thing, discoveryServiceRegistry);
}
return null;
Expand Down
Loading

0 comments on commit 0d8df7c

Please sign in to comment.