]> git.basschouten.com Git - openhab-addons.git/blob
eb54454297994ecb4bfdcbbeff42008d1fcc659c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.meteostick.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.meteostick.internal.handler.MeteostickBridgeHandler;
18 import org.openhab.binding.meteostick.internal.handler.MeteostickSensorHandler;
19 import org.openhab.core.io.transport.serial.SerialPortManager;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingTypeUID;
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.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link MeteostickHandlerFactory} is responsible for creating things and thing
34  * handlers.
35  *
36  * @author Chris Jackson - Initial contribution
37  */
38 @NonNullByDefault
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.meteostick")
40 public class MeteostickHandlerFactory extends BaseThingHandlerFactory {
41     private Logger logger = LoggerFactory.getLogger(MeteostickHandlerFactory.class);
42
43     private final SerialPortManager serialPortManager;
44
45     @Activate
46     public MeteostickHandlerFactory(final @Reference SerialPortManager serialPortManager) {
47         this.serialPortManager = serialPortManager;
48     }
49
50     @Override
51     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
52         return MeteostickBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
53                 | MeteostickSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID);
54     }
55
56     @Override
57     protected @Nullable ThingHandler createHandler(Thing thing) {
58         logger.debug("MeteoStick thing factory: createHandler {} of type {}", thing.getThingTypeUID(), thing.getUID());
59
60         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
61
62         if (MeteostickBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
63             return new MeteostickBridgeHandler((Bridge) thing, serialPortManager);
64         }
65
66         if (MeteostickSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
67             return new MeteostickSensorHandler(thing);
68         }
69
70         return null;
71     }
72 }