]> git.basschouten.com Git - openhab-addons.git/blob
fce9c4edf454c020132384992331b31544cf81fb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.miio.internal.basic;
14
15 import java.util.Map;
16
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;
23
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonPrimitive;
28
29 /**
30  * Conditional Execution of rules
31  *
32  * @author Marcel Verpaalen - Initial contribution
33  */
34 @NonNullByDefault
35 public class ActionConditions {
36     private static final Logger LOGGER = LoggerFactory.getLogger(ActionConditions.class);
37
38     /**
39      * Check if it matches the firmware version.
40      *
41      * @param condition
42      * @param deviceVariables
43      * @param value
44      * @return value in case firmware is matching, return null if not
45      */
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
49         return value;
50     }
51
52     /**
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
55      *
56      * @param value
57      * @return
58      */
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");
63             } else {
64                 return new JsonPrimitive("on");
65             }
66         } else {
67             LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
68         }
69         return value;
70     }
71
72     /**
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
75      *
76      * @param value
77      * @return
78      */
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) {
83                 return value;
84             } else if (intVal > 100) {
85                 return new JsonPrimitive(100);
86             }
87             return null;
88         } else {
89             LOGGER.debug("Could not parse brightness. Value '{}' is not an int", value);
90         }
91         return value;
92     }
93
94     /**
95      * Check if the value is a color which can be send to Color channel.
96      * If not returns a null
97      *
98      * @param command
99      *
100      * @param value
101      * @return
102      */
103     private static @Nullable JsonElement hsbOnly(@Nullable Command command, @Nullable JsonElement value) {
104         if (command != null && command instanceof HSBType) {
105             return value;
106         }
107         return null;
108     }
109
110     /**
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
115      *
116      * @param condition.
117      * @param command
118      * @param value
119      * @return returnValue or value in case matching, return null if no match
120      */
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);
131                     if (matching) {
132                         if (matchCondition.has("returnValue")) {
133                             return matchCondition.get("returnValue");
134                         } else {
135                             return value;
136                         }
137                     }
138                 } else {
139                     LOGGER.debug("Json DB condition is missing matchValue element in match parameter array.");
140                 }
141             }
142         } else {
143             LOGGER.debug("Json DB condition is missing match parameter array.");
144         }
145         return null;
146     }
147
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()) {
151             case "FIRMWARE":
152                 return firmwareCheck(condition, deviceVariables, value);
153             case "BRIGHTNESSEXISTING":
154                 return brightnessExists(value);
155             case "BRIGHTNESSONOFF":
156                 return brightness(value);
157             case "HSBONLY":
158                 return hsbOnly(command, value);
159             case "MATCHVALUE":
160                 return matchValue(condition, command, value);
161             default:
162                 LOGGER.debug("Condition {} not found. Returning '{}'", condition,
163                         value != null ? value.toString() : "");
164                 return value;
165         }
166     }
167 }