diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index 62581b3..878bfe3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -44,6 +44,8 @@ public class SensorController { newSensor.setName(s.getName()); newSensor.setRoomId(s.getRoomId()); newSensor.setValue(s.getValue()); + // newSensor.setError(s.getError()); + // newSensor.setTypical(s.getTypical()); return deviceService.saveAsOwner(newSensor, principal.getName()); } @@ -61,6 +63,21 @@ public class SensorController { value); } + @PutMapping("/{id}/simulation") + public Sensor updateSimulation( + @PathVariable("id") Long sensorId, + @RequestParam("error") BigDecimal error, + @RequestParam("typical") BigDecimal typical, + final Principal principal) + throws NotFoundException { + return sensorService.updateSimulationFromSensor( + sensorRepository + .findByIdAndUsername(sensorId, principal.getName()) + .orElseThrow(NotFoundException::new), + error, + typical); + } + @DeleteMapping("/{id}") public void deleteById(@PathVariable("id") long id, final Principal principal) throws NotFoundException { 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..09b21cf 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,12 @@ public class ThermostatController { newT.setRoomId(t.getRoomId()); newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setOn(false); + if (t.getErr() != null) { + newT.setErr(t.getErr()); + } + if (t.getTypical() != null) { + newT.setTypical(t.getTypical()); + } thermostatService.populateMeasuredTemperature(newT); newT = thermostatRepository.save(newT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 59a089a..f5558c7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -20,6 +20,10 @@ public class SensorSaveRequest { @NotNull private BigDecimal value; + private BigDecimal error; + + private BigDecimal typical; + /** * The room this device belongs in, as a foreign key id. To use when updating and inserting from * a REST call. 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..3734613 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 */ + private BigDecimal err; + + private BigDecimal typical; } 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 f4d444a..72f863a 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 @@ -44,6 +44,12 @@ public class Sensor extends InputDevice implements RangeTriggerable { @Column(nullable = false, precision = 11, scale = 1) private BigDecimal value; + /** The value of the error according to this value */ + @Column(nullable = false, precision = 11, scale = 1) + private BigDecimal err = new BigDecimal(1); + + @Column(nullable = false, precision = 11, scale = 1) + private BigDecimal typical = new BigDecimal(17); /** The type of this sensor */ @Column(nullable = false) @Enumerated(value = EnumType.STRING) @@ -65,6 +71,26 @@ public class Sensor extends InputDevice implements RangeTriggerable { this.value = newValue; } + public BigDecimal getError() { + return err; + } + + public void setError(BigDecimal err) { + this.err = err; + } + + public BigDecimal getTypical() { + return typical; + } + + public void setTypical(BigDecimal typical) { + this.typical = typical; + } + + public void setTypicalNull() { + this.typical = null; + } + public Sensor() { super("sensor"); } 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..4e69b01 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 = new BigDecimal(1); + + @Column(nullable = false, precision = 11, scale = 1) + private BigDecimal typical = new BigDecimal(17.0); + /** 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/SensorService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java index 532442b..f2d8408 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java @@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; import java.math.BigDecimal; +import java.util.Random; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -19,11 +20,13 @@ public class SensorService { @Autowired private SensorSocketEndpoint endpoint; private void randomJitter(Sensor sensor) { - updateValueFromSensor( - sensor, - Sensor.TYPICAL_VALUES - .get(sensor.getSensor()) - .multiply(BigDecimal.valueOf(0.975 + Math.random() / 20))); + + double x; + Random ran = new Random(); + x = + (ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) * 0.975 + + 1; + updateValueFromSensor(sensor, new BigDecimal(x)); } public void sensorFakeUpdate() { @@ -35,11 +38,9 @@ public class SensorService { * Updates the sensor with new measurement and propagates update through websocket * * @param sensor the sensor to update - * @param value the new measurement * @return the updated sensor */ - public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) { - sensor.setValue(value); + public Sensor update(Sensor sensor) { sensor = deviceService.saveAsOwner( sensor, sensorRepository.findUser(sensor.getId()).getUsername()); @@ -47,4 +48,20 @@ public class SensorService { sensor, sensorRepository.findUser(sensor.getId()), false, null, false); return sensor; } + + public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) { + sensor.setValue(value); + return update(sensor); + } + + public Sensor updateSimulationFromSensor(Sensor sensor, BigDecimal error, BigDecimal typical) { + if (error != null) { + sensor.setError(error); + } + + if (typical != null) { + sensor.setTypical(typical); + } + return update(sensor); + } } 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..20d7e32 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,5 @@ 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.Thermostat; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; @@ -8,6 +7,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 +35,14 @@ 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(); + x = + (ran.nextInt(thermostat.getTypical().intValue()) + thermostat.getErr().intValue()) + * 0.975 + + 1; + updateValueForThermostat(thermostat, new BigDecimal(x)); } private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) { diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java index bac53e0..484564f 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorControllerTests.java @@ -69,7 +69,12 @@ public class SensorControllerTests { final SensorSaveRequest toSend = new SensorSaveRequest( - Sensor.SensorType.TEMPERATURE, BigDecimal.ZERO, 42L, "Test sensor"); + Sensor.SensorType.TEMPERATURE, + BigDecimal.ZERO, + BigDecimal.ZERO, + BigDecimal.ZERO, + 42L, + "Test sensor"); final Sensor created = sensorController.create(toSend, mockPrincipal); checkSensorAgainstRequest(created, toSend); diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequestTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequestTests.java index 6c99297..2ca98e8 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequestTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequestTests.java @@ -30,7 +30,13 @@ public class SensorSaveRequestTests { @DisplayName("test constructor") public void testConstructorNotEmpty() { SensorSaveRequest newSaveRequest = - new SensorSaveRequest(Sensor.SensorType.HUMIDITY, new BigDecimal(12), 12L, "name"); + new SensorSaveRequest( + Sensor.SensorType.HUMIDITY, + new BigDecimal(12), + BigDecimal.ZERO, + BigDecimal.ZERO, + 12L, + "name"); assertNotNull(newSaveRequest.getSensor()); assertNotNull(newSaveRequest.getName()); assertNotNull(newSaveRequest.getRoomId());