Pseudocode for automation implemmentation

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-20 14:14:04 +02:00
parent 5c0df55de9
commit 319629a681
4 changed files with 64 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -91,4 +92,8 @@ public abstract class Device {
this.kind = kind; this.kind = kind;
this.flowType = flowType; this.flowType = flowType;
} }
public List<Trigger<? extends Device>> triggeredTriggers() {
return List.of();
}
} }

View file

@ -1,9 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Inheritance; import javax.persistence.Inheritance;
import javax.persistence.InheritanceType; import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
/** /**
* A generic abstraction for an input device, i.e. something that captures input either from the * 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 @Entity
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.JOINED)
public abstract class InputDevice extends Device { public abstract class InputDevice extends Device {
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
private Set<Trigger<? extends Device>> triggers = new HashSet<>();
public InputDevice(String kind) { public InputDevice(String kind) {
super(kind, FlowType.INPUT); super(kind, FlowType.INPUT);
} }

View file

@ -1,9 +1,15 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal; 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.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.validation.constraints.NotNull; 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; * Computes the new thermostat state, for when the thermostat is on;
*
* @return true if the state changed, false if not; * @return true if the state changed, false if not;
*/ */
public boolean computeState() { public boolean computeState() {
@ -75,12 +82,21 @@ public class Thermostat extends Switchable {
@Column private boolean useExternalSensors = false; @Column private boolean useExternalSensors = false;
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
private Set<BooleanTrigger<? extends Device>> triggers = new HashSet<>();
/** Creates a thermostat with a temperature sensor and its initial OFF state */ /** Creates a thermostat with a temperature sensor and its initial OFF state */
public Thermostat() { public Thermostat() {
super("thermostat"); super("thermostat");
this.mode = Mode.OFF; this.mode = Mode.OFF;
} }
@Override
public List<Trigger<? extends Device>> triggeredTriggers() {
return triggers.stream().filter(t -> t.check(this.isOn())).collect(Collectors.toList());
}
public void setMode(Mode state) { public void setMode(Mode state) {
this.mode = state; this.mode = state;
} }

View file

@ -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<Trigger>
- OrderedList<Scene> // 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<Device> deviceRepository;
public <T extends Device> T save(T device) {
List<Trigger<? extends Device>> activated = device.triggeredTriggers();
// map activated -> automations
// remove duplicates
// sort automations per priority
// sort scenes inside automations per priority
// apply scenes (SceneService.apply())
}
}