From 2f05253bc15d7115242fd158ecbfbd5ad679679d Mon Sep 17 00:00:00 2001 From: omenem Date: Mon, 25 May 2020 18:41:30 +0200 Subject: [PATCH] implemented user story 1 for sensor --- .../smarthut/controller/SensorController.java | 2 ++ .../smarthut/dto/SensorSaveRequest.java | 4 +++ .../sanmarinoes/smarthut/models/Sensor.java | 27 +++++++++++++++++++ .../smarthut/service/SensorService.java | 23 ++++++++++++---- 4 files changed, 51 insertions(+), 5 deletions(-) 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..a0b4f82 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()); } 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..5ed77c9 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; + @NotNull private BigDecimal error = new BigDecimal(1); + + 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/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index f4d444a..55bb074 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,13 @@ 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; + + @Column(nullable = true, precision = 11, scale = 1) + private BigDecimal typical; + /** The type of this sensor */ @Column(nullable = false) @Enumerated(value = EnumType.STRING) @@ -65,6 +72,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/service/SensorService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SensorService.java index 532442b..5029e21 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,23 @@ 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; + 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; + } + + updateValueFromSensor(sensor, new BigDecimal(x)); } public void sensorFakeUpdate() {