2 * Copyright (c) 2010-2023 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.factory;
15 import static org.openhab.binding.ipp.internal.IppBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.ipp.internal.handler.IppPrinterHandler;
20 import org.openhab.core.config.core.Configuration;
21 import org.openhab.core.config.discovery.DiscoveryServiceRegistry;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerFactory;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link IppHandlerFactory} is responsible for creating things and thing
38 * @author Tobias Braeutigam - Initial contribution
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ipp")
42 public class IppHandlerFactory extends BaseThingHandlerFactory {
43 private final Logger logger = LoggerFactory.getLogger(IppHandlerFactory.class);
45 private final DiscoveryServiceRegistry discoveryServiceRegistry;
48 public IppHandlerFactory(final @Reference DiscoveryServiceRegistry discoveryServiceRegistry) {
49 this.discoveryServiceRegistry = discoveryServiceRegistry;
53 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
54 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
58 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
59 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
60 logger.trace("createThing({},{},{},{})", thingTypeUID, configuration, thingUID, bridgeUID);
61 if (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, @Nullable ThingUID thingUID,
70 Configuration configuration) {
71 if (thingUID == null) {
72 String name = (String) configuration.get(PRINTER_PARAMETER_NAME);
73 return new ThingUID(thingTypeUID, name);
79 protected @Nullable ThingHandler createHandler(Thing thing) {
80 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81 if (PRINTER_THING_TYPE.equals(thingTypeUID)) {
82 return new IppPrinterHandler(thing, discoveryServiceRegistry);