]> git.basschouten.com Git - openhab-addons.git/blob
e8561aecfd7d99847054205e8aa50cfc725cdbc4
[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.wundergroundupdatereceiver.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
28
29 /**
30  * @author Daniel Demus - Initial contribution
31  */
32 @NonNullByDefault
33 @Component(service = { DiscoveryService.class,
34         WundergroundUpdateReceiverDiscoveryService.class }, configurationPid = "discovery.wundergroundupdatereceiver")
35 public class WundergroundUpdateReceiverDiscoveryService extends AbstractDiscoveryService {
36
37     @Nullable
38     WundergroundUpdateReceiverServletControls servletControls;
39
40     private static final int TIMEOUT_SEC = 1;
41     private final HashMap<String, Map<String, String>> thinglessStationIds = new HashMap<>();
42     private boolean servletWasInactive = false;
43
44     private boolean scanning = false;
45
46     @Activate
47     public WundergroundUpdateReceiverDiscoveryService() throws IllegalArgumentException {
48         this(true);
49     }
50
51     public WundergroundUpdateReceiverDiscoveryService(boolean useBackgroundDiscovery) throws IllegalArgumentException {
52         super(WundergroundUpdateReceiverBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT_SEC,
53                 useBackgroundDiscovery);
54     }
55
56     public void removeUnhandledStationId(String stationId) {
57         thinglessStationIds.remove(stationId);
58     }
59
60     public void addUnhandledStationId(@Nullable String stationId, Map<String, String> request) {
61         if (stationId == null || stationId.isEmpty()) {
62             return;
63         }
64         if (!this.thinglessStationIds.containsKey(stationId)) {
65             this.thinglessStationIds.put(stationId, request);
66             if (isBackgroundDiscoveryEnabled()) {
67                 createDiscoveryResult(stationId);
68             }
69         }
70     }
71
72     public boolean isDiscovering() {
73         return isBackgroundDiscoveryEnabled() || isScanning();
74     }
75
76     public @Nullable Map<String, String> getUnhandledStationRequest(@Nullable String stationId) {
77         return this.thinglessStationIds.get(stationId);
78     }
79
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);
88     }
89
90     @Override
91     @Activate
92     protected void activate(@Nullable Map<String, Object> configProperties) {
93         super.activate(configProperties);
94     }
95
96     @Override
97     @Modified
98     protected void modified(@Nullable Map<String, Object> configProperties) {
99         super.modified(configProperties);
100     }
101
102     @Override
103     protected void startScan() {
104         setScanning(true);
105         if (servletControls != null && !servletControls.isActive()) {
106             servletWasInactive = true;
107             servletControls.activate();
108         }
109         thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
110     }
111
112     @Override
113     protected synchronized void stopScan() {
114         super.stopScan();
115         thinglessStationIds.keySet().forEach(this::createDiscoveryResult);
116         if (!isBackgroundDiscoveryEnabled() && servletControls != null && servletWasInactive) {
117             servletControls.deactivate();
118         }
119         setScanning(false);
120     }
121
122     protected synchronized boolean isScanning() {
123         return this.scanning;
124     }
125
126     protected synchronized void setScanning(boolean value) {
127         this.scanning = value;
128     }
129 }