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 a0b4f82..d4accc3 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,8 +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()); + // newSensor.setError(s.getError()); + // newSensor.setTypical(s.getTypical()); return deviceService.saveAsOwner(newSensor, principal.getName()); } @@ -63,6 +63,21 @@ public class SensorController { value); } + @PutMapping("/{id}/simulation") + public Sensor updateSimulation( + @PathVariable("id") Long sensorId, + @RequestParam("value") BigDecimal error, + @RequestParam("value") 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 5884a05..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,8 +30,12 @@ public class ThermostatController { newT.setRoomId(t.getRoomId()); newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setOn(false); - newT.setErr(t.getErr()); - newT.setTypical(newT.getTypical()); + 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 5ed77c9..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,7 +20,7 @@ public class SensorSaveRequest { @NotNull private BigDecimal value; - @NotNull private BigDecimal error = new BigDecimal(1); + private BigDecimal error; private BigDecimal typical; 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 ae1f87a..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 @@ -28,7 +28,7 @@ public class ThermostatSaveRequest { @NotNull private boolean turnOn; /** The value of the error according to this value */ - @NotNull private BigDecimal err = new BigDecimal(1); + 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 55bb074..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 @@ -46,11 +46,10 @@ public class Sensor extends InputDevice implements RangeTriggerable { /** 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; + 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) 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 83ac603..8dcd657 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 @@ -81,10 +81,10 @@ public class Thermostat extends Switchable implements BooleanTriggerable { /** The value of the error according to this value */ @Column(nullable = false, precision = 11, scale = 1) - private BigDecimal err; + private BigDecimal err = new BigDecimal(1); @Column(nullable = true, precision = 11, scale = 1) - private BigDecimal typical; + private BigDecimal typical = new BigDecimal(17.0);; /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { 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 5029e21..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 @@ -22,20 +22,10 @@ public class SensorService { private void randomJitter(Sensor sensor) { double x; - if (sensor.getTypical() == null) { - BigDecimal typical = Sensor.TYPICAL_VALUES.get(sensor.getSensor()); - - Random ran = new Random(); - x = (ran.nextInt(typical.intValue()) + sensor.getError().intValue()) * 0.975 + 1; - - } else { - Random ran = new Random(); - x = - (ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) - * 0.975 - + 1; - } - + Random ran = new Random(); + x = + (ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) * 0.975 + + 1; updateValueFromSensor(sensor, new BigDecimal(x)); } @@ -48,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()); @@ -60,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); + } }