]> git.basschouten.com Git - openhab-addons.git/blob
c7a824a4528a9543c261ecd737148af1b07c8f94
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.io.hueemulation.internal.dto;
14
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.io.hueemulation.internal.dto.changerequest.HueCommand;
22
23 import com.google.gson.JsonDeserializationContext;
24 import com.google.gson.JsonDeserializer;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParseException;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30
31 /**
32  * Hue API rule object
33  *
34  * @author David Graeff - Initial contribution
35  */
36 @NonNullByDefault
37 public class HueRuleEntry {
38     // A unique, editable name given to the group.
39     public String name = "";
40     public String description = "";
41
42     public String owner = "";
43
44     public static enum Operator {
45         unknown,
46         eq, // equals, Used for bool and int.
47         gt, // greater than, Allowed on int values.
48         lt, // less than, Allowed on int values.
49         dx, // value has changed, Time (timestamps) int and bool values. Only dx or ddx is allowed, but not both.
50         ddx, // delayed value has changed
51         stable, // Time (timestamps) int and bool values. An attribute has or has not changed for a given time.
52         not_stable,
53         in, // Current time is in or not in given time interval (only for /config/localtime, not UTC). “in” rule will be
54             // triggered on starttime and “not in” rule will be triggered on endtime. Only one “in” operator is allowed
55             // in a rule. Multiple “not in” operators are allowed in a rule.
56         not_in
57     }
58
59     /**
60      * A complete condition could look like this:
61      * <p>
62      *
63      * <pre>
64      * {
65      *       "address": "/sensors/2/state/buttonevent",
66      *       "operator": "eq",
67      *       "value": "16"
68      * },
69      * </pre>
70      */
71     public static class Condition {
72         /**
73          * A hue resource address like "/config/localtime" or "/sensors/2/state/buttonevent"
74          */
75         public String address = "";
76         public Operator operator = Operator.unknown;
77         /**
78          * A value like "16"
79          */
80         public @Nullable String value;
81
82         public Condition() {
83         }
84
85         public Condition(String address, Operator operator, @Nullable String value) {
86             this.address = address;
87             this.value = value;
88             this.operator = operator;
89         }
90     }
91
92     // The IDs of the lights that are in the group.
93     public List<Condition> conditions = new ArrayList<>();
94     public List<HueCommand> actions = new ArrayList<>();
95
96     HueRuleEntry() {
97         name = "";
98     }
99
100     public HueRuleEntry(@Nullable String name) {
101         this.name = name != null ? name : "";
102     }
103
104     /**
105      * This custom serializer (de)serialize the Condition class and translates between the enum values that have
106      * an underbar and the json strings that have a whitespace instead.
107      */
108     @NonNullByDefault({})
109     public static class SerializerCondition
110             implements JsonSerializer<HueRuleEntry.Condition>, JsonDeserializer<HueRuleEntry.Condition> {
111         @Override
112         public JsonElement serialize(HueRuleEntry.Condition product, Type type, JsonSerializationContext context) {
113             JsonObject jObj = new JsonObject();
114             jObj.addProperty("address", product.address);
115             String value = product.value;
116             if (value != null) {
117                 jObj.addProperty("value", value);
118             }
119             jObj.addProperty("operator", product.operator.name().replace("_", " "));
120
121             return jObj;
122         }
123
124         @Override
125         public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
126                 throws JsonParseException {
127             JsonObject jObj = json.getAsJsonObject();
128             Condition c = new Condition();
129
130             c.address = jObj.get("address").getAsString();
131             if (jObj.has("value")) {
132                 c.value = jObj.get("value").getAsString();
133             }
134             String operator = jObj.get("operator").getAsString().replace(" ", "_");
135             c.operator = Operator.valueOf(operator);
136             return c;
137         }
138     }
139 }