This commit is contained in:
omenem 2020-05-26 12:05:42 +02:00
parent 689e3b96a7
commit 0046143d82
7 changed files with 51 additions and 29 deletions

View file

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

View file

@ -30,8 +30,12 @@ public class ThermostatController {
newT.setRoomId(t.getRoomId());
newT.setUseExternalSensors(t.isUseExternalSensors());
newT.setOn(false);
if (t.getErr() != null) {
newT.setErr(t.getErr());
newT.setTypical(newT.getTypical());
}
if (t.getTypical() != null) {
newT.setTypical(t.getTypical());
}
thermostatService.populateMeasuredTemperature(newT);
newT = thermostatRepository.save(newT);

View file

@ -20,7 +20,7 @@ public class SensorSaveRequest {
@NotNull private BigDecimal value;
@NotNull private BigDecimal error = new BigDecimal(1);
private BigDecimal error;
private BigDecimal typical;

View file

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

View file

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

View file

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

View file

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