2 * Copyright (c) 2010-2020 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;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.HSBType;
20 import org.openhab.core.types.Command;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonPrimitive;
30 * Conditional Execution of rules
32 * @author Marcel Verpaalen - Initial contribution
35 public class ActionConditions {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ActionConditions.class);
39 * Check if it matches the firmware version.
42 * @param deviceVariables
44 * @return value in case firmware is matching, return null if not
46 private static @Nullable JsonElement firmwareCheck(MiIoDeviceActionCondition condition,
47 @Nullable Map<String, Object> deviceVariables, @Nullable JsonElement value) {
48 // TODO: placeholder for firmware version check to allow for firmware dependent actions
53 * Check if the value is a valid brightness for operating power On/Off switch.
54 * If brightness <1 returns Off, if >=1 returns On
59 private static @Nullable JsonElement brightness(@Nullable JsonElement value) {
60 if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
61 if (value.getAsInt() < 1) {
62 return new JsonPrimitive("off");
64 return new JsonPrimitive("on");
67 LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
73 * Check if the value is a valid brightness between 1-100 which can be send to brightness channel.
74 * If not returns a null
79 private static @Nullable JsonElement brightnessExists(@Nullable JsonElement value) {
80 if (value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) {
81 int intVal = value.getAsInt();
82 if (intVal > 0 && intVal <= 100) {
84 } else if (intVal > 100) {
85 return new JsonPrimitive(100);
89 LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
95 * Check if the value is a color which can be send to Color channel.
96 * If not returns a null
103 private static @Nullable JsonElement hsbOnly(@Nullable Command command, @Nullable JsonElement value) {
104 if (command != null && command instanceof HSBType) {
111 * Check if the command value matches the condition value.
112 * The condition parameter element should be a Json array, containing Json objects with a matchValue element.
113 * Optionally it can contain a 'returnValue'element which will be returned in case of match.
114 * If no match this function will return a null
119 * @return returnValue or value in case matching, return null if no match
121 private static @Nullable JsonElement matchValue(MiIoDeviceActionCondition condition, @Nullable Command command,
122 @Nullable JsonElement value) {
123 if (condition.getParameters().isJsonArray() && command != null) {
124 JsonArray conditionArray = condition.getParameters().getAsJsonArray();
125 for (int i = 0; i < conditionArray.size(); i++) {
126 if (conditionArray.get(i).isJsonObject() && conditionArray.get(i).getAsJsonObject().has("matchValue")) {
127 JsonObject matchCondition = conditionArray.get(i).getAsJsonObject();
128 String matchvalue = matchCondition.get("matchValue").getAsString();
129 boolean matching = command.toString().matches(matchvalue);
130 LOGGER.trace("Matching '{}' with '{}': {}", matchvalue, command, matching);
132 if (matchCondition.has("returnValue")) {
133 return matchCondition.get("returnValue");
139 LOGGER.debug("Json DB condition is missing matchValue element in match parameter array.");
143 LOGGER.debug("Json DB condition is missing match parameter array.");
148 public static @Nullable JsonElement executeAction(MiIoDeviceActionCondition condition,
149 @Nullable Map<String, Object> deviceVariables, @Nullable JsonElement value, @Nullable Command command) {
150 switch (condition.getName().toUpperCase()) {
152 return firmwareCheck(condition, deviceVariables, value);
153 case "BRIGHTNESSEXISTING":
154 return brightnessExists(value);
155 case "BRIGHTNESSONOFF":
156 return brightness(value);
158 return hsbOnly(command, value);
160 return matchValue(condition, command, value);
162 LOGGER.debug("Condition {} not found. Returning '{}'", condition,
163 value != null ? value.toString() : "");