]> git.basschouten.com Git - openhab-addons.git/blob
29849631bbc1bed69f1e9188a83d59df39e28930
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.modbus.internal;
14
15 import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal.*;
16
17 import java.util.HashSet;
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.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;
37
38 /**
39  * The {@link ModbusHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Sami Salonen - Initial contribution
43  */
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.modbus")
45 @NonNullByDefault
46 public class ModbusHandlerFactory extends BaseThingHandlerFactory {
47
48     private final Logger logger = LoggerFactory.getLogger(ModbusHandlerFactory.class);
49
50     private @NonNullByDefault({}) ModbusManager manager;
51
52     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>();
53     static {
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);
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
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);
80         }
81         logger.error("createHandler for unknown thing type uid {}. Thing label was: {}", thing.getThingTypeUID(),
82                 thing.getLabel());
83
84         return null;
85     }
86
87     @Reference
88     public void setModbusManager(ModbusManager manager) {
89         logger.debug("Setting manager: {}", manager);
90         this.manager = manager;
91     }
92
93     public void unsetModbusManager(ModbusManager manager) {
94         this.manager = null;
95     }
96 }