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