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

View file

@ -27,16 +27,16 @@ public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateService; @Autowired private SwitchableStateRepository switchableStateService;
@GetMapping @GetMapping
public List<SwitchableState> findAll() { public List<SwitchableState<?>> findAll() {
return toList(switchableStateService.findAll()); return toList(switchableStateService.findAll());
} }
@GetMapping("/{id}") @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); 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.setDeviceId(ss.getDeviceId());
initial.setSceneId(ss.getSceneId()); initial.setSceneId(ss.getSceneId());
initial.setOn(ss.isOn()); initial.setOn(ss.isOn());
@ -45,12 +45,12 @@ public class SwitchableStateController {
} }
@PostMapping @PostMapping
public SwitchableState create(@Valid @RequestBody SwitchableStateSaveRequest dl) { public SwitchableState<?> create(@Valid @RequestBody SwitchableStateSaveRequest dl) {
return save(new SwitchableState(), dl); return save(new SwitchableState<>(), dl);
} }
@PutMapping @PutMapping
public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss) public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException { throws NotFoundException {
return save( return save(
switchableStateService.findById(ss.getId()).orElseThrow(NotFoundException::new), 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.CrudRepository;
import org.springframework.data.repository.query.Param; 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 @Transactional
void deleteAllBySceneId(long roomId); void deleteAllBySceneId(long roomId);

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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. */ /** A thermostat capable of controlling cooling and heating. */
@Entity @Entity
public class Thermostat extends OutputDevice implements AlterableFromState<State<Thermostat>> { public class Thermostat extends Switchable implements AlterableFromSwitchableState<Thermostat> {
@Override @Override
public void readStateAndSet(State<Thermostat> state) { public boolean isOn() {
final ThermostatState hack = (ThermostatState) state; return mode != Mode.OFF;
setMode(hack.getMode()); }
@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 { 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) { public boolean computeState(Thermostat t) {
if (t.getMode() == Thermostat.Mode.OFF) {
return false;
}
populateMeasuredTemperature(t); populateMeasuredTemperature(t);
BigDecimal measured = t.getMeasuredTemperature(); return t.computeState();
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;
} }
private void updateState(Thermostat t) { private void updateState(Thermostat t) {