]> git.basschouten.com Git - openhab-addons.git/blob
050b5cdeb81ce0be09b125d0e2dc0c6eb1e6fa80
[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.sensibo.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.sensibo.internal.SensiboBindingConstants;
24 import org.openhab.binding.sensibo.internal.handler.SensiboAccountHandler;
25 import org.openhab.binding.sensibo.internal.model.SensiboModel;
26 import org.openhab.binding.sensibo.internal.model.SensiboSky;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Arne Seime - Initial contribution
38  */
39 @NonNullByDefault
40 public class SensiboDiscoveryService extends AbstractDiscoveryService {
41     public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set
42             .of(SensiboBindingConstants.THING_TYPE_SENSIBOSKY);
43     private static final long REFRESH_INTERVAL_MINUTES = 60;
44     private final Logger logger = LoggerFactory.getLogger(SensiboDiscoveryService.class);
45     private final SensiboAccountHandler accountHandler;
46     private Optional<ScheduledFuture<?>> discoveryJob = Optional.empty();
47
48     public SensiboDiscoveryService(final SensiboAccountHandler accountHandler) {
49         super(DISCOVERABLE_THING_TYPES_UIDS, 10);
50         this.accountHandler = accountHandler;
51     }
52
53     @Override
54     protected void startBackgroundDiscovery() {
55         discoveryJob = Optional
56                 .of(scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH_INTERVAL_MINUTES, TimeUnit.MINUTES));
57     }
58
59     @Override
60     protected void startScan() {
61         logger.debug("Start scan for Sensibo devices.");
62         synchronized (this) {
63             removeOlderResults(getTimestampOfLastScan(), null, accountHandler.getThing().getUID());
64             final ThingUID accountUID = accountHandler.getThing().getUID();
65             accountHandler.updateModelFromServerAndUpdateThingStatus();
66             final SensiboModel model = accountHandler.getModel();
67             for (final SensiboSky pod : model.getPods()) {
68                 final ThingUID podUID = new ThingUID(SensiboBindingConstants.THING_TYPE_SENSIBOSKY, accountUID,
69                         pod.getMacAddress());
70                 Map<String, String> properties = pod.getThingProperties();
71
72                 // DiscoveryResult result uses Map<String,Object> as properties while ThingBuilder uses
73                 // Map<String,String>
74                 Map<String, Object> stringObjectProperties = new HashMap<>();
75                 stringObjectProperties.putAll(properties);
76
77                 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(podUID).withBridge(accountUID)
78                         .withLabel(pod.getProductName()).withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS)
79                         .withProperties(stringObjectProperties).build();
80                 thingDiscovered(discoveryResult);
81             }
82         }
83     }
84
85     @Override
86     protected void stopBackgroundDiscovery() {
87         stopScan();
88         discoveryJob.ifPresent(job -> {
89             if (!job.isCancelled()) {
90                 job.cancel(true);
91             }
92             discoveryJob = Optional.empty();
93         });
94     }
95
96     @Override
97     protected void stopScan() {
98         logger.debug("Stop scan for Sensibo devices.");
99         super.stopScan();
100     }
101 }