]> git.basschouten.com Git - openhab-addons.git/blob
a82acc26b2ed2dd0bdfd2b2437491b630c868356
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miele.internal.discovery;
14
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22
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;
37
38 import com.google.gson.JsonElement;
39
40 /**
41  * The {@link MieleApplianceDiscoveryService} tracks appliances that are
42  * associated with the Miele@Home gateway
43  *
44  * @author Karel Goderis - Initial contribution
45  * @author Martin Lepsy - Added protocol information in order so support WiFi devices
46  */
47 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
48
49     private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
50
51     private static final int SEARCH_TIME = 60;
52
53     private MieleBridgeHandler mieleBridgeHandler;
54
55     public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
56         super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
57         this.mieleBridgeHandler = mieleBridgeHandler;
58     }
59
60     public void activate() {
61         mieleBridgeHandler.registerApplianceStatusListener(this);
62     }
63
64     @Override
65     public void deactivate() {
66         removeOlderResults(new Date().getTime());
67         mieleBridgeHandler.unregisterApplianceStatusListener(this);
68     }
69
70     @Override
71     public Set<ThingTypeUID> getSupportedThingTypes() {
72         return MieleApplianceHandler.SUPPORTED_THING_TYPES;
73     }
74
75     @Override
76     public void startScan() {
77         List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
78         if (appliances != null) {
79             for (HomeDevice l : appliances) {
80                 onApplianceAddedInternal(l);
81             }
82         }
83     }
84
85     @Override
86     protected synchronized void stopScan() {
87         super.stopScan();
88         removeOlderResults(getTimestampOfLastScan());
89     }
90
91     @Override
92     public void onApplianceAdded(HomeDevice appliance) {
93         onApplianceAddedInternal(appliance);
94     }
95
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);
101
102             properties.put(PROTOCOL_PROPERTY_NAME, appliance.getProtocol());
103             properties.put(APPLIANCE_ID, appliance.getApplianceId());
104
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()));
110                     break;
111                 }
112             }
113
114             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
115                     .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS)).build();
116
117             thingDiscovered(discoveryResult);
118         } else {
119             logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
120                     appliance.UID);
121         }
122     }
123
124     @Override
125     public void onApplianceRemoved(HomeDevice appliance) {
126         ThingUID thingUID = getThingUID(appliance);
127
128         if (thingUID != null) {
129             thingRemoved(thingUID);
130         }
131     }
132
133     @Override
134     public void onApplianceStateChanged(String uid, DeviceClassObject dco) {
135         // nothing to do
136     }
137
138     @Override
139     public void onAppliancePropertyChanged(String uid, DeviceProperty dp) {
140         // nothing to do
141     }
142
143     private ThingUID getThingUID(HomeDevice appliance) {
144         ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
145         String modelID = null;
146
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());
152                 break;
153             }
154         }
155
156         if (modelID != null) {
157             ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID,
158                     StringUtils.lowerCase(modelID.replaceAll("[^a-zA-Z0-9_]", "_")));
159
160             if (getSupportedThingTypes().contains(thingTypeUID)) {
161                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getId());
162                 return thingUID;
163             } else {
164                 return null;
165             }
166         } else {
167             return null;
168         }
169     }
170 }