]> git.basschouten.com Git - openhab-addons.git/blob
1ec0421cdb88d4c280efb65633409e2ed5b0a5f5
[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.ipp.internal.factory;
14
15 import static org.openhab.binding.ipp.internal.IppBindingConstants.*;
16
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;
33
34 /**
35  * The {@link IppHandlerFactory} is responsible for creating things and thing
36  * handlers.
37  *
38  * @author Tobias Braeutigam - Initial contribution
39  */
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ipp")
41 @NonNullByDefault
42 public class IppHandlerFactory extends BaseThingHandlerFactory {
43     private final Logger logger = LoggerFactory.getLogger(IppHandlerFactory.class);
44
45     private final DiscoveryServiceRegistry discoveryServiceRegistry;
46
47     @Activate
48     public IppHandlerFactory(final @Reference DiscoveryServiceRegistry discoveryServiceRegistry) {
49         this.discoveryServiceRegistry = discoveryServiceRegistry;
50     }
51
52     @Override
53     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
54         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55     }
56
57     @Override
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);
65         }
66         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
67     }
68
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);
74         }
75         return thingUID;
76     }
77
78     @Override
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);
83         }
84         return null;
85     }
86 }