implemented user story 1 for sensor

This commit is contained in:
omenem 2020-05-25 18:41:30 +02:00
parent 691130ada7
commit 2f05253bc1
4 changed files with 51 additions and 5 deletions

View file

@ -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());
}

View file

@ -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.

View file

@ -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");
}

View file

@ -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() {