]> git.basschouten.com Git - openhab-addons.git/blob
26cfe9da2677f79a8ff81387908a1321ce0f0700
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
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;
43
44 /**
45  * The {@link DSMRHandlerFactory} is responsible for creating things and thing handlers.
46  *
47  * @author M. Volaart - Initial contribution
48  * @author Hilbrand Bouwkamp - Refactored discovery service to use standard discovery class methods.
49  */
50 @NonNullByDefault
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.dsmr")
52 public class DSMRHandlerFactory extends BaseThingHandlerFactory {
53
54     private final Logger logger = LoggerFactory.getLogger(DSMRHandlerFactory.class);
55
56     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57
58     private @NonNullByDefault({}) SerialPortManager serialPortManager;
59     private @NonNullByDefault({}) LocaleProvider localeProvider;
60     private @NonNullByDefault({}) TranslationProvider i18nProvider;
61
62     /**
63      * Returns if the specified ThingTypeUID is supported by this handler.
64      *
65      * This handler support the THING_TYPE_DSMR_BRIDGE type and all ThingTypesUID that
66      * belongs to the supported DSMRMeterType objects
67      *
68      * @param thingTypeUID {@link ThingTypeUID} to check
69      * @return true if the specified ThingTypeUID is supported, false otherwise
70      */
71     @Override
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);
75             return true;
76         } else {
77             boolean thingTypeUIDIsMeter = DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID);
78
79             if (thingTypeUIDIsMeter) {
80                 logger.trace("{} is a supported DSMR Meter thing", thingTypeUID);
81             }
82             return thingTypeUIDIsMeter;
83         }
84     }
85
86     /**
87      * Create the ThingHandler for the corresponding Thing
88      *
89      * There are two handlers supported:
90      * - DSMRBridgeHandler that handle the Thing that corresponds to the physical DSMR device and does the serial
91      * communication
92      * - MeterHandler that handles the Meter things that are a logical part of the physical device
93      *
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
96      */
97     @Override
98     protected @Nullable ThingHandler createHandler(Thing thing) {
99         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
100         logger.debug("Searching for thingTypeUID {}", thingTypeUID);
101
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);
105             return handler;
106         } else if (DSMRMeterType.METER_THING_TYPES.contains(thingTypeUID)) {
107             return new DSMRMeterHandler(thing);
108         }
109
110         return null;
111     }
112
113     /**
114      * Registers a meter discovery service for the bridge handler.
115      *
116      * @param bridgeHandler handler to register service for
117      */
118     private synchronized void registerDiscoveryService(DSMRBridgeHandler bridgeHandler) {
119         DSMRMeterDiscoveryService discoveryService = new DSMRMeterDiscoveryService(bridgeHandler);
120
121         discoveryService.setLocaleProvider(localeProvider);
122         discoveryService.setTranslationProvider(i18nProvider);
123         this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
124                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
125     }
126
127     @Override
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();
138                 }
139             }
140         }
141     }
142
143     @Reference
144     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
145         this.serialPortManager = serialPortManager;
146     }
147
148     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
149         this.serialPortManager = null;
150     }
151
152     @Reference
153     protected void setLocaleProvider(final LocaleProvider localeProvider) {
154         this.localeProvider = localeProvider;
155     }
156
157     protected void unsetLocaleProvider(final LocaleProvider localeProvider) {
158         this.localeProvider = null;
159     }
160
161     @Reference
162     protected void setTranslationProvider(TranslationProvider i18nProvider) {
163         this.i18nProvider = i18nProvider;
164     }
165
166     protected void unsetTranslationProvider(TranslationProvider i18nProvider) {
167         this.i18nProvider = null;
168     }
169 }