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