]> git.basschouten.com Git - openhab-addons.git/blob
776428c0b01ae5d7012b3b082a8bcda965abb54d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.serial.internal;
14
15 import static org.openhab.binding.serial.internal.SerialBindingConstants.THING_TYPE_BRIDGE;
16 import static org.openhab.binding.serial.internal.SerialBindingConstants.THING_TYPE_DEVICE;
17
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.serial.internal.handler.SerialBridgeHandler;
23 import org.openhab.binding.serial.internal.handler.SerialDeviceHandler;
24 import org.openhab.binding.serial.internal.transform.CascadedValueTransformationImpl;
25 import org.openhab.binding.serial.internal.transform.NoOpValueTransformation;
26 import org.openhab.binding.serial.internal.transform.ValueTransformation;
27 import org.openhab.binding.serial.internal.transform.ValueTransformationProvider;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.openhab.core.transform.TransformationHelper;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39
40 /**
41  * The {@link SerialHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Mike Major - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(configurationPid = "binding.serial", service = ThingHandlerFactory.class)
48 public class SerialHandlerFactory extends BaseThingHandlerFactory implements ValueTransformationProvider {
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, THING_TYPE_DEVICE);
51
52     private final SerialPortManager serialPortManager;
53
54     @Activate
55     public SerialHandlerFactory(@Reference final SerialPortManager serialPortManager) {
56         this.serialPortManager = serialPortManager;
57     }
58
59     @Override
60     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62     }
63
64     @Override
65     protected @Nullable ThingHandler createHandler(final Thing thing) {
66         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
67
68         if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
69             return new SerialBridgeHandler((Bridge) thing, serialPortManager);
70         } else if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
71             return new SerialDeviceHandler(thing, this);
72         }
73
74         return null;
75     }
76
77     @Override
78     public ValueTransformation getValueTransformation(@Nullable final String pattern) {
79         if (pattern == null) {
80             return NoOpValueTransformation.getInstance();
81         }
82         return new CascadedValueTransformationImpl(pattern,
83                 name -> TransformationHelper.getTransformationService(bundleContext, name));
84     }
85 }