]> git.basschouten.com Git - openhab-addons.git/blob
e28a1dbdf5c889af8968948488092db3fc8d23fb
[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.dsmr.internal;
14
15 import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.*;
16
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;
34
35 /**
36  * The {@link DSMRHandlerFactory} is responsible for creating things and thing handlers.
37  *
38  * @author M. Volaart - Initial contribution
39  * @author Hilbrand Bouwkamp - Refactored discovery service to use standard discovery class methods.
40  */
41 @NonNullByDefault
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.dsmr")
43 public class DSMRHandlerFactory extends BaseThingHandlerFactory {
44
45     private final Logger logger = LoggerFactory.getLogger(DSMRHandlerFactory.class);
46
47     private final SerialPortManager serialPortManager;
48
49     @Activate
50     public DSMRHandlerFactory(@Reference SerialPortManager serialPortManager) {
51         this.serialPortManager = serialPortManager;
52     }
53
54     /**
55      * Returns if the specified ThingTypeUID is supported by this handler.
56      *
57      * This handler support the THING_TYPE_DSMR_BRIDGE type and all ThingTypesUID that
58      * belongs to the supported DSMRMeterType objects
59      *
60      * @param thingTypeUID {@link ThingTypeUID} to check
61      * @return true if the specified ThingTypeUID is supported, false otherwise
62      */
63     @Override
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);
67             return true;
68         } else {
69             boolean thingTypeUIDIsMeter = DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID);
70
71             if (thingTypeUIDIsMeter) {
72                 logger.trace("{} is a supported DSMR Meter thing", thingTypeUID);
73             }
74             return thingTypeUIDIsMeter;
75         }
76     }
77
78     /**
79      * Create the ThingHandler for the corresponding Thing
80      *
81      * There are two handlers supported:
82      * - DSMRBridgeHandler that handle the Thing that corresponds to the physical DSMR device and does the serial
83      * communication
84      * - MeterHandler that handles the Meter things that are a logical part of the physical device
85      *
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
88      */
89     @Override
90     protected @Nullable ThingHandler createHandler(Thing thing) {
91         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92         logger.debug("Searching for thingTypeUID {}", thingTypeUID);
93
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);
98         }
99
100         return null;
101     }
102 }