]> git.basschouten.com Git - openhab-addons.git/blob
7409752eb8eca994066545e02745fe33fb244e68
[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.ojelectronics.internal.services;
14
15 import static org.openhab.binding.ojelectronics.internal.BindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.ojelectronics.internal.OJCloudHandler;
25 import org.openhab.binding.ojelectronics.internal.models.groups.GroupContent;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.osgi.service.component.annotations.Component;
34
35 /**
36  * DiscoveryService for OJ Components
37  *
38  * @author Christian Kittel - Initial Contribution
39  */
40 @NonNullByDefault
41 @Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.ojelectronics")
42 public final class OJDiscoveryService extends AbstractDiscoveryService
43         implements DiscoveryService, ThingHandlerService {
44
45     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_OJCLOUD);
46     private @Nullable OJCloudHandler bridgeHandler;
47     private @Nullable Collection<GroupContent> groupContents;
48
49     /**
50      * Creates a new instance of {@link OJDiscoveryService}
51      *
52      */
53     public OJDiscoveryService() throws IllegalArgumentException {
54         super(SUPPORTED_THING_TYPES_UIDS, 10);
55     }
56
57     /**
58      * Sets the scan result for discovering
59      *
60      * @param groupContents Content from API
61      */
62     public void setScanResultForDiscovery(List<GroupContent> groupContents) {
63         this.groupContents = groupContents;
64     }
65
66     @Override
67     protected void startScan() {
68         final OJCloudHandler bridgeHandler = this.bridgeHandler;
69         final Collection<GroupContent> groupContents = this.groupContents;
70         if (groupContents != null && bridgeHandler != null) {
71             groupContents.stream().flatMap(content -> content.thermostats.stream())
72                     .forEach(thermostat -> thingDiscovered(bridgeHandler.getThing().getUID(), thermostat.serialNumber));
73         }
74     }
75
76     @Override
77     public void setThingHandler(@Nullable ThingHandler handler) {
78         if (handler instanceof OJCloudHandler) {
79             final OJCloudHandler bridgeHandler = (OJCloudHandler) handler;
80             this.bridgeHandler = bridgeHandler;
81             bridgeHandler.setDiscoveryService(this);
82         }
83     }
84
85     @Override
86     public @Nullable ThingHandler getThingHandler() {
87         return bridgeHandler;
88     }
89
90     @Override
91     public void deactivate() {
92         super.deactivate();
93     }
94
95     private void thingDiscovered(ThingUID bridgeUID, String serialNumber) {
96         thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_OWD5, bridgeUID, serialNumber))
97                 .withBridge(bridgeUID).withRepresentationProperty("serialNumber")
98                 .withProperty("serialNumber", serialNumber).withLabel("Thermostat " + serialNumber).build());
99     }
100 }