2 * Copyright (c) 2010-2021 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 java.util.HashMap;
18 import java.util.Hashtable;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.dsmr.internal.discovery.DSMRMeterDiscoveryService;
24 import org.openhab.binding.dsmr.internal.handler.DSMRBridgeHandler;
25 import org.openhab.binding.dsmr.internal.handler.DSMRMeterHandler;
26 import org.openhab.binding.dsmr.internal.meter.DSMRMeterType;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.i18n.LocaleProvider;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.io.transport.serial.SerialPortManager;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link DSMRHandlerFactory} is responsible for creating things and thing handlers.
47 * @author M. Volaart - Initial contribution
48 * @author Hilbrand Bouwkamp - Refactored discovery service to use standard discovery class methods.
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.dsmr")
52 public class DSMRHandlerFactory extends BaseThingHandlerFactory {
54 private final Logger logger = LoggerFactory.getLogger(DSMRHandlerFactory.class);
56 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
58 private @NonNullByDefault({}) SerialPortManager serialPortManager;
59 private @NonNullByDefault({}) LocaleProvider localeProvider;
60 private @NonNullByDefault({}) TranslationProvider i18nProvider;
63 * Returns if the specified ThingTypeUID is supported by this handler.
65 * This handler support the THING_TYPE_DSMR_BRIDGE type and all ThingTypesUID that
66 * belongs to the supported DSMRMeterType objects
68 * @param thingTypeUID {@link ThingTypeUID} to check
69 * @return true if the specified ThingTypeUID is supported, false otherwise
72 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73 if (THING_TYPE_DSMR_BRIDGE.equals(thingTypeUID) || THING_TYPE_SMARTY_BRIDGE.equals(thingTypeUID)) {
74 logger.debug("DSMR Bridge Thing {} supported", thingTypeUID);
77 boolean thingTypeUIDIsMeter = DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID);
79 if (thingTypeUIDIsMeter) {
80 logger.trace("{} is a supported DSMR Meter thing", thingTypeUID);
82 return thingTypeUIDIsMeter;
87 * Create the ThingHandler for the corresponding Thing
89 * There are two handlers supported:
90 * - DSMRBridgeHandler that handle the Thing that corresponds to the physical DSMR device and does the serial
92 * - MeterHandler that handles the Meter things that are a logical part of the physical device
94 * @param thing The Thing to create a ThingHandler for
95 * @return ThingHandler for the given Thing or null if the Thing is not supported
98 protected @Nullable ThingHandler createHandler(Thing thing) {
99 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
100 logger.debug("Searching for thingTypeUID {}", thingTypeUID);
102 if (THING_TYPE_DSMR_BRIDGE.equals(thingTypeUID) || THING_TYPE_SMARTY_BRIDGE.equals(thingTypeUID)) {
103 DSMRBridgeHandler handler = new DSMRBridgeHandler((Bridge) thing, serialPortManager);
104 registerDiscoveryService(handler);
106 } else if (DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID)) {
107 return new DSMRMeterHandler(thing);
114 * Registers a meter discovery service for the bridge handler.
116 * @param bridgeHandler handler to register service for
118 private synchronized void registerDiscoveryService(DSMRBridgeHandler bridgeHandler) {
119 DSMRMeterDiscoveryService discoveryService = new DSMRMeterDiscoveryService(bridgeHandler);
121 discoveryService.setLocaleProvider(localeProvider);
122 discoveryService.setTranslationProvider(i18nProvider);
123 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
124 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
128 protected synchronized void removeHandler(ThingHandler thingHandler) {
129 if (thingHandler instanceof DSMRBridgeHandler) {
130 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
131 if (serviceReg != null) {
132 DSMRMeterDiscoveryService service = (DSMRMeterDiscoveryService) getBundleContext()
133 .getService(serviceReg.getReference());
134 serviceReg.unregister();
135 if (service != null) {
136 service.unsetLocaleProvider();
137 service.unsetTranslationProvider();
144 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
145 this.serialPortManager = serialPortManager;
148 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
149 this.serialPortManager = null;
153 protected void setLocaleProvider(final LocaleProvider localeProvider) {
154 this.localeProvider = localeProvider;
157 protected void unsetLocaleProvider(final LocaleProvider localeProvider) {
158 this.localeProvider = null;
162 protected void setTranslationProvider(TranslationProvider i18nProvider) {
163 this.i18nProvider = i18nProvider;
166 protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
167 this.i18nProvider = null;