almost finished thermostat
This commit is contained in:
parent
7bda571a27
commit
0b1170404d
3 changed files with 195 additions and 3 deletions
|
@ -0,0 +1,64 @@
|
||||||
|
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 java.security.Principal;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.*;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RequestMapping("/thermostat")
|
||||||
|
public class ThermostatController {
|
||||||
|
|
||||||
|
@Autowired private ThermostatRepository thermostatRepository;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<Thermostat> findAll() {
|
||||||
|
return toList(thermostatRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Thermostat findById(@PathVariable("id") long id) throws NotFoundException {
|
||||||
|
return thermostatRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Thermostat save(Thermostat newT, ThermostatSaveRequest t) {
|
||||||
|
newT.setTargetTemperature(t.getTargetTemperature());
|
||||||
|
newT.setState(t.getState());
|
||||||
|
newT.setAverageTemperature(t.getAverageTemperature());
|
||||||
|
newT.setTargetTemperature(t.getTargetTemperature());
|
||||||
|
newT.setId(t.getId());
|
||||||
|
newT.setName(t.getName());
|
||||||
|
newT.setRoomId(t.getRoomId());
|
||||||
|
|
||||||
|
return thermostatRepository.save(newT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public Thermostat create(@Valid @RequestBody ThermostatSaveRequest t) {
|
||||||
|
return save(new Thermostat(), t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public Thermostat update(@Valid @RequestBody ThermostatSaveRequest t, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
return save(
|
||||||
|
thermostatRepository
|
||||||
|
.findByIdAndUsername(t.getId(), principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new),
|
||||||
|
t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void deleteById(@PathVariable("id") long id) {
|
||||||
|
thermostatRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,96 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class ThermostatSaveRequest {
|
||||||
|
|
||||||
|
public enum ThermostatState {
|
||||||
|
OFF,
|
||||||
|
IDLE,
|
||||||
|
COOLING,
|
||||||
|
HEATING
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Device identifier */
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||||
|
* a REST call.
|
||||||
|
*/
|
||||||
|
@NotNull private Long roomId;
|
||||||
|
|
||||||
|
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
|
||||||
|
@NotNull private String name;
|
||||||
|
|
||||||
|
/** Temperature to be reached */
|
||||||
|
@NotNull private BigDecimal targetTemperature;
|
||||||
|
|
||||||
|
/** Average room temperature given by all the temperature sensors in the room */
|
||||||
|
@NotNull BigDecimal averageTemperature;
|
||||||
|
|
||||||
|
/** Embedded temperature sensor */
|
||||||
|
@NotNull private Sensor temperatureSensor;
|
||||||
|
|
||||||
|
/** State of this thermostat */
|
||||||
|
@NotNull private Thermostat.ThermostatState state;
|
||||||
|
|
||||||
|
public void setState(Thermostat.ThermostatState state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Thermostat.ThermostatState getState() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAverageTemperature(BigDecimal averageTemperature) {
|
||||||
|
this.averageTemperature = averageTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getAverageTemperature() {
|
||||||
|
return this.averageTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sensor getTemperatureSensor() {
|
||||||
|
return this.temperatureSensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemperatureSensor(Sensor temperatureSensor) {
|
||||||
|
this.temperatureSensor = temperatureSensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getTargetTemperature() {
|
||||||
|
return this.targetTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTargetTemperature(BigDecimal targetTemperature) {
|
||||||
|
this.targetTemperature = targetTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRoomId(Long roomId) {
|
||||||
|
this.roomId = roomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getRoomId() {
|
||||||
|
return roomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,19 +10,23 @@ import javax.validation.constraints.NotNull;
|
||||||
@Entity
|
@Entity
|
||||||
public class Thermostat extends InputDevice {
|
public class Thermostat extends InputDevice {
|
||||||
|
|
||||||
enum ThermostatState {
|
public enum ThermostatState {
|
||||||
OFF,
|
OFF,
|
||||||
IDLE,
|
IDLE,
|
||||||
COOLING,
|
COOLING,
|
||||||
HEATING
|
HEATING
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Temperature to be reached */
|
||||||
@Column @NotNull private BigDecimal targetTemperature;
|
@Column @NotNull private BigDecimal targetTemperature;
|
||||||
|
|
||||||
|
/** Average room temperature given by all the temperature sensors in the room */
|
||||||
@Column @NotNull BigDecimal averageTemperature;
|
@Column @NotNull BigDecimal averageTemperature;
|
||||||
|
|
||||||
|
/** Embedded temperature sensor */
|
||||||
@Column @NotNull @OneToOne private final Sensor temperatureSensor;
|
@Column @NotNull @OneToOne private final Sensor temperatureSensor;
|
||||||
|
|
||||||
|
/** State of this thermostat */
|
||||||
@Column @NotNull private ThermostatState state;
|
@Column @NotNull private ThermostatState state;
|
||||||
|
|
||||||
/** Creates a thermostat with a temperature sensor and its initial OFF state */
|
/** Creates a thermostat with a temperature sensor and its initial OFF state */
|
||||||
|
@ -33,11 +37,39 @@ public class Thermostat extends InputDevice {
|
||||||
this.state = ThermostatState.OFF;
|
this.state = ThermostatState.OFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Setter method for the thermostat state */
|
|
||||||
public void setState(ThermostatState state) {
|
public void setState(ThermostatState state) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ThermostatState getState() {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAverageTemperature(BigDecimal averageTemperature) {
|
||||||
|
this.averageTemperature = averageTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getAverageTemperature() {
|
||||||
|
return this.averageTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sensor getTemperatureSensor() {
|
||||||
|
return this.temperatureSensor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getTargetTemperature() {
|
||||||
|
return this.targetTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Takes all of the temperature sensors in the room inculding its own embedded sensor and set
|
||||||
|
* this average temperature to the average of all the temperatures registered.
|
||||||
|
*/
|
||||||
|
public void calcAverageTemperature() {
|
||||||
|
// TODO: write actual method body
|
||||||
|
BigDecimal average = new BigDecimal(0);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the target temperature to be reached. Changes the thermostat state accordingly and waits
|
* Sets the target temperature to be reached. Changes the thermostat state accordingly and waits
|
||||||
* until such temperature is reached by the embedded sensor to become idle again.
|
* until such temperature is reached by the embedded sensor to become idle again.
|
||||||
|
@ -46,7 +78,7 @@ public class Thermostat extends InputDevice {
|
||||||
*/
|
*/
|
||||||
public void setTargetTemperature(BigDecimal targetTemperature) {
|
public void setTargetTemperature(BigDecimal targetTemperature) {
|
||||||
if (this.state == ThermostatState.OFF) {
|
if (this.state == ThermostatState.OFF) {
|
||||||
this.state = ThermostatState.IDLE;
|
this.setState(ThermostatState.IDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.targetTemperature = targetTemperature;
|
this.targetTemperature = targetTemperature;
|
||||||
|
|
Loading…
Reference in a new issue