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

View File

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

View File

@ -20,6 +20,10 @@ public class SensorSaveRequest {
@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
* a REST call.

View File

@ -26,4 +26,9 @@ public class ThermostatSaveRequest {
/** State of this thermostat */
@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)
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 */
@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
@ -65,6 +71,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

@ -79,6 +79,13 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
@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 */
public Thermostat() {
super("thermostat");
@ -97,6 +104,22 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
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) {
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.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,13 @@ 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;
Random ran = new Random();
x =
(ran.nextInt(sensor.getTypical().intValue()) + sensor.getError().intValue()) * 0.975
+ 1;
updateValueFromSensor(sensor, new BigDecimal(x));
}
public void sensorFakeUpdate() {
@ -35,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());
@ -47,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);
}
}

View File

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

View File

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

View File

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