]> git.basschouten.com Git - openhab-addons.git/blob
f3ce4a1c10a240e8067a34baebe36f034ae109c6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.livisismarthome.internal.discovery;
14
15 import static org.openhab.binding.livisismarthome.internal.LivisiBindingConstants.PROPERTY_ID;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.Set;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.livisismarthome.internal.LivisiBindingConstants;
25 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.DeviceDTO;
26 import org.openhab.binding.livisismarthome.internal.handler.LivisiBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.ServiceScope;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link LivisiDeviceDiscoveryService} is responsible for discovering new devices.
39  *
40  * @author Oliver Kuhl - Initial contribution
41  * @author Sven Strohschein - Renamed from Innogy to Livisi
42  */
43 @Component(scope = ServiceScope.PROTOTYPE, service = LivisiDeviceDiscoveryService.class)
44 @NonNullByDefault
45 public class LivisiDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<LivisiBridgeHandler> {
46
47     private static final int SEARCH_TIME_SECONDS = 60;
48
49     private final Logger logger = LoggerFactory.getLogger(LivisiDeviceDiscoveryService.class);
50
51     /**
52      * Construct a {@link LivisiDeviceDiscoveryService}.
53      */
54     public LivisiDeviceDiscoveryService() {
55         super(LivisiBridgeHandler.class, SEARCH_TIME_SECONDS);
56     }
57
58     /**
59      * Deactivates the {@link LivisiDeviceDiscoveryService} by unregistering it as
60      * {@link org.openhab.binding.livisismarthome.internal.listener.DeviceStatusListener} on the
61      * {@link LivisiBridgeHandler}. Older discovery results will be removed.
62      *
63      * @see org.openhab.core.config.discovery.AbstractDiscoveryService#deactivate()
64      */
65     @Override
66     public void dispose() {
67         super.dispose();
68         removeOlderResults(new Date().getTime());
69     }
70
71     @Override
72     public Set<ThingTypeUID> getSupportedThingTypes() {
73         return LivisiBindingConstants.SUPPORTED_DEVICE_THING_TYPES;
74     }
75
76     @Override
77     protected void startScan() {
78         logger.debug("SCAN for new LIVISI SmartHome devices started...");
79         for (final DeviceDTO d : thingHandler.loadDevices()) {
80             onDeviceAdded(d);
81         }
82     }
83
84     @Override
85     protected synchronized void stopScan() {
86         super.stopScan();
87         removeOlderResults(getTimestampOfLastScan());
88     }
89
90     public void onDeviceAdded(DeviceDTO device) {
91         final ThingUID bridgeUID = thingHandler.getThing().getUID();
92         final Optional<ThingUID> thingUID = getThingUID(bridgeUID, device);
93         final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
94
95         if (thingUID.isPresent() && thingTypeUID.isPresent()) {
96             String name = device.getConfig().getName();
97             if (name.isEmpty()) {
98                 name = device.getSerialNumber();
99             }
100
101             final Map<String, Object> properties = new HashMap<>();
102             properties.put(PROPERTY_ID, device.getId());
103
104             final String label;
105             if (device.hasLocation()) {
106                 label = device.getType() + ": " + name + " (" + device.getLocation().getName() + ")";
107             } else {
108                 label = device.getType() + ": " + name;
109             }
110
111             final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID.get())
112                     .withThingType(thingTypeUID.get()).withProperties(properties).withBridge(bridgeUID)
113                     .withRepresentationProperty(PROPERTY_ID).withLabel(label).build();
114
115             thingDiscovered(discoveryResult);
116         } else {
117             logger.debug("Discovered unsupported device of type '{}' and name '{}' with id {}", device.getType(),
118                     device.getConfig().getName(), device.getId());
119         }
120     }
121
122     /**
123      * Returns the {@link ThingUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
124      *
125      * @param bridgeUID bridge
126      * @param device device
127      * @return {@link ThingUID} for the given {@link DeviceDTO} or empty
128      */
129     private Optional<ThingUID> getThingUID(ThingUID bridgeUID, DeviceDTO device) {
130         final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
131
132         if (thingTypeUID.isPresent() && getSupportedThingTypes().contains(thingTypeUID.get())) {
133             return Optional.of(new ThingUID(thingTypeUID.get(), bridgeUID, device.getId()));
134         }
135         return Optional.empty();
136     }
137
138     /**
139      * Returns a {@link ThingTypeUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
140      *
141      * @param device device
142      * @return {@link ThingTypeUID} for the given {@link DeviceDTO} or empty
143      */
144     private Optional<ThingTypeUID> getThingTypeUID(DeviceDTO device) {
145         final String thingTypeId = device.getType();
146         if (thingTypeId != null) {
147             return Optional.of(new ThingTypeUID(LivisiBindingConstants.BINDING_ID, thingTypeId));
148         }
149         return Optional.empty();
150     }
151 }