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.modbus.discovery.internal;
15 import java.util.List;
16 import java.util.concurrent.CopyOnWriteArrayList;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.modbus.discovery.ModbusDiscoveryListener;
21 import org.openhab.binding.modbus.discovery.ModbusDiscoveryParticipant;
22 import org.openhab.binding.modbus.handler.ModbusEndpointThingHandler;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * A new instance of this class is created for each Modbus endpoint handler
30 * that supports discovery.
31 * This service gets called each time a discovery is requested, and it is
32 * responsible to execute the discovery on the connected thing handler.
33 * Actual discovery is done by the registered ModbusDiscoveryparticipants
35 * @author Nagy Attila Gabor - initial contribution
39 public class ModbusEndpointDiscoveryService implements ModbusThingHandlerDiscoveryService {
41 private final Logger logger = LoggerFactory.getLogger(ModbusEndpointDiscoveryService.class);
43 // This is the handler we will do the discovery on
44 private @Nullable ModbusEndpointThingHandler handler;
46 // List of the registered participants
47 // this only contains data when there is scan in progress
48 private final List<ModbusDiscoveryParticipant> participants = new CopyOnWriteArrayList<>();
50 // This is set true when we're waiting for a participant to finish discovery
51 private boolean waitingForParticipant = false;
54 public void setThingHandler(@Nullable ThingHandler handler) {
55 if (handler instanceof ModbusEndpointThingHandler) {
56 this.handler = (ModbusEndpointThingHandler) handler;
61 public @Nullable ThingHandler getThingHandler() {
62 return (ThingHandler) handler;
66 public boolean startScan(ModbusDiscoveryService service) {
67 ModbusEndpointThingHandler handler = this.handler;
68 if (handler == null || !handler.isDiscoveryEnabled()) {
71 logger.trace("Starting discovery on endpoint {}", handler.getUID().getAsString());
73 participants.addAll(service.getDiscoveryParticipants());
75 startNextParticipant(handler, service);
81 public boolean scanInProgress() {
82 return !participants.isEmpty() || waitingForParticipant;
86 * Run the next participant's discovery process
88 * @param service reference to the ModbusDiscoveryService that will collect all the
91 private void startNextParticipant(final ModbusEndpointThingHandler handler, final ModbusDiscoveryService service) {
92 if (participants.isEmpty()) {
93 logger.trace("All participants has finished");
94 service.scanFinished();
95 return; // We're finished, this will exit the process
98 ModbusDiscoveryParticipant participant = participants.remove(0);
100 waitingForParticipant = true;
102 // Call startDiscovery on the next participant. The ModbusDiscoveryListener
103 // callback will be notified each time a thing is discovered, and also when
104 // the discovery is finished by this participant
105 participant.startDiscovery(handler, new ModbusDiscoveryListener() {
108 * Participant has found a thing
111 public void thingDiscovered(DiscoveryResult result) {
112 service.thingDiscovered(result);
116 * Participant finished discovery.
117 * We can continue to the next participant
120 public void discoveryFinished() {
121 waitingForParticipant = false;
122 startNextParticipant(handler, service);