thermostat should be finished

This commit is contained in:
Tommaso Rodolfo Masera 2020-04-15 15:10:02 +02:00
parent 44321fe153
commit 0e924c088b
5 changed files with 80 additions and 43 deletions

View file

@ -5,6 +5,7 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
import java.security.Principal;
import java.util.*;
import javax.validation.Valid;
@ -29,6 +30,8 @@ public class RoomController {
@Autowired private KnobDimmerRepository knobDimmerRepository;
@Autowired private ThermostatService thermostatService;
@GetMapping
public List<Room> findAll() {
return toList(roomRepository.findAll());
@ -99,6 +102,12 @@ public class RoomController {
*/
@GetMapping(path = "/{roomId}/devices")
public List<Device> getDevices(@PathVariable("roomId") long roomid) {
return deviceRepository.findByRoomId(roomid);
Iterable<Device> devices = deviceRepository.findByRoomId(roomid);
for (Device d : devices) {
if (d instanceof Thermostat) {
thermostatService.populateMeasuredTemperature((Thermostat) d);
}
}
return toList(devices);
}
}

View file

@ -1,17 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
import java.security.Principal;
import java.util.*;
import java.util.List;
import java.util.stream.StreamSupport;
import javax.validation.Valid;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@ -31,18 +28,26 @@ public class ThermostatController {
}
@GetMapping("/{id}")
public Thermostat findById(@PathVariable("id") long id, Principal principal) throws NotFoundException {
return thermostatService.findById(id, principal.getName()).orElseThrow(NotFoundException::new);
public Thermostat findById(@PathVariable("id") long id, Principal principal)
throws NotFoundException {
return thermostatService
.findById(id, principal.getName())
.orElseThrow(NotFoundException::new);
}
private Thermostat save(Thermostat newT, ThermostatSaveRequest t) {
newT.setTargetTemperature(t.getTargetTemperature());
newT.setState(t.getState());
newT.setTargetTemperature(t.getTargetTemperature());
newT.setId(t.getId());
newT.setName(t.getName());
newT.setRoomId(t.getRoomId());
if (newT.getState() == Thermostat.ThermostatState.OFF
&& t.getState() != Thermostat.ThermostatState.OFF) {
thermostatService.computeState(newT);
} else {
newT.setState(t.getState());
}
newT = thermostatRepository.save(newT);
thermostatService.populateMeasuredTemperature(newT);
return newT;

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -33,8 +32,7 @@ public class Thermostat extends OutputDevice {
/** State of this thermostat */
@Column @NotNull private ThermostatState state;
@Transient
private BigDecimal measuredTemperature;
@Transient private BigDecimal measuredTemperature;
@Column boolean useExternalSensors = false;
@ -64,6 +62,10 @@ public class Thermostat extends OutputDevice {
return useExternalSensors;
}
public BigDecimal getMeasuredTemperature() {
return measuredTemperature;
}
public void setMeasuredTemperature(BigDecimal measuredTemperature) {
this.measuredTemperature = measuredTemperature;
}

View file

@ -2,47 +2,31 @@ 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.SensorRepository;
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;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.StreamSupport;
@Component
public class SensorService {
@Autowired
private SensorRepository sensorRepository;
@Autowired private SensorRepository sensorRepository;
@Autowired
private ThermostatRepository thermostatRepository;
@Autowired private ThermostatService thermostatService;
@Autowired
private SensorSocketEndpoint endpoint;
@Autowired private SensorSocketEndpoint endpoint;
private void randomJitter(Sensor sensor) {
updateValueFromSensor(
sensor,
Sensor.TYPICAL_VALUES
.get(sensor.getSensor())
.multiply(
BigDecimal.valueOf(0.9875 + Math.random() / 40)));
.multiply(BigDecimal.valueOf(0.975 + Math.random() / 20)));
}
public void sensorFakeUpdate() {
sensorRepository.findAllBySensor(Sensor.SensorType.HUMIDITY).forEach(this::randomJitter);
sensorRepository.findAllBySensor(Sensor.SensorType.LIGHT).forEach(this::randomJitter);
temperatureSensorFakeUpdate(sensorRepository.findAllBySensor(Sensor.SensorType.HUMIDITY));
}
public void temperatureSensorFakeUpdate(List<Sensor> temperatureSensors) {
for (Sensor temperature : temperatureSensors) {
//Thermostat t = thermostatRepository.findByRoomId()
}
sensorRepository.findAll().forEach(this::randomJitter);
thermostatService.updateStates();
}
/**

View file

@ -1,20 +1,19 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository;
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;
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ThermostatService {
@Autowired private SensorRepository sensorRepository;
@Autowired private SensorSocketEndpoint endpoint;
@Autowired private ThermostatRepository thermostatRepository;
@ -24,6 +23,44 @@ public class ThermostatService {
return Utils.toList(all);
}
public boolean computeState(Thermostat t) {
if (t.getState() == Thermostat.ThermostatState.OFF) {
return false;
}
populateMeasuredTemperature(t);
BigDecimal measured = t.getMeasuredTemperature();
BigDecimal target = t.getTargetTemperature();
BigDecimal delta = target.subtract(measured);
if (delta.abs().doubleValue() < 0.25) {
if (t.getState() == Thermostat.ThermostatState.IDLE) return false;
t.setState(Thermostat.ThermostatState.IDLE);
} else if (delta.signum() > 0) {
if (t.getState() == Thermostat.ThermostatState.HEATING) return false;
t.setState(Thermostat.ThermostatState.HEATING);
} else {
if (t.getState() == Thermostat.ThermostatState.COOLING) return false;
t.setState(Thermostat.ThermostatState.COOLING);
}
return true;
}
private void updateState(Thermostat t) {
boolean shouldUpdate = this.computeState(t);
if (shouldUpdate) {
thermostatRepository.save(t);
endpoint.queueDeviceUpdate(t, thermostatRepository.findUser(t.getId()));
}
}
public void updateStates() {
Iterable<Thermostat> ts = thermostatRepository.findAll();
ts.forEach(this::updateState);
}
public Optional<Thermostat> findById(Long thermostat, String username) {
Optional<Thermostat> t = thermostatRepository.findByIdAndUsername(thermostat, username);