2 * Copyright (c) 2010-2020 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.miele.internal.discovery;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
23 import org.apache.commons.lang.StringUtils;
24 import org.openhab.binding.miele.internal.handler.ApplianceStatusListener;
25 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
26 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceClassObject;
28 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceProperty;
29 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.HomeDevice;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonElement;
41 * The {@link MieleApplianceDiscoveryService} tracks appliances that are
42 * associated with the Miele@Home gateway
44 * @author Karel Goderis - Initial contribution
45 * @author Martin Lepsy - Added protocol information in order so support WiFi devices
47 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
49 private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
51 private static final int SEARCH_TIME = 60;
53 private MieleBridgeHandler mieleBridgeHandler;
55 public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
56 super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
57 this.mieleBridgeHandler = mieleBridgeHandler;
60 public void activate() {
61 mieleBridgeHandler.registerApplianceStatusListener(this);
65 public void deactivate() {
66 removeOlderResults(new Date().getTime());
67 mieleBridgeHandler.unregisterApplianceStatusListener(this);
71 public Set<ThingTypeUID> getSupportedThingTypes() {
72 return MieleApplianceHandler.SUPPORTED_THING_TYPES;
76 public void startScan() {
77 List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
78 if (appliances != null) {
79 for (HomeDevice l : appliances) {
80 onApplianceAddedInternal(l);
86 protected synchronized void stopScan() {
88 removeOlderResults(getTimestampOfLastScan());
92 public void onApplianceAdded(HomeDevice appliance) {
93 onApplianceAddedInternal(appliance);
96 private void onApplianceAddedInternal(HomeDevice appliance) {
97 ThingUID thingUID = getThingUID(appliance);
98 if (thingUID != null) {
99 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
100 Map<String, Object> properties = new HashMap<>(2);
102 properties.put(PROTOCOL_PROPERTY_NAME, appliance.getProtocol());
103 properties.put(APPLIANCE_ID, appliance.getApplianceId());
105 for (JsonElement dc : appliance.DeviceClasses) {
106 if (dc.getAsString().contains("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele")
107 && !dc.getAsString().equals("com.miele.xgw3000.gateway.hdm.deviceclasses.MieleAppliance")) {
108 properties.put(DEVICE_CLASS, StringUtils.right(dc.getAsString(), dc.getAsString().length()
109 - new String("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele").length()));
114 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
115 .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS)).build();
117 thingDiscovered(discoveryResult);
119 logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
125 public void onApplianceRemoved(HomeDevice appliance) {
126 ThingUID thingUID = getThingUID(appliance);
128 if (thingUID != null) {
129 thingRemoved(thingUID);
134 public void onApplianceStateChanged(String uid, DeviceClassObject dco) {
139 public void onAppliancePropertyChanged(String uid, DeviceProperty dp) {
143 private ThingUID getThingUID(HomeDevice appliance) {
144 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
145 String modelID = null;
147 for (JsonElement dc : appliance.DeviceClasses) {
148 if (dc.getAsString().contains("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele")
149 && !dc.getAsString().equals("com.miele.xgw3000.gateway.hdm.deviceclasses.MieleAppliance")) {
150 modelID = StringUtils.right(dc.getAsString(), dc.getAsString().length()
151 - new String("com.miele.xgw3000.gateway.hdm.deviceclasses.Miele").length());
156 if (modelID != null) {
157 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID,
158 StringUtils.lowerCase(modelID.replaceAll("[^a-zA-Z0-9_]", "_")));
160 if (getSupportedThingTypes().contains(thingTypeUID)) {
161 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getId());