[daikin] Add the ability to disable background discovery (#12300)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
parent
2e72ac78fd
commit
6000bc75fb
@ -13,6 +13,14 @@ This may work with the older KRP series of wired adapters, but has not been test
|
||||
This add-on will broadcast messages on your local network looking for Daikin air conditioning units and adding them to the queue of new items discovered.
|
||||
You can also manually add a new item if you know the IP address.
|
||||
|
||||
Background discovery polls the network every minute for devices.
|
||||
Background discovery is **enabled** by default.
|
||||
To **disable** background discovery, add the following line to the *conf/services/runtime.cfg* file:
|
||||
|
||||
```text
|
||||
discovery.daikin:background=false
|
||||
```
|
||||
|
||||
### BRP072C42 adapter discovery
|
||||
|
||||
A BRP072C42 adapter requires a registered UUID to authenticate. Upon discovery, a UUID will be generated but the adapter's key must be entered in the Thing configuration to complete the UUID registration.
|
||||
|
||||
@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory;
|
||||
* @author Paul Smedley <paul@smedley.id.au> - Modifications to support Airbase Controllers
|
||||
*
|
||||
*/
|
||||
@Component(service = DiscoveryService.class)
|
||||
@Component(service = DiscoveryService.class, configurationPid = "discovery.daikin")
|
||||
@NonNullByDefault
|
||||
public class DaikinACUnitDiscoveryService extends AbstractDiscoveryService {
|
||||
private static final String UDP_PACKET_CONTENTS = "DAIKIN_UDP/common/basic_info";
|
||||
@ -62,17 +62,15 @@ public class DaikinACUnitDiscoveryService extends AbstractDiscoveryService {
|
||||
private Logger logger = LoggerFactory.getLogger(DaikinACUnitDiscoveryService.class);
|
||||
|
||||
private @Nullable HttpClient httpClient;
|
||||
private final Runnable scanner;
|
||||
private @Nullable ScheduledFuture<?> backgroundFuture;
|
||||
|
||||
public DaikinACUnitDiscoveryService() {
|
||||
super(Collections.singleton(DaikinBindingConstants.THING_TYPE_AC_UNIT), 600, true);
|
||||
scanner = createScanner();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startScan() {
|
||||
scheduler.execute(scanner);
|
||||
scheduler.execute(this::scanner);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -83,7 +81,7 @@ public class DaikinACUnitDiscoveryService extends AbstractDiscoveryService {
|
||||
backgroundFuture.cancel(true);
|
||||
backgroundFuture = null;
|
||||
}
|
||||
backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
|
||||
backgroundFuture = scheduler.scheduleWithFixedDelay(this::scanner, 0, 60, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -96,32 +94,30 @@ public class DaikinACUnitDiscoveryService extends AbstractDiscoveryService {
|
||||
super.stopBackgroundDiscovery();
|
||||
}
|
||||
|
||||
private Runnable createScanner() {
|
||||
return () -> {
|
||||
long timestampOfLastScan = getTimestampOfLastScan();
|
||||
for (InetAddress broadcastAddress : getBroadcastAddresses()) {
|
||||
logger.trace("Starting broadcast for {}", broadcastAddress.toString());
|
||||
private void scanner() {
|
||||
long timestampOfLastScan = getTimestampOfLastScan();
|
||||
for (InetAddress broadcastAddress : getBroadcastAddresses()) {
|
||||
logger.trace("Starting broadcast for {}", broadcastAddress.toString());
|
||||
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
socket.setBroadcast(true);
|
||||
socket.setReuseAddress(true);
|
||||
byte[] packetContents = UDP_PACKET_CONTENTS.getBytes(StandardCharsets.UTF_8);
|
||||
DatagramPacket packet = new DatagramPacket(packetContents, packetContents.length, broadcastAddress,
|
||||
REMOTE_UDP_PORT);
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
socket.setBroadcast(true);
|
||||
socket.setReuseAddress(true);
|
||||
byte[] packetContents = UDP_PACKET_CONTENTS.getBytes(StandardCharsets.UTF_8);
|
||||
DatagramPacket packet = new DatagramPacket(packetContents, packetContents.length, broadcastAddress,
|
||||
REMOTE_UDP_PORT);
|
||||
|
||||
// Send before listening in case the port isn't bound until here.
|
||||
socket.send(packet);
|
||||
// Send before listening in case the port isn't bound until here.
|
||||
socket.send(packet);
|
||||
|
||||
// receivePacketAndDiscover will return false if no packet is received after 1 second
|
||||
while (receivePacketAndDiscover(socket)) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Nothing to do here - the host couldn't be found, likely because it doesn't exist
|
||||
// receivePacketAndDiscover will return false if no packet is received after 1 second
|
||||
while (receivePacketAndDiscover(socket)) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Nothing to do here - the host couldn't be found, likely because it doesn't exist
|
||||
}
|
||||
}
|
||||
|
||||
removeOlderResults(timestampOfLastScan);
|
||||
};
|
||||
removeOlderResults(timestampOfLastScan);
|
||||
}
|
||||
|
||||
private boolean receivePacketAndDiscover(DatagramSocket socket) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user