]> git.basschouten.com Git - openhab-addons.git/blob
5c0de6e09951a63a6e593ac3936b4d493f67241a
[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.FullyQualifiedApplianceIdentifier;
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 /**
39  * The {@link MieleApplianceDiscoveryService} tracks appliances that are
40  * associated with the Miele@Home gateway
41  *
42  * @author Karel Goderis - Initial contribution
43  * @author Martin Lepsy - Added protocol information in order so support WiFi devices
44  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
45  */
46 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
47
48     private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
49
50     private static final int SEARCH_TIME = 60;
51
52     private MieleBridgeHandler mieleBridgeHandler;
53
54     public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
55         super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
56         this.mieleBridgeHandler = mieleBridgeHandler;
57     }
58
59     public void activate() {
60         mieleBridgeHandler.registerApplianceStatusListener(this);
61     }
62
63     @Override
64     public void deactivate() {
65         removeOlderResults(new Date().getTime());
66         mieleBridgeHandler.unregisterApplianceStatusListener(this);
67     }
68
69     @Override
70     public Set<ThingTypeUID> getSupportedThingTypes() {
71         return MieleApplianceHandler.SUPPORTED_THING_TYPES;
72     }
73
74     @Override
75     public void startScan() {
76         List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
77         if (appliances != null) {
78             for (HomeDevice l : appliances) {
79                 onApplianceAddedInternal(l);
80             }
81         }
82     }
83
84     @Override
85     protected synchronized void stopScan() {
86         super.stopScan();
87         removeOlderResults(getTimestampOfLastScan());
88     }
89
90     @Override
91     public void onApplianceAdded(HomeDevice appliance) {
92         onApplianceAddedInternal(appliance);
93     }
94
95     private void onApplianceAddedInternal(HomeDevice appliance) {
96         ThingUID thingUID = getThingUID(appliance);
97         if (thingUID != null) {
98             ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
99             Map<String, Object> properties = new HashMap<>(2);
100
101             FullyQualifiedApplianceIdentifier applianceIdentifier = appliance.getApplianceIdentifier();
102             properties.put(MODEL_PROPERTY_NAME, appliance.getApplianceModel());
103             String deviceClass = appliance.getDeviceClass();
104             if (deviceClass != null) {
105                 properties.put(DEVICE_CLASS, deviceClass);
106             }
107             properties.put(PROTOCOL_ADAPTER_PROPERTY_NAME, appliance.ProtocolAdapterName);
108             properties.put(APPLIANCE_ID, applianceIdentifier.getApplianceId());
109             properties.put(SERIAL_NUMBER_PROPERTY_NAME, appliance.getSerialNumber());
110             String connectionType = appliance.getConnectionType();
111             if (connectionType != null) {
112                 properties.put(CONNECTION_TYPE_PROPERTY_NAME, connectionType);
113             }
114
115             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
116                     .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS))
117                     .withRepresentationProperty(APPLIANCE_ID).build();
118
119             thingDiscovered(discoveryResult);
120         } else {
121             logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
122                     appliance.UID);
123         }
124     }
125
126     @Override
127     public void onApplianceRemoved(HomeDevice appliance) {
128         ThingUID thingUID = getThingUID(appliance);
129
130         if (thingUID != null) {
131             thingRemoved(thingUID);
132         }
133     }
134
135     @Override
136     public void onApplianceStateChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceClassObject dco) {
137         // nothing to do
138     }
139
140     @Override
141     public void onAppliancePropertyChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceProperty dp) {
142         // nothing to do
143     }
144
145     private ThingUID getThingUID(HomeDevice appliance) {
146         ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
147         String modelId = appliance.getDeviceClass();
148
149         if (modelId != null) {
150             ThingTypeUID thingTypeUID = getThingTypeUidFromModelId(modelId);
151
152             if (getSupportedThingTypes().contains(thingTypeUID)) {
153                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getApplianceIdentifier().getId());
154                 return thingUID;
155             } else {
156                 return null;
157             }
158         } else {
159             return null;
160         }
161     }
162
163     private ThingTypeUID getThingTypeUidFromModelId(String modelId) {
164         /*
165          * Coffee machine CVA 6805 is reported as CoffeeSystem, but thing type is
166          * coffeemachine. At least until it is known if any models are actually reported
167          * as CoffeeMachine, we need this special mapping.
168          */
169         if (MIELE_DEVICE_CLASS_COFFEE_SYSTEM.equals(modelId)) {
170             return THING_TYPE_COFFEEMACHINE;
171         }
172
173         String thingTypeId = modelId.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase();
174
175         return new ThingTypeUID(BINDING_ID, thingTypeId);
176     }
177 }