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.wundergroundupdatereceiver.internal;
15 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.config.discovery.AbstractDiscoveryService;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.ThingUID;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Modified;
30 * @author Daniel Demus - Initial contribution
33 @Component(service = { DiscoveryService.class,
34 WundergroundUpdateReceiverDiscoveryService.class }, configurationPid = "discovery.wundergroundupdatereceiver")
35 public class WundergroundUpdateReceiverDiscoveryService extends AbstractDiscoveryService {
38 WundergroundUpdateReceiverServletControls servletControls;
40 private static final int TIMEOUT_SEC = 1;
41 private final HashMap<String, Map<String, String>> thinglessStationIds = new HashMap<>();
42 private boolean servletWasInactive = false;
44 private boolean scanning = false;
47 public WundergroundUpdateReceiverDiscoveryService() throws IllegalArgumentException {
51 public WundergroundUpdateReceiverDiscoveryService(boolean useBackgroundDiscovery) throws IllegalArgumentException {
52 super(WundergroundUpdateReceiverBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT_SEC,
53 useBackgroundDiscovery);
56 public void removeUnhandledStationId(String stationId) {
57 thinglessStationIds.remove(stationId);
60 public void addUnhandledStationId(@Nullable String stationId, Map<String, String> request) {
61 if (stationId == null || stationId.isEmpty()) {
64 if (!this.thinglessStationIds.containsKey(stationId)) {
65 this.thinglessStationIds.put(stationId, request);
66 if (isBackgroundDiscoveryEnabled()) {
67 createDiscoveryResult(stationId);
72 public boolean isDiscovering() {
73 return isBackgroundDiscoveryEnabled() || isScanning();
76 public @Nullable Map<String, String> getUnhandledStationRequest(@Nullable String stationId) {
77 return this.thinglessStationIds.get(stationId);
80 private void createDiscoveryResult(String stationId) {
81 ThingUID id = new ThingUID(WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER, stationId);
82 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(id)
83 .withRepresentationProperty(WundergroundUpdateReceiverBindingConstants.REPRESENTATION_PROPERTY)
84 .withProperty(WundergroundUpdateReceiverBindingConstants.REPRESENTATION_PROPERTY, stationId)
85 .withThingType(WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER)
86 .withLabel("WundergroundUpdateReceiver ID " + stationId).build();
87 this.thingDiscovered(discoveryResult);
92 protected void activate(@Nullable Map<String, Object> configProperties) {
93 super.activate(configProperties);
98 protected void modified(@Nullable Map<String, Object> configProperties) {
99 super.modified(configProperties);
103 protected void startScan() {
105 if (servletControls != null && !servletControls.isActive()) {
106 servletWasInactive = true;
107 servletControls.activate();
109 thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
113 protected synchronized void stopScan() {
115 thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
116 if (!isBackgroundDiscoveryEnabled() && servletControls != null && servletWasInactive) {
117 servletControls.deactivate();
122 protected synchronized boolean isScanning() {
123 return this.scanning;
126 protected synchronized void setScanning(boolean value) {
127 this.scanning = value;