From 319629a681da900c0963a9e288eb92dad7c57227 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Mon, 20 Apr 2020 14:14:04 +0200 Subject: [PATCH] Pseudocode for automation implemmentation --- .../sanmarinoes/smarthut/models/Device.java | 5 +++ .../smarthut/models/InputDevice.java | 7 ++++ .../smarthut/models/Thermostat.java | 16 +++++++++ .../smarthut/service/DeviceService.java | 36 +++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 0114ac3..7fd7b80 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import com.google.gson.annotations.SerializedName; import io.swagger.annotations.ApiModelProperty; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.persistence.*; import javax.validation.constraints.NotNull; @@ -91,4 +92,8 @@ public abstract class Device { this.kind = kind; this.flowType = flowType; } + + public List> triggeredTriggers() { + return List.of(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java index 2acf0c2..0623872 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java @@ -1,9 +1,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; +import javax.persistence.OneToMany; /** * A generic abstraction for an input device, i.e. something that captures input either from the @@ -12,6 +15,10 @@ import javax.persistence.InheritanceType; @Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class InputDevice extends Device { + @OneToMany(mappedBy = "scene", orphanRemoval = true) + @GsonExclude + private Set> triggers = new HashSet<>(); + public InputDevice(String kind) { super(kind, FlowType.INPUT); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java index 7c54d54..dc8b977 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java @@ -1,9 +1,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import com.google.gson.annotations.SerializedName; import java.math.BigDecimal; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.OneToMany; import javax.persistence.Transient; import javax.validation.constraints.NotNull; @@ -24,6 +30,7 @@ public class Thermostat extends Switchable { /** * Computes the new thermostat state, for when the thermostat is on; + * * @return true if the state changed, false if not; */ public boolean computeState() { @@ -75,12 +82,21 @@ public class Thermostat extends Switchable { @Column private boolean useExternalSensors = false; + @OneToMany(mappedBy = "scene", orphanRemoval = true) + @GsonExclude + private Set> triggers = new HashSet<>(); + /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { super("thermostat"); this.mode = Mode.OFF; } + @Override + public List> triggeredTriggers() { + return triggers.stream().filter(t -> t.check(this.isOn())).collect(Collectors.toList()); + } + public void setMode(Mode state) { this.mode = state; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java new file mode 100644 index 0000000..c843c42 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -0,0 +1,36 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.service; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class DeviceService { + + /* + Automation entity + - name + - id + - Set + - OrderedList // define order of application of scenes of the automation + // separate AutomationWithOrder id entity: + // - application_order_id (1 for first, 2 for second) + // - scene + // - FK automation + */ + + @Autowired DeviceRepository deviceRepository; + + public T save(T device) { + List> activated = device.triggeredTriggers(); + + // map activated -> automations + // remove duplicates + // sort automations per priority + // sort scenes inside automations per priority + // apply scenes (SceneService.apply()) + } +}