2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.touchwand.internal.discovery;
15 import static org.openhab.binding.touchwand.internal.TouchWandBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.Date;
19 import java.util.HashMap;
21 import java.util.concurrent.ScheduledFuture;
22 import java.util.concurrent.TimeUnit;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.touchwand.internal.TouchWandBridgeHandler;
27 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
28 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitFromJson;
29 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.ServiceScope;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.JsonArray;
40 import com.google.gson.JsonElement;
41 import com.google.gson.JsonParser;
42 import com.google.gson.JsonSyntaxException;
45 * The {@link TouchWandUnitDiscoveryService} Discovery service for TouchWand units.
47 * @author Roie Geron - Initial contribution
49 @Component(scope = ServiceScope.PROTOTYPE, service = TouchWandUnitDiscoveryService.class)
51 public class TouchWandUnitDiscoveryService extends AbstractThingHandlerDiscoveryService<TouchWandBridgeHandler> {
53 private static final int SEARCH_TIME_SEC = 10;
54 private static final int SCAN_INTERVAL_SEC = 60;
55 private static final int LINK_DISCOVERY_SERVICE_INITIAL_DELAY_SEC = 5;
56 private static final String[] CONNECTIVITY_OPTIONS = { CONNECTIVITY_KNX, CONNECTIVITY_ZWAVE, CONNECTIVITY_RISCO,
57 CONNECTIVITY_PIMA, CONNECTIVITY_ACWAND };
58 private final Logger logger = LoggerFactory.getLogger(TouchWandUnitDiscoveryService.class);
60 private @Nullable ScheduledFuture<?> scanningJob;
62 public TouchWandUnitDiscoveryService() {
63 super(TouchWandBridgeHandler.class, SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
67 protected void startScan() {
68 if (thingHandler.getThing().getStatus() != ThingStatus.ONLINE) {
69 logger.debug("Could not scan units while bridge offline");
73 logger.debug("Starting TouchWand discovery on bridge {}", thingHandler.getThing().getUID());
74 String response = thingHandler.touchWandClient.cmdListUnits();
75 if (response.isEmpty()) {
80 JsonArray jsonArray = JsonParser.parseString(response).getAsJsonArray();
81 if (jsonArray.isJsonArray()) {
83 for (JsonElement unit : jsonArray) {
84 TouchWandUnitData touchWandUnit;
85 touchWandUnit = TouchWandUnitFromJson.parseResponse(unit.getAsJsonObject());
87 if (!thingHandler.isAddSecondaryControllerUnits()) {
88 if (!Arrays.asList(CONNECTIVITY_OPTIONS).contains(touchWandUnit.getConnectivity())) {
92 String type = touchWandUnit.getType();
93 if (!Arrays.asList(SUPPORTED_TOUCHWAND_TYPES).contains(type)) {
94 logger.debug("Unit discovery skipping unsupported unit type : {} ", type);
98 case TYPE_WALLCONTROLLER:
99 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_WALLCONTROLLER);
102 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SWITCH);
105 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_DIMMER);
108 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SHUTTER);
110 case TYPE_ALARMSENSOR:
111 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_ALARMSENSOR);
114 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_BSENSOR);
116 case TYPE_THERMOSTAT:
117 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_THERMOSTAT);
123 } catch (JsonSyntaxException e) {
124 logger.warn("Could not parse unit {}", e.getMessage());
127 } catch (JsonSyntaxException msg) {
128 logger.warn("Could not parse list units response error:{} ", msg.getMessage());
133 protected void stopScan() {
134 removeOlderResults(getTimestampOfLastScan());
139 public void initialize() {
140 removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
145 public void dispose() {
147 removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
151 protected void startBackgroundDiscovery() {
152 ScheduledFuture<?> localScanningJob = scanningJob;
153 if (localScanningJob == null || localScanningJob.isCancelled()) {
154 scanningJob = scheduler.scheduleWithFixedDelay(this::startScan, LINK_DISCOVERY_SERVICE_INITIAL_DELAY_SEC,
155 SCAN_INTERVAL_SEC, TimeUnit.SECONDS);
160 protected void stopBackgroundDiscovery() {
161 ScheduledFuture<?> myScanningJob = scanningJob;
162 if (myScanningJob != null) {
163 myScanningJob.cancel(true);
169 public int getScanTimeout() {
170 return SEARCH_TIME_SEC;
173 private void addDeviceDiscoveryResult(TouchWandUnitData unit, ThingTypeUID typeUID) {
174 ThingUID bridgeUID = thingHandler.getThing().getUID();
175 ThingUID thingUID = new ThingUID(typeUID, bridgeUID, unit.getId().toString());
176 Map<String, Object> properties = new HashMap<>();
177 properties.put(HANDLER_PROPERTIES_ID, unit.getId().toString());
178 properties.put(HANDLER_PROPERTIES_NAME, unit.getName());
180 thingDiscovered(DiscoveryResultBuilder.create(thingUID)
181 .withThingType(typeUID)
182 .withLabel(unit.getName())
183 .withBridge(bridgeUID)
184 .withProperties(properties)
185 .withRepresentationProperty(HANDLER_PROPERTIES_ID)