Converted thermostat to dimmable state and fixed errors on generics

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-17 19:09:32 +02:00
parent 3cedaf5f50
commit fefd073534
10 changed files with 53 additions and 123 deletions

View File

@ -4,11 +4,8 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.List;
import javax.validation.Valid;
@ -31,7 +28,7 @@ public class SceneController {
@Autowired SceneRepository sceneService;
@Autowired UserRepository userService;
@Autowired StateRepository stateService;
@Autowired StateRepository<State<?>> stateService;
@GetMapping
public List<Scene> findAll() {
@ -85,8 +82,8 @@ public class SceneController {
* id).
*/
@GetMapping(path = "/{sceneId}/devices")
public List<Device> getDevices(@PathVariable("sceneId") long sceneId) {
Iterable<Device> states = stateService.findBySceneId(sceneId);
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
Iterable<State<?>> states = stateService.findBySceneId(sceneId);
return toList(states);
}
}

View File

@ -27,16 +27,16 @@ public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateService;
@GetMapping
public List<SwitchableState> findAll() {
public List<SwitchableState<?>> findAll() {
return toList(switchableStateService.findAll());
}
@GetMapping("/{id}")
public SwitchableState findById(@PathVariable("id") long id) throws NotFoundException {
public SwitchableState<?> findById(@PathVariable("id") long id) throws NotFoundException {
return switchableStateService.findById(id).orElseThrow(NotFoundException::new);
}
private SwitchableState save(SwitchableState initial, SwitchableStateSaveRequest ss) {
private SwitchableState<?> save(SwitchableState<?> initial, SwitchableStateSaveRequest ss) {
initial.setDeviceId(ss.getDeviceId());
initial.setSceneId(ss.getSceneId());
initial.setOn(ss.isOn());
@ -45,12 +45,12 @@ public class SwitchableStateController {
}
@PostMapping
public SwitchableState create(@Valid @RequestBody SwitchableStateSaveRequest dl) {
return save(new SwitchableState(), dl);
public SwitchableState<?> create(@Valid @RequestBody SwitchableStateSaveRequest dl) {
return save(new SwitchableState<>(), dl);
}
@PutMapping
public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss)
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException {
return save(
switchableStateService.findById(ss.getId()).orElseThrow(NotFoundException::new),

View File

@ -1,64 +0,0 @@
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.ThermostatStateSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatStateRepository;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/thermostatState")
public class ThermostatStateController {
@Autowired private ThermostatStateRepository thermostatStateService;
@GetMapping
public List<ThermostatState> findAll() {
return toList(thermostatStateService.findAll());
}
@GetMapping("/{id}")
public ThermostatState findById(@PathVariable("id") long id) throws NotFoundException {
return thermostatStateService.findById(id).orElseThrow(NotFoundException::new);
}
private ThermostatState save(ThermostatState initial, ThermostatStateSaveRequest ts) {
initial.setDeviceId(ts.getDeviceId());
initial.setSceneId(ts.getSceneId());
initial.setMode(ts.getMode());
return thermostatStateService.save(initial);
}
@PostMapping
public ThermostatState create(@Valid @RequestBody ThermostatStateSaveRequest dl) {
return save(new ThermostatState(), dl);
}
@PutMapping
public ThermostatState update(@Valid @RequestBody ThermostatStateSaveRequest ts)
throws NotFoundException {
return save(
thermostatStateService.findById(ts.getId()).orElseThrow(NotFoundException::new),
ts);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
thermostatStateService.deleteById(id);
}
}

View File

@ -0,0 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface DimmableStateRepository extends StateRepository<DimmableState<?>> {}

View File

@ -5,7 +5,7 @@ import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface StateRepository<T extends State> extends CrudRepository<T, Long> {
public interface StateRepository<T extends State<?>> extends CrudRepository<T, Long> {
@Transactional
void deleteAllBySceneId(long roomId);

View File

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchableStateRepository extends StateRepository<SwitchableState> {}
public interface SwitchableStateRepository extends StateRepository<SwitchableState<?>> {}

View File

@ -9,12 +9,44 @@ import javax.validation.constraints.NotNull;
/** A thermostat capable of controlling cooling and heating. */
@Entity
public class Thermostat extends OutputDevice implements AlterableFromState<State<Thermostat>> {
public class Thermostat extends Switchable implements AlterableFromSwitchableState<Thermostat> {
@Override
public void readStateAndSet(State<Thermostat> state) {
final ThermostatState hack = (ThermostatState) state;
setMode(hack.getMode());
public boolean isOn() {
return mode != Mode.OFF;
}
@Override
public void setOn(boolean on) {
mode = on ? Mode.IDLE : Mode.OFF;
computeState();
}
/**
* Computes the new thermostat state, for when the thermostat is on;
* @return true if the state changed, false if not;
*/
public boolean computeState() {
if (mode == Thermostat.Mode.OFF) {
return false;
}
BigDecimal measured = this.getMeasuredTemperature();
BigDecimal target = this.getTargetTemperature();
BigDecimal delta = target.subtract(measured);
if (delta.abs().doubleValue() < 0.25) {
if (this.getMode() == Thermostat.Mode.IDLE) return false;
this.setMode(Thermostat.Mode.IDLE);
} else if (delta.signum() > 0) {
if (this.getMode() == Thermostat.Mode.HEATING) return false;
this.setMode(Thermostat.Mode.HEATING);
} else {
if (this.getMode() == Thermostat.Mode.COOLING) return false;
this.setMode(Thermostat.Mode.COOLING);
}
return true;
}
public enum Mode {

View File

@ -1,16 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
@Entity
public class ThermostatState extends State<Thermostat> {
private Thermostat.Mode mode;
public Thermostat.Mode getMode() {
return mode;
}
public void setMode(Thermostat.Mode mode) {
this.mode = mode;
}
}

View File

@ -1,3 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface ThermostatStateRepository extends StateRepository<ThermostatState> {}

View File

@ -43,27 +43,8 @@ public class ThermostatService {
}
public boolean computeState(Thermostat t) {
if (t.getMode() == Thermostat.Mode.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.getMode() == Thermostat.Mode.IDLE) return false;
t.setMode(Thermostat.Mode.IDLE);
} else if (delta.signum() > 0) {
if (t.getMode() == Thermostat.Mode.HEATING) return false;
t.setMode(Thermostat.Mode.HEATING);
} else {
if (t.getMode() == Thermostat.Mode.COOLING) return false;
t.setMode(Thermostat.Mode.COOLING);
}
return true;
return t.computeState();
}
private void updateState(Thermostat t) {