This commit is contained in:
omenem 2020-05-07 16:38:45 +02:00
parent 74364effe6
commit 1f5f948e23
4 changed files with 82 additions and 3 deletions

View file

@ -4,7 +4,7 @@ import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class BooleanCondition<D extends Device>
public class BooleanCondition<D extends Device & BooleanTriggerable>
extends Condition<D> { // TODO add interface to type constraints
@Column(name = "switchable_on")
@ -25,4 +25,9 @@ public class BooleanCondition<D extends Device>
public boolean check(boolean on) {
return this.on == on;
}
@Override
public boolean triggered() {
return this.getDevice().readTriggerState() == isOn();
}
}

View file

@ -13,7 +13,7 @@ import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
@Entity
public class Condition<D extends Device> {
public abstract class Condition<D extends Device> {
@Transient private String kind;
@ -67,6 +67,8 @@ public class Condition<D extends Device> {
@NotNull
private Long automationId;
public abstract boolean triggered();
public long getId() {
return id;
}

View file

@ -7,7 +7,7 @@ import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
@Entity
public class RangeCondition<D extends Device> extends Condition<D> {
public class RangeCondition<D extends Device & RangeTriggerable> extends Condition<D> {
protected RangeCondition(String kind) {
super(kind);
@ -49,4 +49,22 @@ public class RangeCondition<D extends Device> extends Condition<D> {
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();
}
}

View file

@ -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<Thermostat> {
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;
}
}
}