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.dsmr.internal;
15 import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.dsmr.internal.handler.DSMRBridgeHandler;
20 import org.openhab.binding.dsmr.internal.handler.DSMRMeterHandler;
21 import org.openhab.binding.dsmr.internal.meter.DSMRMeterType;
22 import org.openhab.core.io.transport.serial.SerialPortManager;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link DSMRHandlerFactory} is responsible for creating things and thing handlers.
38 * @author M. Volaart - Initial contribution
39 * @author Hilbrand Bouwkamp - Refactored discovery service to use standard discovery class methods.
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.dsmr")
43 public class DSMRHandlerFactory extends BaseThingHandlerFactory {
45 private final Logger logger = LoggerFactory.getLogger(DSMRHandlerFactory.class);
47 private final SerialPortManager serialPortManager;
50 public DSMRHandlerFactory(@Reference SerialPortManager serialPortManager) {
51 this.serialPortManager = serialPortManager;
55 * Returns if the specified ThingTypeUID is supported by this handler.
57 * This handler support the THING_TYPE_DSMR_BRIDGE type and all ThingTypesUID that
58 * belongs to the supported DSMRMeterType objects
60 * @param thingTypeUID {@link ThingTypeUID} to check
61 * @return true if the specified ThingTypeUID is supported, false otherwise
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 if (THING_TYPE_DSMR_BRIDGE.equals(thingTypeUID) || THING_TYPE_SMARTY_BRIDGE.equals(thingTypeUID)) {
66 logger.debug("DSMR Bridge Thing {} supported", thingTypeUID);
69 boolean thingTypeUIDIsMeter = DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID);
71 if (thingTypeUIDIsMeter) {
72 logger.trace("{} is a supported DSMR Meter thing", thingTypeUID);
74 return thingTypeUIDIsMeter;
79 * Create the ThingHandler for the corresponding Thing
81 * There are two handlers supported:
82 * - DSMRBridgeHandler that handle the Thing that corresponds to the physical DSMR device and does the serial
84 * - MeterHandler that handles the Meter things that are a logical part of the physical device
86 * @param thing The Thing to create a ThingHandler for
87 * @return ThingHandler for the given Thing or null if the Thing is not supported
90 protected @Nullable ThingHandler createHandler(Thing thing) {
91 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92 logger.debug("Searching for thingTypeUID {}", thingTypeUID);
94 if (THING_TYPE_DSMR_BRIDGE.equals(thingTypeUID) || THING_TYPE_SMARTY_BRIDGE.equals(thingTypeUID)) {
95 return new DSMRBridgeHandler((Bridge) thing, serialPortManager);
96 } else if (DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID)) {
97 return new DSMRMeterHandler(thing);