From 495c317eb887f086bdfe06154cb08993177cd3ee Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Wed, 15 Apr 2020 23:08:57 +0200 Subject: [PATCH] Added apply() method on State to change underlying device accordingly --- .../controller/ThermostatController.java | 5 ++--- .../models/AlterableFromDimmableState.java | 13 +++++++++++ .../smarthut/models/AlterableFromState.java | 6 +++++ .../models/AlterableFromSwitchableState.java | 13 +++++++++++ .../sanmarinoes/smarthut/models/Curtains.java | 12 +++++++++- .../smarthut/models/DimmableLight.java | 12 +++++++++- ...ableLightState.java => DimmableState.java} | 14 ++++++------ .../smarthut/models/IDimmable.java | 10 +++++++++ .../smarthut/models/ISwtichable.java | 20 +++++++++++++++++ .../smarthut/models/RegularLight.java | 2 +- .../smarthut/models/SecurityCamera.java | 2 +- .../smarthut/models/SmartPlug.java | 2 +- .../sanmarinoes/smarthut/models/State.java | 19 +++++++++++----- .../smarthut/models/Switchable.java | 16 +------------- .../smarthut/models/SwitchableState.java | 4 ++-- .../smarthut/models/Thermostat.java | 22 ++++++++++++------- .../smarthut/models/ThermostatState.java | 16 ++++++++++++++ .../smarthut/service/ThermostatService.java | 14 ++++++------ 18 files changed, 150 insertions(+), 52 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromDimmableState.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromState.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromSwitchableState.java rename src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/{DimmableLightState.java => DimmableState.java} (55%) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/IDimmable.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ISwtichable.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatState.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java index 57e1ca9..00899c8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java @@ -5,7 +5,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService; import java.security.Principal; -import java.util.*; import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -42,10 +41,10 @@ public class ThermostatController { newT.setUseExternalSensors(t.isUseExternalSensors()); if (t.isTurnOn()) { - newT.setState(Thermostat.ThermostatState.IDLE); + newT.setMode(Thermostat.Mode.IDLE); thermostatService.computeState(newT); } else { - newT.setState(Thermostat.ThermostatState.OFF); + newT.setMode(Thermostat.Mode.OFF); } newT = thermostatRepository.save(newT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromDimmableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromDimmableState.java new file mode 100644 index 0000000..64b7039 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromDimmableState.java @@ -0,0 +1,13 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** + * An IDimmable Device whose state can be set by a DimmableState of the corresponding type + * @param the type of the class that extends this interface (just for type bounds, does not mean actually anything) + */ +public interface AlterableFromDimmableState> + extends IDimmable, AlterableFromState> { + default void readStateAndSet(State state) { + final DimmableState hack = (DimmableState) state; + setDimAmount(hack.getDimAmount()); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromState.java new file mode 100644 index 0000000..235017c --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromState.java @@ -0,0 +1,6 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** An OutputDevice whose state can be set by a State of corresponding device type */ +public interface AlterableFromState> { + void readStateAndSet(T state); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromSwitchableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromSwitchableState.java new file mode 100644 index 0000000..a4f2d29 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AlterableFromSwitchableState.java @@ -0,0 +1,13 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** + * An ISwitchable Device whose state can be set by a SwitchableState of the corresponding type + * @param the type of the class that extends this interface (just for type bounds, does not mean actually anything) + */ +public interface AlterableFromSwitchableState> + extends ISwtichable, AlterableFromState> { + default void readStateAndSet(State state) { + final SwitchableState hack = (SwitchableState) state; + setOn(hack.isOn()); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java index cfc3c2f..00fa464 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java @@ -7,7 +7,7 @@ import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; @Entity -public class Curtains extends OutputDevice { +public class Curtains extends OutputDevice implements IDimmable, AlterableFromDimmableState { /** * it represents how much the curtains are opened, 0 is completely closed 100 is completely open @@ -38,4 +38,14 @@ public class Curtains extends OutputDevice { this.openedAmount = newOpening; } } + + @Override + public Integer getDimAmount() { + return getOpenedAmount(); + } + + @Override + public void setDimAmount(Integer intensity) { + setOpenedAmount(intensity); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java index 6b537c6..300ec6e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java @@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull; /** Represent a dimmable light */ @Entity -public class DimmableLight extends Switchable { +public class DimmableLight extends Switchable implements IDimmable, AlterableFromDimmableState { public static final Connector BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR = @@ -86,4 +86,14 @@ public class DimmableLight extends Switchable { super.setSwitchId(switchId); this.dimmerId = null; } + + @Override + public Integer getDimAmount() { + return getIntensity(); + } + + @Override + public void setDimAmount(Integer intensity) { + setIntensity(intensity); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java similarity index 55% rename from src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightState.java rename to src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java index 6162cb7..d04c089 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java @@ -6,22 +6,22 @@ import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; -/** Represent a dimmable light state */ +/** Represent a state for an IDimmable device */ @Entity -public class DimmableLightState extends State { +public class DimmableState>> extends State { /** The light intensity value. Goes from 0 (off) to 100 (on) */ @NotNull @Column(nullable = false) @Min(0) @Max(100) - private Integer intensity = 0; + private Integer dimAmount = 0; - public Integer getIntensity() { - return intensity; + public Integer getDimAmount() { + return dimAmount; } - public void setIntensity(Integer intensity) { - this.intensity = intensity; + public void setDimAmount(Integer dimAmount) { + this.dimAmount = dimAmount; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/IDimmable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/IDimmable.java new file mode 100644 index 0000000..3ee8a77 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/IDimmable.java @@ -0,0 +1,10 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** + * Although not totally enforced in code, it represents an OutputDevice that can be set a state (here "dimAmount") + * that ranges from 0 to 100 + */ +public interface IDimmable { + Integer getDimAmount(); + void setDimAmount(Integer intensity); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ISwtichable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ISwtichable.java new file mode 100644 index 0000000..fda22fa --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ISwtichable.java @@ -0,0 +1,20 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** Interface equivalent of Switchable. Altough not totally enforced in code, it represents an OutputDevice that can be + * turned on or off + */ +public interface ISwtichable { + /** + * Returns whether the device is on (true) or not (false) + * + * @return whether the device is on (true) or not (false) + */ + boolean isOn(); + + /** + * Sets the on status of the device + * + * @param on the new on status: true for on, false for off + */ + void setOn(boolean on); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java index 2dcff1b..5d8cb3f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java @@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull; /** Represents a standard non-dimmable light */ @Entity -public class RegularLight extends Switchable { +public class RegularLight extends Switchable implements AlterableFromSwitchableState { /** Whether the light is on or not */ @Column(name = "light_on", nullable = false) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java index 7ddde5d..be0f559 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SecurityCamera.java @@ -5,7 +5,7 @@ import javax.persistence.Entity; import javax.validation.constraints.NotNull; @Entity -public class SecurityCamera extends Switchable { +public class SecurityCamera extends Switchable implements AlterableFromSwitchableState { public SecurityCamera() { super("securityCamera"); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java index 9b07e69..9f138d9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -7,7 +7,7 @@ import javax.validation.constraints.NotNull; /** A smart plug that can be turned either on or off */ @Entity -public class SmartPlug extends Switchable { +public class SmartPlug extends Switchable implements AlterableFromSwitchableState { /** The average consumption of an active plug when on in Watt */ public static final Double AVERAGE_CONSUMPTION_KW = 200.0; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java index 25e9e31..8e58f76 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java @@ -2,6 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import io.swagger.annotations.ApiModelProperty; +import org.hibernate.annotations.Type; + import javax.persistence.*; import javax.validation.constraints.NotNull; @@ -11,7 +13,7 @@ import javax.validation.constraints.NotNull; */ @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) -public abstract class State { +public abstract class State>> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -19,10 +21,10 @@ public abstract class State { @ApiModelProperty(hidden = true) private long id; - @ManyToOne + @ManyToOne(targetEntity = OutputDevice.class) @JoinColumn(name = "device_id", updatable = false, insertable = false) @GsonExclude - private Device device; + private D device; /** * The device this state belongs in, as a foreign key id. To use when updating and inserting @@ -41,6 +43,13 @@ public abstract class State { @NotNull private Long sceneId; + /** + * Sets the state of the connected device to the state represented by this object. + */ + public void apply() { + device.readStateAndSet(this); + } + public long getId() { return id; } @@ -49,11 +58,11 @@ public abstract class State { this.id = id; } - public Device getDevice() { + public D getDevice() { return device; } - public void setDevice(Device device) { + public void setDevice(D device) { this.device = device; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index 5ba0702..898c6a4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -6,7 +6,7 @@ import javax.persistence.*; /** A device that can be turned either on or off */ @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) -public abstract class Switchable extends OutputDevice { +public abstract class Switchable extends OutputDevice implements ISwtichable { public static final Connector SWITCH_SWITCHABLE_CONNECTOR = Connector.basic(Switch::getOutputs, Switchable::setSwitchId); @@ -23,20 +23,6 @@ public abstract class Switchable extends OutputDevice { super(kind); } - /** - * Returns whether the device is on (true) or not (false) - * - * @return whether the device is on (true) or not (false) - */ - public abstract boolean isOn(); - - /** - * Sets the on status of the device - * - * @param on the new on status: true for on, false for off - */ - public abstract void setOn(boolean on); - public Long getSwitchId() { return switchId; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java index 95efe8a..9260801 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java @@ -4,9 +4,9 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.validation.constraints.NotNull; -/** A state for RegularLight and SmartPlug */ +/** Represents a state for a Switchable device */ @Entity -public class SwitchableState extends State { +public class SwitchableState>> extends State { @Column(name = "switchable_on", nullable = false) @NotNull 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 8524b8f..61081f5 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 @@ -9,9 +9,15 @@ import javax.validation.constraints.NotNull; /** A thermostat capable of controlling cooling and heating. */ @Entity -public class Thermostat extends OutputDevice { +public class Thermostat extends OutputDevice implements AlterableFromState> { - public enum ThermostatState { + @Override + public void readStateAndSet(State state) { + final ThermostatState hack = (ThermostatState) state; + setMode(hack.getMode()); + } + + public enum Mode { @SerializedName("OFF") OFF, @SerializedName("IDLE") @@ -31,7 +37,7 @@ public class Thermostat extends OutputDevice { Sensor.TYPICAL_VALUES.get(Sensor.SensorType.TEMPERATURE); /** State of this thermostat */ - @Column @NotNull private ThermostatState state; + @Column @NotNull private Thermostat.Mode mode; @Transient private BigDecimal measuredTemperature; @@ -40,15 +46,15 @@ public class Thermostat extends OutputDevice { /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { super("thermostat"); - this.state = ThermostatState.OFF; + this.mode = Mode.OFF; } - public void setState(ThermostatState state) { - this.state = state; + public void setMode(Mode state) { + this.mode = state; } - public ThermostatState getState() { - return this.state; + public Mode getMode() { + return this.mode; } public BigDecimal getTargetTemperature() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatState.java new file mode 100644 index 0000000..f80c34d --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatState.java @@ -0,0 +1,16 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import javax.persistence.Entity; + +@Entity +public class ThermostatState extends State { + private Thermostat.Mode mode; + + public Thermostat.Mode getMode() { + return mode; + } + + public void setMode(Thermostat.Mode mode) { + this.mode = mode; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java index fca88ee..fcd5378 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java @@ -43,7 +43,7 @@ public class ThermostatService { } public boolean computeState(Thermostat t) { - if (t.getState() == Thermostat.ThermostatState.OFF) { + if (t.getMode() == Thermostat.Mode.OFF) { return false; } @@ -53,14 +53,14 @@ public class ThermostatService { BigDecimal delta = target.subtract(measured); if (delta.abs().doubleValue() < 0.25) { - if (t.getState() == Thermostat.ThermostatState.IDLE) return false; - t.setState(Thermostat.ThermostatState.IDLE); + if (t.getMode() == Thermostat.Mode.IDLE) return false; + t.setMode(Thermostat.Mode.IDLE); } else if (delta.signum() > 0) { - if (t.getState() == Thermostat.ThermostatState.HEATING) return false; - t.setState(Thermostat.ThermostatState.HEATING); + if (t.getMode() == Thermostat.Mode.HEATING) return false; + t.setMode(Thermostat.Mode.HEATING); } else { - if (t.getState() == Thermostat.ThermostatState.COOLING) return false; - t.setState(Thermostat.ThermostatState.COOLING); + if (t.getMode() == Thermostat.Mode.COOLING) return false; + t.setMode(Thermostat.Mode.COOLING); } return true;