2 * Copyright (c) 2010-2024 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.serial.internal;
15 import static org.openhab.binding.serial.internal.SerialBindingConstants.THING_TYPE_BRIDGE;
16 import static org.openhab.binding.serial.internal.SerialBindingConstants.THING_TYPE_DEVICE;
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;
41 * The {@link SerialHandlerFactory} is responsible for creating things and thing
44 * @author Mike Major - Initial contribution
47 @Component(configurationPid = "binding.serial", service = ThingHandlerFactory.class)
48 public class SerialHandlerFactory extends BaseThingHandlerFactory implements ValueTransformationProvider {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_BRIDGE, THING_TYPE_DEVICE);
52 private final SerialPortManager serialPortManager;
55 public SerialHandlerFactory(@Reference final SerialPortManager serialPortManager) {
56 this.serialPortManager = serialPortManager;
60 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65 protected @Nullable ThingHandler createHandler(final Thing thing) {
66 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);
78 public ValueTransformation getValueTransformation(@Nullable final String pattern) {
79 if (pattern == null) {
80 return NoOpValueTransformation.getInstance();
82 return new CascadedValueTransformationImpl(pattern,
83 name -> TransformationHelper.getTransformationService(bundleContext, name));