2 * Copyright (c) 2010-2023 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.modbus.internal;
15 import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal.*;
17 import java.util.HashSet;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.modbus.handler.ModbusPollerThingHandler;
23 import org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler;
24 import org.openhab.binding.modbus.internal.handler.ModbusSerialThingHandler;
25 import org.openhab.binding.modbus.internal.handler.ModbusTcpThingHandler;
26 import org.openhab.core.io.transport.modbus.ModbusManager;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link ModbusHandlerFactory} is responsible for creating things and thing
42 * @author Sami Salonen - Initial contribution
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.modbus")
46 public class ModbusHandlerFactory extends BaseThingHandlerFactory {
48 private final Logger logger = LoggerFactory.getLogger(ModbusHandlerFactory.class);
50 private @NonNullByDefault({}) ModbusManager manager;
52 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>();
54 SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_MODBUS_TCP);
55 SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_MODBUS_SERIAL);
56 SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_MODBUS_POLLER);
57 SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_MODBUS_DATA);
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68 if (thingTypeUID.equals(THING_TYPE_MODBUS_TCP)) {
69 logger.debug("createHandler Modbus tcp");
70 return new ModbusTcpThingHandler((Bridge) thing, manager);
71 } else if (thingTypeUID.equals(THING_TYPE_MODBUS_SERIAL)) {
72 logger.debug("createHandler Modbus serial");
73 return new ModbusSerialThingHandler((Bridge) thing, manager);
74 } else if (thingTypeUID.equals(THING_TYPE_MODBUS_POLLER)) {
75 logger.debug("createHandler Modbus poller");
76 return new ModbusPollerThingHandler((Bridge) thing);
77 } else if (thingTypeUID.equals(THING_TYPE_MODBUS_DATA)) {
78 logger.debug("createHandler data");
79 return new ModbusDataThingHandler(thing);
81 logger.error("createHandler for unknown thing type uid {}. Thing label was: {}", thing.getThingTypeUID(),
88 public void setModbusManager(ModbusManager manager) {
89 logger.debug("Setting manager: {}", manager);
90 this.manager = manager;
93 public void unsetModbusManager(ModbusManager manager) {