]> git.basschouten.com Git - openhab-addons.git/blob
9ba7be57971d6caf0ba8fc1399f5fb6fc4c1ea81
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tplinksmarthome.internal;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.TreeMap;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.DeviceType;
23 import org.openhab.binding.tplinksmarthome.internal.model.Sysinfo;
24
25 /**
26  *
27  * @author Hilbrand Bouwkamp - Initial contribution
28  */
29 @NonNullByDefault
30 public final class PropertiesCollector {
31
32     private PropertiesCollector() {
33         // Util class
34     }
35
36     /**
37      * Collect all properties of the thing from the {@link Sysinfo} object.
38      *
39      * @param thingType thing to get the properties for
40      * @param ipAddress ip address of the device
41      * @param sysinfo system info data returned from the device
42      * @return map of properties
43      */
44     public static Map<String, Object> collectProperties(TPLinkSmartHomeThingType thingType, String ipAddress,
45             Sysinfo sysinfo) {
46         final Map<String, Object> properties = new TreeMap<>();
47
48         putNonNull(properties, CONFIG_IP, ipAddress);
49         if (thingType.getDeviceType() == DeviceType.RANGE_EXTENDER) {
50             collectPropertiesRangeExtender(properties, sysinfo);
51         } else {
52             collectProperties(properties, sysinfo);
53             if (thingType.getDeviceType() == DeviceType.BULB) {
54                 collectPropertiesBulb(properties, sysinfo);
55             } else {
56                 collectPropertiesOther(properties, sysinfo);
57             }
58         }
59         return properties;
60     }
61
62     /**
63      * Collect generic properties.
64      *
65      * @param properties properties object to store properties in
66      * @param sysinfo system info data returned from the device
67      */
68     private static void collectProperties(Map<String, Object> properties, Sysinfo sysinfo) {
69         putNonNull(properties, CONFIG_DEVICE_ID, sysinfo.getDeviceId());
70         putNonNull(properties, PROPERTY_MODEL, sysinfo.getModel());
71         putNonNull(properties, PROPERTY_HARDWARE_VERSION, sysinfo.getHwVer());
72         putNonNull(properties, PROPERTY_SOFWARE_VERSION, sysinfo.getSwVer());
73         putNonNull(properties, PROPERTY_HARDWARE_ID, sysinfo.getHwId());
74         putNonNull(properties, PROPERTY_OEM_ID, sysinfo.getOemId());
75     }
76
77     /**
78      * Collect Smart Bulb specific properties.
79      *
80      * @param properties properties object to store properties in
81      * @param sysinfo system info data returned from the device
82      */
83     private static void collectPropertiesBulb(Map<String, Object> properties, Sysinfo sysinfo) {
84         putNonNull(properties, PROPERTY_TYPE, sysinfo.getType());
85         putNonNull(properties, PROPERTY_MAC, sysinfo.getMac());
86         putNonNull(properties, PROPERTY_PROTOCOL_NAME, sysinfo.getProtocolName());
87         putNonNull(properties, PROPERTY_PROTOCOL_VERSION, sysinfo.getProtocolVersion());
88     }
89
90     /**
91      * Collect Smart Range Extender specific properties.
92      *
93      * @param properties properties object to store properties in
94      * @param sysinfo system info data returned from the device
95      */
96     private static void collectPropertiesRangeExtender(Map<String, Object> properties, Sysinfo sysinfo) {
97         final Sysinfo system = sysinfo.getSystem();
98         collectProperties(properties, system);
99         putNonNull(properties, PROPERTY_TYPE, system.getType());
100         putNonNull(properties, PROPERTY_MAC, system.getMac());
101         putNonNull(properties, PROPERTY_DEVICE_NAME, system.getDevName());
102         putNonNull(properties, PROPERTY_FEATURE, sysinfo.getPlug().getFeature());
103     }
104
105     /**
106      * Collect Smart Switch specific properties.
107      *
108      * @param properties properties object to store properties in
109      * @param sysinfo system info data returned from the device
110      */
111     private static void collectPropertiesOther(Map<String, Object> properties, Sysinfo sysinfo) {
112         putNonNull(properties, PROPERTY_TYPE, sysinfo.getType());
113         putNonNull(properties, PROPERTY_MAC, sysinfo.getMac());
114         putNonNull(properties, PROPERTY_DEVICE_NAME, sysinfo.getDevName());
115         putNonNull(properties, PROPERTY_FIRMWARE_ID, sysinfo.getFwId());
116         putNonNull(properties, PROPERTY_FEATURE, sysinfo.getFeature());
117     }
118
119     private static void putNonNull(Map<String, Object> properties, String key, @Nullable String value) {
120         if (value != null) {
121             properties.put(key, value);
122         }
123     }
124 }