]> git.basschouten.com Git - openhab-addons.git/blob
7ea32c6f88ec6eeef1d671188adb60d38063f231
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mielecloud.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
21 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
24
25 /**
26  * Helper class extracting information related to things from {@link DeviceState}s received from the Miele cloud.
27  *
28  * @author Björn Lange - Initial contribution
29  */
30 @NonNullByDefault
31 public final class ThingInformationExtractor {
32     private ThingInformationExtractor() {
33         throw new IllegalStateException(getClass().getName() + " cannot be instantiated");
34     }
35
36     /**
37      * Extracts thing properties from a {@link DeviceState}.
38      *
39      * The returned properties always contain {@link Thing#PROPERTY_SERIAL_NUMBER} and {@link Thing#PROPERTY_MODEL_ID}.
40      * More might be present depending on the type of device.
41      *
42      * @param thingTypeUid {@link ThingTypeUID} of the thing to extract properties for.
43      * @param deviceState {@link DeviceState} received from the Miele cloud.
44      * @return A {@link Map} holding the properties as key-value pairs.
45      */
46     public static Map<String, String> extractProperties(ThingTypeUID thingTypeUid, DeviceState deviceState) {
47         var propertyMap = new HashMap<String, String>();
48         propertyMap.put(Thing.PROPERTY_SERIAL_NUMBER, getSerialNumber(deviceState));
49         propertyMap.put(Thing.PROPERTY_MODEL_ID, getModelId(deviceState));
50         propertyMap.put(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, deviceState.getDeviceIdentifier());
51
52         if (MieleCloudBindingConstants.THING_TYPE_HOB.equals(thingTypeUid)) {
53             deviceState.getPlateStepCount().ifPresent(plateCount -> propertyMap
54                     .put(MieleCloudBindingConstants.PROPERTY_PLATE_COUNT, plateCount.toString()));
55         }
56
57         return propertyMap;
58     }
59
60     private static String getSerialNumber(DeviceState deviceState) {
61         return deviceState.getFabNumber().orElse(deviceState.getDeviceIdentifier());
62     }
63
64     private static String getModelId(DeviceState deviceState) {
65         return getDeviceAndTechType(deviceState).orElse("Unknown");
66     }
67
68     /**
69      * Formats device type and tech type from the given {@link DeviceState} for the purpose of displaying then to the
70      * user.
71      *
72      * If either of device or tech type is missing then it will be omitted. If both are missing then an empty
73      * {@link Optional} will be returned.
74      *
75      * @param deviceState {@link DeviceState} obtained from the Miele cloud.
76      * @return An {@link Optional} holding the formatted value or an empty {@link Optional} if neither device type nor
77      *         tech type were present.
78      */
79     static Optional<String> getDeviceAndTechType(DeviceState deviceState) {
80         Optional<String> deviceType = deviceState.getType();
81         Optional<String> techType = deviceState.getTechType();
82         if (deviceType.isPresent() && techType.isPresent()) {
83             return Optional.of(deviceType.get() + " " + techType.get());
84         }
85         if (deviceType.isEmpty() && techType.isPresent()) {
86             return techType;
87         }
88         if (deviceType.isPresent() && techType.isEmpty()) {
89             return deviceType;
90         }
91         return Optional.empty();
92     }
93 }