2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeathomesystem.internal.datamodel;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
26 * The {@link FreeAtHomeDeviceDescription} is responsible for determining the device type
27 * based on the received json string
29 * @author Andras Uhrin - Initial contribution
33 public class FreeAtHomeDeviceDescription {
35 private final Logger logger = LoggerFactory.getLogger(FreeAtHomeDeviceDescription.class);
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";
44 public String deviceLabel = "";
45 public String deviceId = "";
46 public String interfaceType = "";
47 public boolean validDevice = false;
49 private boolean sceneIsDetected;
50 private boolean ruleIsDetected;
52 public List<FreeAtHomeDeviceChannel> listOfChannels = new ArrayList<>();
54 public FreeAtHomeDeviceDescription() {
58 public FreeAtHomeDeviceDescription(JsonObject jsonObject, String id) {
62 // set the device invalid at first
65 sceneIsDetected = id.toLowerCase().startsWith("ffff48");
66 ruleIsDetected = id.toLowerCase().startsWith("ffff4a");
68 JsonObject jsonObjectOfId = jsonObject.getAsJsonObject(id);
70 if (jsonObjectOfId == null) {
74 JsonElement jsonObjectOfInterface = jsonObjectOfId.get("interface");
76 if (jsonObjectOfInterface != null) {
77 String interfaceString = jsonObjectOfInterface.getAsString();
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;
88 interfaceType = DEVICE_INTERFACE_UNKNOWN_TYPE;
91 interfaceType = DEVICE_INTERFACE_UNKNOWN_TYPE;
94 JsonElement jsonObjectOfDeviceLabel = jsonObjectOfId.get("displayName");
96 if (jsonObjectOfDeviceLabel == null) {
97 this.deviceLabel = "NoName";
99 this.deviceLabel = jsonObjectOfDeviceLabel.getAsString();
102 if (this.deviceLabel.isEmpty()) {
103 this.deviceLabel = "NoName";
106 JsonObject jsonObjectOfChannels = jsonObjectOfId.getAsJsonObject("channels");
108 logger.debug("Detecting device features - device id: {} - device label: {}", this.deviceId, this.deviceLabel);
110 if (jsonObjectOfChannels != null) {
111 // Scan channels for functions
112 for (String nextChannel : jsonObjectOfChannels.keySet()) {
113 FreeAtHomeDeviceChannel newChannel = new FreeAtHomeDeviceChannel();
115 if (newChannel.createChannelFromJson(deviceLabel, nextChannel, jsonObjectOfChannels, sceneIsDetected,
117 if (interfaceType == DEVICE_INTERFACE_VIRTUAL_TYPE) {
118 newChannel.applyChangesForVirtualDevice();
121 listOfChannels.add(newChannel);
127 public boolean isRule() {
128 return ruleIsDetected;
131 public boolean isScene() {
132 return sceneIsDetected;
135 public boolean isVirtual() {
136 return interfaceType == DEVICE_INTERFACE_VIRTUAL_TYPE;
139 public int getNumberOfChannels() {
140 return listOfChannels.size();
143 public FreeAtHomeDeviceChannel getChannel(int idx) {
144 return listOfChannels.get(idx);
147 public String getDeviceId() {
151 public String getDeviceLabel() {