]> git.basschouten.com Git - openhab-addons.git/blob
2b50a83127e668a18c92e47abc68c35dc1b43b91
[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.freeathomesystem.internal.datamodel;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24
25 /**
26  * The {@link FreeAtHomeDeviceDescription} is responsible for determining the device type
27  * based on the received json string
28  *
29  * @author Andras Uhrin - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public class FreeAtHomeDeviceDescription {
34
35     private final Logger logger = LoggerFactory.getLogger(FreeAtHomeDeviceDescription.class);
36
37     // interface strings
38     public static final String DEVICE_INTERFACE_UNKNOWN_TYPE = "unknown";
39     public static final String DEVICE_INTERFACE_WIRELESS_TYPE = "wireless";
40     public static final String DEVICE_INTERFACE_VIRTUAL_TYPE = "virtual";
41     public static final String DEVICE_INTERFACE_WIRED_TYPE = "wired";
42     public static final String DEVICE_INTERFACE_HUE_TYPE = "hue";
43
44     public String deviceLabel = "";
45     public String deviceId = "";
46     public String interfaceType = "";
47     public boolean validDevice = false;
48
49     private boolean sceneIsDetected;
50     private boolean ruleIsDetected;
51
52     public List<FreeAtHomeDeviceChannel> listOfChannels = new ArrayList<>();
53
54     public FreeAtHomeDeviceDescription() {
55         validDevice = false;
56     }
57
58     public FreeAtHomeDeviceDescription(JsonObject jsonObject, String id) {
59         // set the device ID
60         deviceId = id;
61
62         // set the device invalid at first
63         validDevice = false;
64
65         sceneIsDetected = id.toLowerCase().startsWith("ffff48");
66         ruleIsDetected = id.toLowerCase().startsWith("ffff4a");
67
68         JsonObject jsonObjectOfId = jsonObject.getAsJsonObject(id);
69
70         if (jsonObjectOfId == null) {
71             return;
72         }
73
74         JsonElement jsonObjectOfInterface = jsonObjectOfId.get("interface");
75
76         if (jsonObjectOfInterface != null) {
77             String interfaceString = jsonObjectOfInterface.getAsString();
78
79             if (interfaceString.toLowerCase().startsWith("vdev:")) {
80                 interfaceType = DEVICE_INTERFACE_VIRTUAL_TYPE;
81             } else if (interfaceString.toLowerCase().startsWith("hue")) {
82                 interfaceType = DEVICE_INTERFACE_HUE_TYPE;
83             } else if (interfaceString.toLowerCase().startsWith("rf")) {
84                 interfaceType = DEVICE_INTERFACE_WIRELESS_TYPE;
85             } else if (interfaceString.toLowerCase().startsWith("tp")) {
86                 interfaceType = DEVICE_INTERFACE_WIRED_TYPE;
87             } else {
88                 interfaceType = DEVICE_INTERFACE_UNKNOWN_TYPE;
89             }
90         } else {
91             interfaceType = DEVICE_INTERFACE_UNKNOWN_TYPE;
92         }
93
94         JsonElement jsonObjectOfDeviceLabel = jsonObjectOfId.get("displayName");
95
96         if (jsonObjectOfDeviceLabel == null) {
97             this.deviceLabel = "NoName";
98         } else {
99             this.deviceLabel = jsonObjectOfDeviceLabel.getAsString();
100         }
101
102         if (this.deviceLabel.isEmpty()) {
103             this.deviceLabel = "NoName";
104         }
105
106         JsonObject jsonObjectOfChannels = jsonObjectOfId.getAsJsonObject("channels");
107
108         logger.debug("Detecting device features - device id: {} - device label: {}", this.deviceId, this.deviceLabel);
109
110         if (jsonObjectOfChannels != null) {
111             // Scan channels for functions
112             for (String nextChannel : jsonObjectOfChannels.keySet()) {
113                 FreeAtHomeDeviceChannel newChannel = new FreeAtHomeDeviceChannel();
114
115                 if (newChannel.createChannelFromJson(deviceLabel, nextChannel, jsonObjectOfChannels, sceneIsDetected,
116                         ruleIsDetected)) {
117                     if (interfaceType == DEVICE_INTERFACE_VIRTUAL_TYPE) {
118                         newChannel.applyChangesForVirtualDevice();
119                     }
120
121                     listOfChannels.add(newChannel);
122                 }
123             }
124         }
125     }
126
127     public boolean isRule() {
128         return ruleIsDetected;
129     }
130
131     public boolean isScene() {
132         return sceneIsDetected;
133     }
134
135     public boolean isVirtual() {
136         return interfaceType == DEVICE_INTERFACE_VIRTUAL_TYPE;
137     }
138
139     public int getNumberOfChannels() {
140         return listOfChannels.size();
141     }
142
143     public FreeAtHomeDeviceChannel getChannel(int idx) {
144         return listOfChannels.get(idx);
145     }
146
147     public String getDeviceId() {
148         return deviceId;
149     }
150
151     public String getDeviceLabel() {
152         return deviceLabel;
153     }
154 }