2 * Copyright (c) 2010-2023 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.miio.internal.basic;
15 import java.awt.Color;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.types.HSBType;
21 import org.openhab.core.types.Command;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
31 * Conditional Execution of rules
33 * @author Marcel Verpaalen - Initial contribution
36 public class ActionConditions {
37 private static final Logger LOGGER = LoggerFactory.getLogger(ActionConditions.class);
40 * Check if it matches the firmware version.
43 * @param deviceVariables
45 * @return value in case firmware is matching, return null if not
47 private static @Nullable JsonElement firmwareCheck(MiIoDeviceActionCondition condition,
48 @Nullable Map<String, Object> deviceVariables, @Nullable JsonElement value) {
49 // TODO: placeholder for firmware version check to allow for firmware dependent actions
54 * Check if the value is a valid brightness for operating power On/Off switch.
55 * If brightness <1 returns Off, if >=1 returns On
60 private static @Nullable JsonElement brightness(@Nullable JsonElement value) {
61 if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
62 if (value.getAsInt() < 1) {
63 return new JsonPrimitive("off");
65 return new JsonPrimitive("on");
68 LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
74 * Convert HSV value to RGB+Brightness
77 * @return RGB value + brightness as first byte
79 private static @Nullable JsonElement hsvToBRGB(@Nullable Command command, @Nullable JsonElement value) {
80 if (command instanceof HSBType) {
81 HSBType hsb = (HSBType) command;
82 Color color = Color.getHSBColor(hsb.getHue().floatValue() / 360, hsb.getSaturation().floatValue() / 100,
83 hsb.getBrightness().floatValue() / 100);
84 return new JsonPrimitive((hsb.getBrightness().byteValue() << 24) + (color.getRed() << 16)
85 + (color.getGreen() << 8) + color.getBlue());
91 * Check if the value is a valid brightness between 1-100 which can be send to brightness channel.
92 * If not returns a null
97 private static @Nullable JsonElement brightnessExists(@Nullable JsonElement value) {
98 if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
99 int intVal = value.getAsInt();
100 if (intVal > 0 && intVal <= 100) {
102 } else if (intVal > 100) {
103 return new JsonPrimitive(100);
107 LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
113 * Check if the value is a color which can be send to Color channel.
114 * If not returns a null
121 private static @Nullable JsonElement hsbOnly(@Nullable Command command, @Nullable JsonElement value) {
122 if (command instanceof HSBType) {
129 * Check if the command value matches the condition value.
130 * The condition parameter element should be a Json array, containing Json objects with a matchValue element.
131 * Optionally it can contain a 'returnValue'element which will be returned in case of match.
132 * If no match this function will return a null
137 * @return returnValue or value in case matching, return null if no match
139 private static @Nullable JsonElement matchValue(MiIoDeviceActionCondition condition, @Nullable Command command,
140 @Nullable JsonElement value) {
141 if (condition.getParameters().isJsonArray() && command != null) {
142 JsonArray conditionArray = condition.getParameters().getAsJsonArray();
143 for (int i = 0; i < conditionArray.size(); i++) {
144 if (conditionArray.get(i).isJsonObject() && conditionArray.get(i).getAsJsonObject().has("matchValue")) {
145 JsonObject matchCondition = conditionArray.get(i).getAsJsonObject();
146 String matchvalue = matchCondition.get("matchValue").getAsString();
147 boolean matching = command.toString().matches(matchvalue);
148 LOGGER.trace("Matching '{}' with '{}': {}", matchvalue, command, matching);
150 if (matchCondition.has("returnValue")) {
151 return matchCondition.get("returnValue");
157 LOGGER.debug("Json DB condition is missing matchValue element in match parameter array.");
161 LOGGER.debug("Json DB condition is missing match parameter array.");
166 public static @Nullable JsonElement executeAction(MiIoDeviceActionCondition condition,
167 @Nullable Map<String, Object> deviceVariables, @Nullable JsonElement value, @Nullable Command command) {
168 switch (condition.getName().toUpperCase()) {
170 return firmwareCheck(condition, deviceVariables, value);
171 case "BRIGHTNESSEXISTING":
172 return brightnessExists(value);
174 return hsvToBRGB(command, value);
175 case "BRIGHTNESSONOFF":
176 return brightness(value);
178 return hsbOnly(command, value);
180 return matchValue(condition, command, value);
182 LOGGER.debug("Condition {} not found. Returning '{}'", condition,
183 value != null ? value.toString() : "");