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.io.hueemulation.internal.dto;
15 import java.lang.reflect.Type;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.io.hueemulation.internal.dto.changerequest.HueCommand;
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;
34 * @author David Graeff - Initial contribution
37 public class HueRuleEntry {
38 // A unique, editable name given to the group.
39 public String name = "";
40 public String description = "";
42 public String owner = "";
44 public static enum Operator {
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.
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.
60 * A complete condition could look like this:
65 * "address": "/sensors/2/state/buttonevent",
71 public static class Condition {
73 * A hue resource address like "/config/localtime" or "/sensors/2/state/buttonevent"
75 public String address = "";
76 public Operator operator = Operator.unknown;
80 public @Nullable String value;
85 public Condition(String address, Operator operator, @Nullable String value) {
86 this.address = address;
88 this.operator = operator;
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<>();
100 public HueRuleEntry(@Nullable String name) {
101 this.name = name != null ? name : "";
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.
108 @NonNullByDefault({})
109 public static class SerializerCondition
110 implements JsonSerializer<HueRuleEntry.Condition>, JsonDeserializer<HueRuleEntry.Condition> {
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;
117 jObj.addProperty("value", value);
119 jObj.addProperty("operator", product.operator.name().replace("_", " "));
125 public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
126 throws JsonParseException {
127 JsonObject jObj = json.getAsJsonObject();
128 Condition c = new Condition();
130 c.address = jObj.get("address").getAsString();
131 if (jObj.has("value")) {
132 c.value = jObj.get("value").getAsString();
134 String operator = jObj.get("operator").getAsString().replace(" ", "_");
135 c.operator = Operator.valueOf(operator);