]> git.basschouten.com Git - openhab-addons.git/blob
b85ce8e11763d74e79475f52134b168b04fd847d
[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.openhab.binding.miele.internal.handler.ApplianceStatusListener;
24 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
25 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
26 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceClassObject;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceProperty;
28 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.HomeDevice;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.gson.JsonElement;
38
39 /**
40  * The {@link MieleApplianceDiscoveryService} tracks appliances that are
41  * associated with the Miele@Home gateway
42  *
43  * @author Karel Goderis - Initial contribution
44  * @author Martin Lepsy - Added protocol information in order so support WiFi devices
45  */
46 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
47
48     private static final String MIELE_APPLIANCE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.MieleAppliance";
49     private static final String MIELE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.Miele";
50
51     private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
52
53     private static final int SEARCH_TIME = 60;
54
55     private MieleBridgeHandler mieleBridgeHandler;
56
57     public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
58         super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
59         this.mieleBridgeHandler = mieleBridgeHandler;
60     }
61
62     public void activate() {
63         mieleBridgeHandler.registerApplianceStatusListener(this);
64     }
65
66     @Override
67     public void deactivate() {
68         removeOlderResults(new Date().getTime());
69         mieleBridgeHandler.unregisterApplianceStatusListener(this);
70     }
71
72     @Override
73     public Set<ThingTypeUID> getSupportedThingTypes() {
74         return MieleApplianceHandler.SUPPORTED_THING_TYPES;
75     }
76
77     @Override
78     public void startScan() {
79         List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
80         if (appliances != null) {
81             for (HomeDevice l : appliances) {
82                 onApplianceAddedInternal(l);
83             }
84         }
85     }
86
87     @Override
88     protected synchronized void stopScan() {
89         super.stopScan();
90         removeOlderResults(getTimestampOfLastScan());
91     }
92
93     @Override
94     public void onApplianceAdded(HomeDevice appliance) {
95         onApplianceAddedInternal(appliance);
96     }
97
98     private void onApplianceAddedInternal(HomeDevice appliance) {
99         ThingUID thingUID = getThingUID(appliance);
100         if (thingUID != null) {
101             ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
102             Map<String, Object> properties = new HashMap<>(2);
103
104             properties.put(PROTOCOL_PROPERTY_NAME, appliance.getProtocol());
105             properties.put(APPLIANCE_ID, appliance.getApplianceId());
106
107             for (JsonElement dc : appliance.DeviceClasses) {
108                 String dcStr = dc.getAsString();
109                 if (dcStr.contains(MIELE_CLASS) && !dcStr.equals(MIELE_APPLIANCE_CLASS)) {
110                     properties.put(DEVICE_CLASS, dcStr.substring(MIELE_CLASS.length()));
111                     break;
112                 }
113             }
114
115             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
116                     .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS)).build();
117
118             thingDiscovered(discoveryResult);
119         } else {
120             logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
121                     appliance.UID);
122         }
123     }
124
125     @Override
126     public void onApplianceRemoved(HomeDevice appliance) {
127         ThingUID thingUID = getThingUID(appliance);
128
129         if (thingUID != null) {
130             thingRemoved(thingUID);
131         }
132     }
133
134     @Override
135     public void onApplianceStateChanged(String uid, DeviceClassObject dco) {
136         // nothing to do
137     }
138
139     @Override
140     public void onAppliancePropertyChanged(String uid, DeviceProperty dp) {
141         // nothing to do
142     }
143
144     private ThingUID getThingUID(HomeDevice appliance) {
145         ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
146         String modelID = null;
147
148         for (JsonElement dc : appliance.DeviceClasses) {
149             String dcStr = dc.getAsString();
150             if (dcStr.contains(MIELE_CLASS) && !dcStr.equals(MIELE_APPLIANCE_CLASS)) {
151                 modelID = dcStr.substring(MIELE_CLASS.length());
152                 break;
153             }
154         }
155
156         if (modelID != null) {
157             ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID,
158                     modelID.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase());
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 }