Merge branch '72-batch-3-implement-simulation-panel-routes-through-sensors' into 'dev'

Resolve "Batch 3: Implement simulation panel routes through sensors"

Closes #72

See merge request sa4-2020/the-sanmarinoes/backend!190
This commit is contained in:
Matteo Omenetti 2020-05-26 13:00:13 +02:00
commit e006ff83d2
10 changed files with 128 additions and 16 deletions

View file

@ -44,6 +44,8 @@ public class SensorController {
newSensor.setName(s.getName()); newSensor.setName(s.getName());
newSensor.setRoomId(s.getRoomId()); newSensor.setRoomId(s.getRoomId());
newSensor.setValue(s.getValue()); newSensor.setValue(s.getValue());
// newSensor.setError(s.getError());
// newSensor.setTypical(s.getTypical());
return deviceService.saveAsOwner(newSensor, principal.getName()); return deviceService.saveAsOwner(newSensor, principal.getName());
} }
@ -61,6 +63,21 @@ public class SensorController {
value); 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}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id, final Principal principal) public void deleteById(@PathVariable("id") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {

View file

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

View file

@ -20,6 +20,10 @@ public class SensorSaveRequest {
@NotNull private BigDecimal value; @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 * The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call. * a REST call.

View file

@ -26,4 +26,9 @@ public class ThermostatSaveRequest {
/** State of this thermostat */ /** State of this thermostat */
@NotNull private boolean turnOn; @NotNull private boolean turnOn;
/** The value of the error according to this value */
private BigDecimal err;
private BigDecimal typical;
} }

View file

@ -44,6 +44,12 @@ public class Sensor extends InputDevice implements RangeTriggerable {
@Column(nullable = false, precision = 11, scale = 1) @Column(nullable = false, precision = 11, scale = 1)
private BigDecimal value; 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 */ /** The type of this sensor */
@Column(nullable = false) @Column(nullable = false)
@Enumerated(value = EnumType.STRING) @Enumerated(value = EnumType.STRING)
@ -65,6 +71,26 @@ public class Sensor extends InputDevice implements RangeTriggerable {
this.value = newValue; 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() { public Sensor() {
super("sensor"); super("sensor");
} }

View file

@ -79,6 +79,13 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
@Column private boolean useExternalSensors = false; @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 */ /** Creates a thermostat with a temperature sensor and its initial OFF state */
public Thermostat() { public Thermostat() {
super("thermostat"); super("thermostat");
@ -97,6 +104,22 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
return sb.toString(); 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) { public void setMode(Mode state) {
this.mode = state; this.mode = state;
} }

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.models.SensorRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -19,11 +20,13 @@ public class SensorService {
@Autowired private SensorSocketEndpoint endpoint; @Autowired private SensorSocketEndpoint endpoint;
private void randomJitter(Sensor sensor) { private void randomJitter(Sensor sensor) {
updateValueFromSensor(
sensor, double x;
Sensor.TYPICAL_VALUES Random ran = new Random();
.get(sensor.getSensor()) x =
.multiply(BigDecimal.valueOf(0.975 + Math.random() / 20))); (ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) * 0.975
+ 1;
updateValueFromSensor(sensor, new BigDecimal(x));
} }
public void sensorFakeUpdate() { public void sensorFakeUpdate() {
@ -35,11 +38,9 @@ public class SensorService {
* Updates the sensor with new measurement and propagates update through websocket * Updates the sensor with new measurement and propagates update through websocket
* *
* @param sensor the sensor to update * @param sensor the sensor to update
* @param value the new measurement
* @return the updated sensor * @return the updated sensor
*/ */
public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) { public Sensor update(Sensor sensor) {
sensor.setValue(value);
sensor = sensor =
deviceService.saveAsOwner( deviceService.saveAsOwner(
sensor, sensorRepository.findUser(sensor.getId()).getUsername()); sensor, sensorRepository.findUser(sensor.getId()).getUsername());
@ -47,4 +48,20 @@ public class SensorService {
sensor, sensorRepository.findUser(sensor.getId()), false, null, false); sensor, sensorRepository.findUser(sensor.getId()), false, null, false);
return sensor; 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);
}
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service; 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.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; 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.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -35,11 +35,14 @@ public class ThermostatService {
} }
private void randomJitter(Thermostat thermostat) { private void randomJitter(Thermostat thermostat) {
updateValueForThermostat(
thermostat, double x;
Sensor.TYPICAL_VALUES Random ran = new Random();
.get(Sensor.SensorType.TEMPERATURE) x =
.multiply(BigDecimal.valueOf(0.975 + Math.random() / 20))); (ran.nextInt(thermostat.getTypical().intValue()) + thermostat.getErr().intValue())
* 0.975
+ 1;
updateValueForThermostat(thermostat, new BigDecimal(x));
} }
private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) { private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) {

View file

@ -69,7 +69,12 @@ public class SensorControllerTests {
final SensorSaveRequest toSend = final SensorSaveRequest toSend =
new SensorSaveRequest( 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); final Sensor created = sensorController.create(toSend, mockPrincipal);
checkSensorAgainstRequest(created, toSend); checkSensorAgainstRequest(created, toSend);

View file

@ -30,7 +30,13 @@ public class SensorSaveRequestTests {
@DisplayName("test constructor") @DisplayName("test constructor")
public void testConstructorNotEmpty() { public void testConstructorNotEmpty() {
SensorSaveRequest newSaveRequest = 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.getSensor());
assertNotNull(newSaveRequest.getName()); assertNotNull(newSaveRequest.getName());
assertNotNull(newSaveRequest.getRoomId()); assertNotNull(newSaveRequest.getRoomId());