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.discovery;
15 import java.util.HashMap;
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;
30 * Base class for discovery services.
32 * @author M. Volaart - Initial contribution
33 * @author Hilbrand Bouwkamp - Refactored code to detect meters during actual discovery phase.
36 public abstract class DSMRDiscoveryService extends AbstractDiscoveryService {
38 * Timeout for discovery time.
40 private static final int DSMR_DISCOVERY_TIMEOUT_SECONDS = 60;
42 private final Logger logger = LoggerFactory.getLogger(DSMRDiscoveryService.class);
45 * Meter Detector instance.
47 protected final DSMRMeterDetector meterDetector = new DSMRMeterDetector();
50 * Constructs a new DSMRMeterDiscoveryService with the specified DSMR Bridge ThingUID
52 public DSMRDiscoveryService() {
53 super(DSMRMeterType.METER_THING_TYPES, DSMR_DISCOVERY_TIMEOUT_SECONDS, false);
57 * Callback when a new meter is discovered
58 * The new meter is described by the {@link DSMRMeterDescriptor}
60 * There will be a DiscoveryResult created and sent to the framework.
62 * At this moment there are no reasons why a new meter will not be accepted.
64 * Therefore this callback will always return true.
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)
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());
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());
80 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
81 .withBridge(dsmrBridgeUID).withProperties(properties).withLabel(meterType.meterKind.getLabelKey())
84 logger.debug("{} for meterDescriptor {}", discoveryResult, meterDescriptor);
85 thingDiscovered(discoveryResult);