]> git.basschouten.com Git - openhab-addons.git/blob
48cf29a0da098c245f06c71cf4b202310ffa7b65
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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;
14
15 import static org.openhab.binding.ipp.internal.IppBindingConstants.PRINTER_THING_TYPE;
16
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;
30
31 /**
32  * The {@link IppHandlerFactory} is responsible for creating things and thing
33  * handlers.
34  *
35  * @author Tobias Braeutigam - Initial contribution
36  */
37 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.ipp")
38 public class IppHandlerFactory extends BaseThingHandlerFactory {
39     private final Logger logger = LoggerFactory.getLogger(IppHandlerFactory.class);
40
41     private DiscoveryServiceRegistry discoveryServiceRegistry;
42
43     @Reference
44     protected void setDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
45         this.discoveryServiceRegistry = discoveryServiceRegistry;
46     }
47
48     protected void unsetDiscoveryServiceRegistry(DiscoveryServiceRegistry discoveryServiceRegistry) {
49         this.discoveryServiceRegistry = null;
50     }
51
52     @Override
53     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
54         return IppBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55     }
56
57     @Override
58     public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
59             ThingUID bridgeUID) {
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);
65         }
66         throw new IllegalArgumentException("The thing type {} " + thingTypeUID + " is not supported by the binding.");
67     }
68
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);
73         }
74         return thingUID;
75     }
76
77     @Override
78     protected ThingHandler createHandler(Thing thing) {
79         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
80         if (thingTypeUID.equals(PRINTER_THING_TYPE)) {
81             return new IppPrinterHandler(thing, discoveryServiceRegistry);
82         }
83         return null;
84     }
85 }