2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipp.internal;
15 import static org.openhab.binding.ipp.internal.IppBindingConstants.PRINTER_THING_TYPE;
17 import org.openhab.binding.ipp.internal.handler.IppPrinterHandler;
18 import org.openhab.core.config.core.Configuration;
19 import org.openhab.core.config.discovery.DiscoveryServiceRegistry;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingTypeUID;
22 import org.openhab.core.thing.ThingUID;
23 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.openhab.core.thing.binding.ThingHandlerFactory;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link IppHandlerFactory} is responsible for creating things and thing
35 * @author Tobias Braeutigam - Initial contribution
37 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ipp")
38 public class IppHandlerFactory extends BaseThingHandlerFactory {
39 private final Logger logger = LoggerFactory.getLogger(IppHandlerFactory.class);
41 private DiscoveryServiceRegistry discoveryServiceRegistry;
44 protected void setDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
45 this.discoveryServiceRegistry = discoveryServiceRegistry;
48 protected void unsetDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
49 this.discoveryServiceRegistry = null;
53 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
54 return IppBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
58 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
60 logger.trace("createThing({},{},{},{})", thingTypeUID, configuration, thingUID, bridgeUID);
61 if (IppBindingConstants.PRINTER_THING_TYPE.equals(thingTypeUID)) {
62 ThingUID deviceUID = getIppPrinterUID(thingTypeUID, thingUID, configuration);
63 logger.debug("creating thing {} from deviceUID: {}", thingTypeUID, deviceUID);
64 return super.createThing(thingTypeUID, configuration, deviceUID, null);
66 throw new IllegalArgumentException("The thing type {} " + thingTypeUID + " is not supported by the binding.");
69 private ThingUID getIppPrinterUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
70 if (thingUID == null) {
71 String name = (String) configuration.get(IppBindingConstants.PRINTER_PARAMETER_NAME);
72 thingUID = new ThingUID(thingTypeUID, name);
78 protected ThingHandler createHandler(Thing thing) {
79 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
80 if (thingTypeUID.equals(PRINTER_THING_TYPE)) {
81 return new IppPrinterHandler(thing, discoveryServiceRegistry);