]> git.basschouten.com Git - openhab-addons.git/blob
23721dfa4c65894dc6e88f2a09080b35ffa9adc4
[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.modbus.discovery.internal;
14
15 import java.util.List;
16 import java.util.concurrent.CopyOnWriteArrayList;
17
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;
27
28 /**
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
34  *
35  * @author Nagy Attila Gabor - initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class ModbusEndpointDiscoveryService implements ModbusThingHandlerDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(ModbusEndpointDiscoveryService.class);
42
43     // This is the handler we will do the discovery on
44     private @Nullable ModbusEndpointThingHandler handler;
45
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<>();
49
50     // This is set true when we're waiting for a participant to finish discovery
51     private boolean waitingForParticipant = false;
52
53     @Override
54     public void setThingHandler(@Nullable ThingHandler handler) {
55         if (handler instanceof ModbusEndpointThingHandler) {
56             this.handler = (ModbusEndpointThingHandler) handler;
57         }
58     }
59
60     @Override
61     public @Nullable ThingHandler getThingHandler() {
62         return (ThingHandler) handler;
63     }
64
65     @Override
66     public boolean startScan(ModbusDiscoveryService service) {
67         ModbusEndpointThingHandler handler = this.handler;
68         if (handler == null || !handler.isDiscoveryEnabled()) {
69             return false;
70         }
71         logger.trace("Starting discovery on endpoint {}", handler.getUID().getAsString());
72
73         participants.addAll(service.getDiscoveryParticipants());
74
75         startNextParticipant(handler, service);
76
77         return true;
78     }
79
80     @Override
81     public boolean scanInProgress() {
82         return !participants.isEmpty() || waitingForParticipant;
83     }
84
85     /**
86      * Run the next participant's discovery process
87      *
88      * @param service reference to the ModbusDiscoveryService that will collect all the
89      *            discovered items
90      */
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
96         }
97
98         ModbusDiscoveryParticipant participant = participants.remove(0);
99
100         waitingForParticipant = true;
101
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() {
106
107             /**
108              * Participant has found a thing
109              */
110             @Override
111             public void thingDiscovered(DiscoveryResult result) {
112                 service.thingDiscovered(result);
113             }
114
115             /**
116              * Participant finished discovery.
117              * We can continue to the next participant
118              */
119             @Override
120             public void discoveryFinished() {
121                 waitingForParticipant = false;
122                 startNextParticipant(handler, service);
123             }
124         });
125     }
126 }