]> git.basschouten.com Git - openhab-addons.git/blob
f1a22d63de2646df812f70847c1fbdc75de7dc25
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
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  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
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<>(9);
101
102             FullyQualifiedApplianceIdentifier applianceIdentifier = appliance.getApplianceIdentifier();
103             properties.put(Thing.PROPERTY_VENDOR, appliance.Vendor);
104             properties.put(Thing.PROPERTY_MODEL_ID, appliance.getApplianceModel());
105             properties.put(Thing.PROPERTY_SERIAL_NUMBER, appliance.getSerialNumber());
106             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, appliance.getFirmwareVersion());
107             properties.put(PROPERTY_PROTOCOL_ADAPTER, appliance.ProtocolAdapterName);
108             properties.put(APPLIANCE_ID, applianceIdentifier.getApplianceId());
109             String deviceClass = appliance.getDeviceClass();
110             if (deviceClass != null) {
111                 properties.put(PROPERTY_DEVICE_CLASS, deviceClass);
112             }
113             String connectionType = appliance.getConnectionType();
114             if (connectionType != null) {
115                 properties.put(PROPERTY_CONNECTION_TYPE, connectionType);
116             }
117             String connectionBaudRate = appliance.getConnectionBaudRate();
118             if (connectionBaudRate != null) {
119                 properties.put(PROPERTY_CONNECTION_BAUD_RATE, connectionBaudRate);
120             }
121
122             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
123                     .withBridge(bridgeUID).withLabel(deviceClass != null ? deviceClass : appliance.getApplianceModel())
124                     .withRepresentationProperty(APPLIANCE_ID).build();
125
126             thingDiscovered(discoveryResult);
127         } else {
128             logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
129                     appliance.UID);
130         }
131     }
132
133     @Override
134     public void onApplianceRemoved(HomeDevice appliance) {
135         ThingUID thingUID = getThingUID(appliance);
136
137         if (thingUID != null) {
138             thingRemoved(thingUID);
139         }
140     }
141
142     @Override
143     public void onApplianceStateChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceClassObject dco) {
144         // nothing to do
145     }
146
147     @Override
148     public void onAppliancePropertyChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceProperty dp) {
149         // nothing to do
150     }
151
152     private ThingUID getThingUID(HomeDevice appliance) {
153         ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
154         String modelId = appliance.getDeviceClass();
155
156         if (modelId != null) {
157             ThingTypeUID thingTypeUID = getThingTypeUidFromModelId(modelId);
158
159             if (getSupportedThingTypes().contains(thingTypeUID)) {
160                 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getApplianceIdentifier().getId());
161                 return thingUID;
162             } else {
163                 return null;
164             }
165         } else {
166             return null;
167         }
168     }
169
170     private ThingTypeUID getThingTypeUidFromModelId(String modelId) {
171         /*
172          * Coffee machine CVA 6805 is reported as CoffeeSystem, but thing type is
173          * coffeemachine. At least until it is known if any models are actually reported
174          * as CoffeeMachine, we need this special mapping.
175          */
176         if (MIELE_DEVICE_CLASS_COFFEE_SYSTEM.equals(modelId)) {
177             return THING_TYPE_COFFEEMACHINE;
178         }
179
180         String thingTypeId = modelId.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase();
181
182         return new ThingTypeUID(BINDING_ID, thingTypeId);
183     }
184 }