]> git.basschouten.com Git - openhab-addons.git/blob
f24fc6acb58b24c1ff4b1cd47fc9d08f0c8f4f43
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.groheondus.internal.discovery;
14
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSE;
16 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSEGUARD;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.openhab.binding.groheondus.internal.handler.GroheOndusAccountHandler;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import io.github.floriansw.ondus.api.OndusService;
37 import io.github.floriansw.ondus.api.model.BaseAppliance;
38
39 /**
40  * @author Florian Schmidt - Initial contribution
41  */
42 @NonNullByDefault
43 public class GroheOndusDiscoveryService extends AbstractDiscoveryService {
44     private static final String PROPERTY_APPLIANCE_ID = "applianceId";
45
46     private static final String PROPERTY_ROOM_ID = "roomId";
47
48     private static final String PROPERTY_LOCATION_ID = "locationId";
49
50     private final Logger logger = LoggerFactory.getLogger(GroheOndusDiscoveryService.class);
51
52     private final GroheOndusAccountHandler bridgeHandler;
53
54     public GroheOndusDiscoveryService(GroheOndusAccountHandler bridgeHandler) {
55         super(Collections
56                 .unmodifiableSet(Stream.of(THING_TYPE_SENSE, THING_TYPE_SENSEGUARD).collect(Collectors.toSet())), 30);
57         logger.debug("initialize discovery service");
58         this.bridgeHandler = bridgeHandler;
59         this.activate(null);
60     }
61
62     @Override
63     protected void startScan() {
64         // Remove old results - or they will stay there forever
65         removeOlderResults(getTimestampOfLastScan(), null, bridgeHandler.getThing().getUID());
66
67         OndusService service;
68         try {
69             service = bridgeHandler.getService();
70         } catch (IllegalStateException e) {
71             logger.debug("No instance of OndusService given.", e);
72             return;
73         }
74         List<BaseAppliance> discoveredAppliances = new ArrayList<>();
75         try {
76             discoveredAppliances = service.appliances();
77         } catch (IOException e) {
78             logger.debug("Could not discover appliances.", e);
79         }
80
81         discoveredAppliances.forEach(appliance -> {
82             ThingUID bridgeUID = bridgeHandler.getThing().getUID();
83             ThingUID thingUID = null;
84             switch (appliance.getType()) {
85                 case io.github.floriansw.ondus.api.model.guard.Appliance.TYPE:
86                     thingUID = new ThingUID(THING_TYPE_SENSEGUARD, bridgeUID, appliance.getApplianceId());
87                     break;
88                 case io.github.floriansw.ondus.api.model.sense.Appliance.TYPE:
89                     thingUID = new ThingUID(THING_TYPE_SENSE, bridgeUID, appliance.getApplianceId());
90                     break;
91                 default:
92                     return;
93             }
94
95             Map<String, Object> properties = new HashMap<>();
96             properties.put(PROPERTY_LOCATION_ID, appliance.getRoom().getLocation().getId());
97             properties.put(PROPERTY_ROOM_ID, appliance.getRoom().getId());
98             properties.put(PROPERTY_APPLIANCE_ID, appliance.getApplianceId());
99
100             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
101                     .withBridge(bridgeUID).withLabel(appliance.getName())
102                     .withRepresentationProperty(PROPERTY_APPLIANCE_ID).build();
103
104             thingDiscovered(discoveryResult);
105         });
106     }
107 }