2 * Copyright (c) 2010-2023 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.NonNull;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.touchwand.internal.TouchWandBridgeHandler;
28 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
29 import org.openhab.binding.touchwand.internal.dto.TouchWandUnitFromJson;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.gson.JsonArray;
41 import com.google.gson.JsonElement;
42 import com.google.gson.JsonParser;
43 import com.google.gson.JsonSyntaxException;
46 * The {@link TouchWandUnitDiscoveryService} Discovery service for TouchWand units.
48 * @author Roie Geron - Initial contribution
51 public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
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 @NonNullByDefault({}) TouchWandBridgeHandler touchWandBridgeHandler;
59 private final Logger logger = LoggerFactory.getLogger(TouchWandUnitDiscoveryService.class);
61 private @Nullable ScheduledFuture<?> scanningJob;
63 public TouchWandUnitDiscoveryService() {
64 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
68 protected void startScan() {
69 if (touchWandBridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) {
70 logger.debug("Could not scan units while bridge offline");
74 logger.debug("Starting TouchWand discovery on bridge {}", touchWandBridgeHandler.getThing().getUID());
75 String response = touchWandBridgeHandler.touchWandClient.cmdListUnits();
76 if (response.isEmpty()) {
81 JsonArray jsonArray = JsonParser.parseString(response).getAsJsonArray();
82 if (jsonArray.isJsonArray()) {
84 for (JsonElement unit : jsonArray) {
85 TouchWandUnitData touchWandUnit;
86 touchWandUnit = TouchWandUnitFromJson.parseResponse(unit.getAsJsonObject());
88 if (!touchWandBridgeHandler.isAddSecondaryControllerUnits()) {
89 if (!Arrays.asList(CONNECTIVITY_OPTIONS).contains(touchWandUnit.getConnectivity())) {
93 String type = touchWandUnit.getType();
94 if (!Arrays.asList(SUPPORTED_TOUCHWAND_TYPES).contains(type)) {
95 logger.debug("Unit discovery skipping unsupported unit type : {} ", type);
99 case TYPE_WALLCONTROLLER:
100 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_WALLCONTROLLER);
103 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SWITCH);
106 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_DIMMER);
109 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SHUTTER);
111 case TYPE_ALARMSENSOR:
112 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_ALARMSENSOR);
115 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_BSENSOR);
117 case TYPE_THERMOSTAT:
118 addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_THERMOSTAT);
124 } catch (JsonSyntaxException e) {
125 logger.warn("Could not parse unit {}", e.getMessage());
128 } catch (JsonSyntaxException msg) {
129 logger.warn("Could not parse list units response error:{} ", msg.getMessage());
134 protected void stopScan() {
135 removeOlderResults(getTimestampOfLastScan());
140 public void activate() {
141 super.activate(null);
142 removeOlderResults(new Date().getTime(), touchWandBridgeHandler.getThing().getUID());
146 public void deactivate() {
147 removeOlderResults(new Date().getTime(), touchWandBridgeHandler.getThing().getUID());
152 protected void startBackgroundDiscovery() {
153 ScheduledFuture<?> localScanningJob = scanningJob;
154 if (localScanningJob == null || localScanningJob.isCancelled()) {
155 scanningJob = scheduler.scheduleWithFixedDelay(this::startScan, LINK_DISCOVERY_SERVICE_INITIAL_DELAY_SEC,
156 SCAN_INTERVAL_SEC, TimeUnit.SECONDS);
161 protected void stopBackgroundDiscovery() {
162 ScheduledFuture<?> myScanningJob = scanningJob;
163 if (myScanningJob != null) {
164 myScanningJob.cancel(true);
170 public int getScanTimeout() {
171 return SEARCH_TIME_SEC;
174 private void addDeviceDiscoveryResult(TouchWandUnitData unit, ThingTypeUID typeUID) {
175 ThingUID bridgeUID = touchWandBridgeHandler.getThing().getUID();
176 ThingUID thingUID = new ThingUID(typeUID, bridgeUID, unit.getId().toString());
177 Map<String, Object> properties = new HashMap<>();
178 properties.put(HANDLER_PROPERTIES_ID, unit.getId().toString());
179 properties.put(HANDLER_PROPERTIES_NAME, unit.getName());
181 thingDiscovered(DiscoveryResultBuilder.create(thingUID)
182 .withThingType(typeUID)
183 .withLabel(unit.getName())
184 .withBridge(bridgeUID)
185 .withProperties(properties)
186 .withRepresentationProperty(HANDLER_PROPERTIES_ID)
193 public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
194 if (handler instanceof TouchWandBridgeHandler touchWandBridgeHandler) {
195 this.touchWandBridgeHandler = touchWandBridgeHandler;
200 public @NonNull ThingHandler getThingHandler() {
201 return touchWandBridgeHandler;