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 d45ba96..5884a05 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 @@ -30,6 +30,8 @@ public class ThermostatController { newT.setRoomId(t.getRoomId()); newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setOn(false); + newT.setErr(t.getErr()); + newT.setTypical(newT.getTypical()); thermostatService.populateMeasuredTemperature(newT); newT = thermostatRepository.save(newT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java index 6bbfbb8..ae1f87a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java @@ -26,4 +26,9 @@ public class ThermostatSaveRequest { /** State of this thermostat */ @NotNull private boolean turnOn; + + /** The value of the error according to this value */ + @NotNull private BigDecimal err = new BigDecimal(1); + + private BigDecimal typical; } 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 229f7f6..83ac603 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 @@ -79,6 +79,13 @@ public class Thermostat extends Switchable implements BooleanTriggerable { @Column private boolean useExternalSensors = false; + /** The value of the error according to this value */ + @Column(nullable = false, precision = 11, scale = 1) + private BigDecimal err; + + @Column(nullable = true, precision = 11, scale = 1) + private BigDecimal typical; + /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { super("thermostat"); @@ -97,6 +104,22 @@ public class Thermostat extends Switchable implements BooleanTriggerable { return sb.toString(); } + public BigDecimal getErr() { + return err; + } + + public void setErr(BigDecimal err) { + this.err = err; + } + + public BigDecimal getTypical() { + return typical; + } + + public void setTypical(BigDecimal typical) { + this.typical = typical; + } + public void setMode(Mode state) { this.mode = state; } 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 9065826..54ae868 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 @@ -1,6 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor.SensorType; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; @@ -8,6 +9,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils; import java.math.BigDecimal; import java.util.List; import java.util.Optional; +import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -35,11 +37,23 @@ public class ThermostatService { } private void randomJitter(Thermostat thermostat) { - updateValueForThermostat( - thermostat, - Sensor.TYPICAL_VALUES - .get(Sensor.SensorType.TEMPERATURE) - .multiply(BigDecimal.valueOf(0.975 + Math.random() / 20))); + + double x; + Random ran = new Random(); + ; + if (thermostat.getTypical() == null) { + BigDecimal typical = Sensor.TYPICAL_VALUES.get(SensorType.TEMPERATURE); + + x = (ran.nextInt(typical.intValue()) + thermostat.getErr().intValue()) * 0.975 + 1; + + } else { + x = + (ran.nextInt(thermostat.getTypical().intValue()) + + thermostat.getErr().intValue()) + * 0.975 + + 1; + } + updateValueForThermostat(thermostat, new BigDecimal(x)); } private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) {