thermostat should be finished
This commit is contained in:
parent
44321fe153
commit
0e924c088b
5 changed files with 80 additions and 43 deletions
|
@ -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.dto.RoomSaveRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
@ -29,6 +30,8 @@ public class RoomController {
|
||||||
|
|
||||||
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
||||||
|
|
||||||
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Room> findAll() {
|
public List<Room> findAll() {
|
||||||
return toList(roomRepository.findAll());
|
return toList(roomRepository.findAll());
|
||||||
|
@ -99,6 +102,12 @@ public class RoomController {
|
||||||
*/
|
*/
|
||||||
@GetMapping(path = "/{roomId}/devices")
|
@GetMapping(path = "/{roomId}/devices")
|
||||||
public List<Device> getDevices(@PathVariable("roomId") long roomid) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,14 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
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.dto.ThermostatSaveRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.*;
|
import org.springframework.boot.autoconfigure.*;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
@ -31,18 +28,26 @@ public class ThermostatController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public Thermostat findById(@PathVariable("id") long id, Principal principal) throws NotFoundException {
|
public Thermostat findById(@PathVariable("id") long id, Principal principal)
|
||||||
return thermostatService.findById(id, principal.getName()).orElseThrow(NotFoundException::new);
|
throws NotFoundException {
|
||||||
|
return thermostatService
|
||||||
|
.findById(id, principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Thermostat save(Thermostat newT, ThermostatSaveRequest t) {
|
private Thermostat save(Thermostat newT, ThermostatSaveRequest t) {
|
||||||
newT.setTargetTemperature(t.getTargetTemperature());
|
|
||||||
newT.setState(t.getState());
|
|
||||||
newT.setTargetTemperature(t.getTargetTemperature());
|
newT.setTargetTemperature(t.getTargetTemperature());
|
||||||
newT.setId(t.getId());
|
newT.setId(t.getId());
|
||||||
newT.setName(t.getName());
|
newT.setName(t.getName());
|
||||||
newT.setRoomId(t.getRoomId());
|
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);
|
newT = thermostatRepository.save(newT);
|
||||||
thermostatService.populateMeasuredTemperature(newT);
|
thermostatService.populateMeasuredTemperature(newT);
|
||||||
return newT;
|
return newT;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
@ -33,8 +32,7 @@ public class Thermostat extends OutputDevice {
|
||||||
/** State of this thermostat */
|
/** State of this thermostat */
|
||||||
@Column @NotNull private ThermostatState state;
|
@Column @NotNull private ThermostatState state;
|
||||||
|
|
||||||
@Transient
|
@Transient private BigDecimal measuredTemperature;
|
||||||
private BigDecimal measuredTemperature;
|
|
||||||
|
|
||||||
@Column boolean useExternalSensors = false;
|
@Column boolean useExternalSensors = false;
|
||||||
|
|
||||||
|
@ -64,6 +62,10 @@ public class Thermostat extends OutputDevice {
|
||||||
return useExternalSensors;
|
return useExternalSensors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMeasuredTemperature() {
|
||||||
|
return measuredTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
public void setMeasuredTemperature(BigDecimal measuredTemperature) {
|
public void setMeasuredTemperature(BigDecimal measuredTemperature) {
|
||||||
this.measuredTemperature = measuredTemperature;
|
this.measuredTemperature = measuredTemperature;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.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.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.socket.SensorSocketEndpoint;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class SensorService {
|
public class SensorService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired private SensorRepository sensorRepository;
|
||||||
private SensorRepository sensorRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private ThermostatService thermostatService;
|
||||||
private ThermostatRepository thermostatRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
private SensorSocketEndpoint endpoint;
|
|
||||||
|
|
||||||
private void randomJitter(Sensor sensor) {
|
private void randomJitter(Sensor sensor) {
|
||||||
updateValueFromSensor(
|
updateValueFromSensor(
|
||||||
sensor,
|
sensor,
|
||||||
Sensor.TYPICAL_VALUES
|
Sensor.TYPICAL_VALUES
|
||||||
.get(sensor.getSensor())
|
.get(sensor.getSensor())
|
||||||
.multiply(
|
.multiply(BigDecimal.valueOf(0.975 + Math.random() / 20)));
|
||||||
BigDecimal.valueOf(0.9875 + Math.random() / 40)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sensorFakeUpdate() {
|
public void sensorFakeUpdate() {
|
||||||
sensorRepository.findAllBySensor(Sensor.SensorType.HUMIDITY).forEach(this::randomJitter);
|
sensorRepository.findAll().forEach(this::randomJitter);
|
||||||
sensorRepository.findAllBySensor(Sensor.SensorType.LIGHT).forEach(this::randomJitter);
|
thermostatService.updateStates();
|
||||||
temperatureSensorFakeUpdate(sensorRepository.findAllBySensor(Sensor.SensorType.HUMIDITY));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void temperatureSensorFakeUpdate(List<Sensor> temperatureSensors) {
|
|
||||||
for (Sensor temperature : temperatureSensors) {
|
|
||||||
//Thermostat t = thermostatRepository.findByRoomId()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,20 +1,19 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
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.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.utils.Utils;
|
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.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ThermostatService {
|
public class ThermostatService {
|
||||||
|
|
||||||
@Autowired private SensorRepository sensorRepository;
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
|
|
||||||
@Autowired private ThermostatRepository thermostatRepository;
|
@Autowired private ThermostatRepository thermostatRepository;
|
||||||
|
|
||||||
|
@ -24,6 +23,44 @@ public class ThermostatService {
|
||||||
return Utils.toList(all);
|
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) {
|
public Optional<Thermostat> findById(Long thermostat, String username) {
|
||||||
Optional<Thermostat> t = thermostatRepository.findByIdAndUsername(thermostat, username);
|
Optional<Thermostat> t = thermostatRepository.findByIdAndUsername(thermostat, username);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue