From 43576061877c43d8a66900e6968d07ea794fd20d Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 21 Apr 2020 16:58:12 +0200 Subject: [PATCH] Code review --- .../smarthut/models/Automation.java | 2 +- .../smarthut/models/BooleanTrigger.java | 7 ++++++- .../smarthut/models/BooleanTriggerable.java | 5 +++++ .../sanmarinoes/smarthut/models/Dimmer.java | 4 +--- .../smarthut/models/KnobDimmer.java | 10 +++++++--- .../smarthut/models/MotionSensor.java | 7 ++++++- .../smarthut/models/RangeTrigger.java | 5 +++++ .../smarthut/models/RangeTriggerable.java | 5 +++++ .../smarthut/models/ScenePriority.java | 20 ++----------------- .../sanmarinoes/smarthut/models/Sensor.java | 7 ++++++- .../sanmarinoes/smarthut/models/Switch.java | 11 ++++++---- .../smarthut/models/Thermostat.java | 14 ++++++------- .../smarthut/service/DeviceService.java | 5 +++-- 13 files changed, 60 insertions(+), 42 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerable.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTriggerable.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java index 21b4ad3..9dfefe9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java @@ -23,7 +23,7 @@ public class Automation { @GsonExclude private Set> triggers = new HashSet<>(); - @OneToMany(mappedBy = "scenes", cascade = CascadeType.DETACH) + @OneToMany(mappedBy = "scenes", cascade = CascadeType.REMOVE) @GsonExclude private Set scenes = new HashSet<>(); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java index 4e35e1d..db83c94 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTrigger.java @@ -7,7 +7,7 @@ import javax.persistence.InheritanceType; @Entity @Inheritance(strategy = InheritanceType.JOINED) -public class BooleanTrigger extends Trigger { +public class BooleanTrigger extends Trigger { @Column(name = "switchable_on") private boolean on; @@ -23,4 +23,9 @@ public class BooleanTrigger extends Trigger { public boolean check(boolean on) { return this.on == on; } + + @Override + public boolean check() { + return getDevice().readTriggerState() == isOn(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerable.java new file mode 100644 index 0000000..6ff219f --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerable.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface BooleanTriggerable { + boolean readTriggerState(); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index 7ef0808..9b3d627 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -1,7 +1,6 @@ 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.*; @@ -19,8 +18,7 @@ public abstract class Dimmer extends InputDevice { @JoinTable( name = "dimmer_dimmable", joinColumns = @JoinColumn(name = "dimmer_id"), - inverseJoinColumns = @JoinColumn(name = "dimmable_id") - ) + inverseJoinColumns = @JoinColumn(name = "dimmable_id")) private Set dimmables = new HashSet<>(); /** diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index 87b527d..89273f8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -8,10 +8,9 @@ import javax.persistence.Entity; * value, like a knob) */ @Entity -public class KnobDimmer extends Dimmer { +public class KnobDimmer extends Dimmer implements RangeTriggerable { - @Column - Integer intensity = 0; + @Column private int intensity = 0; public KnobDimmer() { super("knobDimmer"); @@ -28,4 +27,9 @@ public class KnobDimmer extends Dimmer { dl.setIntensity(intensity); } } + + @Override + public double readTriggerState() { + return intensity; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java index 6c5baf7..4998922 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java @@ -5,7 +5,7 @@ import javax.persistence.Entity; /** Represents a motion sensor device */ @Entity -public class MotionSensor extends InputDevice { +public class MotionSensor extends InputDevice implements BooleanTriggerable { @Column(nullable = false) private boolean detected; @@ -21,4 +21,9 @@ public class MotionSensor extends InputDevice { public MotionSensor() { super("motionSensor"); } + + @Override + public boolean readTriggerState() { + return detected; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java index c1f5ce4..6c34f80 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java @@ -10,6 +10,11 @@ import javax.validation.constraints.NotNull; @Inheritance(strategy = InheritanceType.JOINED) public class RangeTrigger extends Trigger { + @Override + public boolean check(T d) { + return false; + } + enum Operator { EQUAL, LESS, diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTriggerable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTriggerable.java new file mode 100644 index 0000000..489a763 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTriggerable.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface RangeTriggerable { + double readTriggerState(); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java index 7ceb39e..0652e5e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriority.java @@ -1,11 +1,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; -import io.swagger.annotations.ApiModelProperty; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @@ -16,12 +13,6 @@ import javax.validation.constraints.NotNull; @Entity public class ScenePriority { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id", updatable = false, nullable = false, unique = true) - @ApiModelProperty(hidden = true) - private long id; - @ManyToOne @JoinColumn(name = "automation_id", updatable = false, insertable = false) @GsonExclude @@ -41,18 +32,11 @@ public class ScenePriority { @GsonExclude private Scene scene; - @Column(name = "scene_id", nullable = false) + @Id + @Column(name = "scene_id", nullable = false, updatable = false, unique = true) @NotNull private Long sceneId; - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - public Integer getPriority() { return priority; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index 6b5e6b9..2fb5442 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull; /** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */ @Entity -public class Sensor extends InputDevice { +public class Sensor extends InputDevice implements RangeTriggerable { public static final Map TYPICAL_VALUES = Map.of( @@ -19,6 +19,11 @@ public class Sensor extends InputDevice { SensorType.HUMIDITY, new BigDecimal(40.0), SensorType.LIGHT, new BigDecimal(1000)); + @Override + public double readTriggerState() { + return value.doubleValue(); + } + /** Type of sensor, i.e. of the thing the sensor measures. */ public enum SensorType { /** A sensor that measures temperature in degrees celsius */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index e78bf9f..0768d6b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -1,22 +1,20 @@ 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.*; /** A switch input device */ @Entity -public class Switch extends InputDevice { +public class Switch extends InputDevice implements BooleanTriggerable { @ManyToMany(cascade = CascadeType.DETACH) @GsonExclude @JoinTable( name = "switch_switchable", joinColumns = @JoinColumn(name = "switch_id"), - inverseJoinColumns = @JoinColumn(name = "switchable_id") - ) + inverseJoinColumns = @JoinColumn(name = "switchable_id")) private Set switchables = new HashSet<>(); /** The state of this switch */ @@ -57,4 +55,9 @@ public class Switch extends InputDevice { public Set getOutputs() { return switchables; } + + @Override + public boolean readTriggerState() { + return on; + } } 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 dc8b977..2dd3772 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 @@ -4,9 +4,7 @@ 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; @@ -15,7 +13,7 @@ import javax.validation.constraints.NotNull; /** A thermostat capable of controlling cooling and heating. */ @Entity -public class Thermostat extends Switchable { +public class Thermostat extends Switchable implements BooleanTriggerable { @Override public boolean isOn() { @@ -56,6 +54,11 @@ public class Thermostat extends Switchable { return true; } + @Override + public boolean readTriggerState() { + return mode != Mode.OFF; + } + public enum Mode { @SerializedName("OFF") OFF, @@ -92,11 +95,6 @@ public class Thermostat extends Switchable { 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 index f248356..d8bbb0d 100644 --- 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 @@ -28,9 +28,10 @@ public class DeviceService { public T save(T device) { - final Long deviceId = device.getId(); + final long deviceId = device.getId(); - List> triggers = triggerRepository.findAllByDeviceId(deviceId); + List> triggers = + triggerRepository.findAllByDeviceId(deviceId); // map activated -> automations // remove duplicates