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.opensprinkler.internal.discovery;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.opensprinkler.internal.api.exception.CommunicationApiException;
17 import org.openhab.binding.opensprinkler.internal.api.exception.GeneralApiException;
18 import org.openhab.binding.opensprinkler.internal.api.exception.UnauthorizedApiException;
19 import org.openhab.binding.opensprinkler.internal.config.OpenSprinklerHttpInterfaceConfig;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The {@link OpenSprinklerDiscoveryJob} class allow manual discovery of
25 * OpenSprinkler devices for a single IP address. This is used
26 * for threading to make discovery faster.
28 * @author Chris Graham - Initial contribution
31 public class OpenSprinklerDiscoveryJob implements Runnable {
32 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDiscoveryJob.class);
33 private OpenSprinklerDiscoveryService discoveryClass;
34 private String ipAddress;
36 public OpenSprinklerDiscoveryJob(OpenSprinklerDiscoveryService service, String ip) {
37 this.discoveryClass = service;
43 if (hasOpenSprinklerDevice(this.ipAddress)) {
44 discoveryClass.submitDiscoveryResults(this.ipAddress);
49 * Determines if an OpenSprinkler device is available at a given IP address.
51 * @param ip IP address of the OpenSprinkler device as a string.
52 * @return True if a device is found, false if not.
54 private boolean hasOpenSprinklerDevice(String ip) {
56 OpenSprinklerHttpInterfaceConfig config = new OpenSprinklerHttpInterfaceConfig();
58 discoveryClass.getApiFactory().getHttpApi(config);
59 } catch (UnauthorizedApiException e) {
61 } catch (CommunicationApiException | GeneralApiException exp) {
62 logger.debug("No OpenSprinkler device found at IP address ({}) because of error: {}", ip, exp.getMessage());