]> git.basschouten.com Git - openhab-addons.git/blob
0ef4d3e8558d641dd67631e3ad399d96a9078f54
[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.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.dsmr.internal.meter.DSMRMeterDescriptor;
20 import org.openhab.binding.dsmr.internal.meter.DSMRMeterType;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Base class for discovery services.
31  *
32  * @author M. Volaart - Initial contribution
33  * @author Hilbrand Bouwkamp - Refactored code to detect meters during actual discovery phase.
34  */
35 @NonNullByDefault
36 public abstract class DSMRDiscoveryService extends AbstractDiscoveryService {
37     /**
38      * Timeout for discovery time.
39      */
40     private static final int DSMR_DISCOVERY_TIMEOUT_SECONDS = 60;
41
42     private final Logger logger = LoggerFactory.getLogger(DSMRDiscoveryService.class);
43
44     /**
45      * Meter Detector instance.
46      */
47     protected final DSMRMeterDetector meterDetector = new DSMRMeterDetector();
48
49     /**
50      * Constructs a new DSMRMeterDiscoveryService with the specified DSMR Bridge ThingUID
51      */
52     public DSMRDiscoveryService() {
53         super(DSMRMeterType.METER_THING_TYPES, DSMR_DISCOVERY_TIMEOUT_SECONDS, false);
54     }
55
56     /**
57      * Callback when a new meter is discovered
58      * The new meter is described by the {@link DSMRMeterDescriptor}
59      *
60      * There will be a DiscoveryResult created and sent to the framework.
61      *
62      * At this moment there are no reasons why a new meter will not be accepted.
63      *
64      * Therefore this callback will always return true.
65      *
66      * @param meterDescriptor the descriptor of the new detected meter
67      * @param dsmrBridgeUID ThingUID for the DSMR Bridges
68      * @return true (meter is always accepted)
69      */
70     public boolean meterDiscovered(DSMRMeterDescriptor meterDescriptor, ThingUID dsmrBridgeUID) {
71         DSMRMeterType meterType = meterDescriptor.getMeterType();
72         ThingTypeUID thingTypeUID = meterType.getThingTypeUID();
73         ThingUID thingUID = new ThingUID(thingTypeUID, dsmrBridgeUID, meterDescriptor.getChannelId());
74
75         // Construct the configuration for this meter
76         Map<String, Object> properties = new HashMap<>();
77         properties.put("meterType", meterType.name());
78         properties.put("channel", meterDescriptor.getChannel());
79
80         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
81                 .withBridge(dsmrBridgeUID).withProperties(properties).withLabel(meterType.meterKind.getLabelKey())
82                 .build();
83
84         logger.debug("{} for meterDescriptor {}", discoveryResult, meterDescriptor);
85         thingDiscovered(discoveryResult);
86
87         return true;
88     }
89 }