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