diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java index 0957ce1..5b3f1e3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanCondition.java @@ -4,7 +4,7 @@ import javax.persistence.Column; import javax.persistence.Entity; @Entity -public class BooleanCondition +public class BooleanCondition extends Condition { // TODO add interface to type constraints @Column(name = "switchable_on") @@ -25,4 +25,9 @@ public class BooleanCondition public boolean check(boolean on) { return this.on == on; } + + @Override + public boolean triggered() { + return this.getDevice().readTriggerState() == isOn(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java index 9366e90..e643f24 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java @@ -13,7 +13,7 @@ import javax.persistence.Transient; import javax.validation.constraints.NotNull; @Entity -public class Condition { +public abstract class Condition { @Transient private String kind; @@ -67,6 +67,8 @@ public class Condition { @NotNull private Long automationId; + public abstract boolean triggered(); + public long getId() { return id; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java index 8283245..6047235 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java @@ -7,7 +7,7 @@ import javax.persistence.Entity; import javax.validation.constraints.NotNull; @Entity -public class RangeCondition extends Condition { +public class RangeCondition extends Condition { protected RangeCondition(String kind) { super(kind); @@ -49,4 +49,22 @@ public class RangeCondition extends Condition { public void setRange(Double range) { this.range = range; } + + @Override + public boolean triggered() { + double value = getDevice().readTriggerState(); + switch (operator) { + case EQUAL: + return value == range; + case LESS: + return value < range; + case GREATER: + return value > range; + case GREATER_EQUAL: + return value >= range; + case LESS_EQUAL: + return value <= range; + } + throw new IllegalStateException(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java new file mode 100644 index 0000000..439f32d --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java @@ -0,0 +1,54 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import com.google.gson.annotations.SerializedName; +import javax.persistence.Column; +import javax.persistence.Entity; + +@Entity +public class ThermostatCondition extends Condition { + + protected ThermostatCondition(String kind) { + super(kind); + } + + public enum Operator { + @SerializedName("EQUAL") + EQUAL, + @SerializedName("NOTEQUAL") + NOTEQUAL, + } + + @Column(nullable = false) + private ThermostatCondition.Operator operator; + + @Column(nullable = false) + private Thermostat.Mode mode; + + public Operator getOperator() { + return operator; + } + + public void setOperator(Operator operator) { + this.operator = operator; + } + + public Thermostat.Mode getState() { + return mode; + } + + public void setState(Thermostat.Mode state) { + this.mode = state; + } + + @Override + public boolean triggered() { + switch (operator) { + case EQUAL: + return getDevice().getMode() == mode; + case NOTEQUAL: + return getDevice().getMode() != mode; + default: + return false; + } + } +}