This commit is contained in:
omenem 2020-05-17 11:13:50 +02:00
commit dd1d59a095
49 changed files with 292 additions and 687 deletions

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
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.BooleanCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository;
@ -29,7 +29,7 @@ public class BooleanConditionController {
return booleanConditionRepository.findAllByAutomationId(automationId); return booleanConditionRepository.findAllByAutomationId(automationId);
} }
private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) { private BooleanCondition save(BooleanCondition newRL, BooleanConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn()); newRL.setOn(s.isOn());
@ -39,13 +39,13 @@ public class BooleanConditionController {
@PostMapping @PostMapping
public BooleanCondition create( public BooleanCondition create(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanCondition(), booleanTriggerSaveRequest); return save(new BooleanCondition(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public BooleanCondition update( public BooleanCondition update(
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(
booleanConditionRepository booleanConditionRepository

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanTriggerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionOrTriggerSaveRequest;
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.BooleanTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository;
@ -29,7 +29,7 @@ public class BooleanTriggerController {
return booleanTriggerRepository.findAllByAutomationId(automationId); return booleanTriggerRepository.findAllByAutomationId(automationId);
} }
private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { private BooleanTrigger save(BooleanTrigger newRL, BooleanConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn()); newRL.setOn(s.isOn());
@ -39,13 +39,13 @@ public class BooleanTriggerController {
@PostMapping @PostMapping
public BooleanTrigger create( public BooleanTrigger create(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanTrigger(), booleanTriggerSaveRequest); return save(new BooleanTrigger(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public BooleanTrigger update( public BooleanTrigger update(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody BooleanConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(
booleanTriggerRepository booleanTriggerRepository

View file

@ -18,7 +18,7 @@ public class CurtainsController {
@Autowired private DeviceService deviceService; @Autowired private DeviceService deviceService;
@Autowired private CurtainsRepository curtainsService; @Autowired private CurtainsRepository curtainsService;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository; @Autowired private StateRepository<State> stateRepository;
private Curtains save(Curtains newRL, DimmableSaveRequest s, final Principal principal) { private Curtains save(Curtains newRL, DimmableSaveRequest s, final Principal principal) {
newRL.setName(s.getName()); newRL.setName(s.getName());
@ -53,7 +53,7 @@ public class CurtainsController {
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -63,7 +63,7 @@ public class CurtainsController {
curtainsService curtainsService
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = c.cloneState(); State s = c.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -18,7 +18,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
private final DimmableLightRepository dimmableLightRepository; private final DimmableLightRepository dimmableLightRepository;
private final SceneRepository sceneRepository; private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository; private final StateRepository<State> stateRepository;
private final DeviceService deviceService; private final DeviceService deviceService;
@Autowired @Autowired
@ -26,7 +26,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
UserRepository userRepository, UserRepository userRepository,
DimmableLightRepository dimmableLightRepository, DimmableLightRepository dimmableLightRepository,
SceneRepository sceneRepository, SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository, StateRepository<State> stateRepository,
DeviceService deviceService) { DeviceService deviceService) {
super(userRepository, dimmableLightRepository); super(userRepository, dimmableLightRepository);
this.dimmableLightRepository = dimmableLightRepository; this.dimmableLightRepository = dimmableLightRepository;
@ -87,7 +87,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
* necessary to specify the query in the mapping * necessary to specify the query in the mapping
*/ */
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -97,7 +97,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
dimmableLightRepository dimmableLightRepository
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = d.cloneState(); State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -17,9 +17,9 @@ public class DimmableStateController {
@Autowired private DimmableStateRepository dimmableStateRepository; @Autowired private DimmableStateRepository dimmableStateRepository;
@PutMapping @PutMapping
public DimmableState<?> update(@Valid @RequestBody DimmableStateSaveRequest ss) public DimmableState update(@Valid @RequestBody DimmableStateSaveRequest ss)
throws NotFoundException { throws NotFoundException {
final DimmableState<?> initial = final DimmableState initial =
dimmableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new); dimmableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setIntensity(ss.getIntensity()); initial.setIntensity(ss.getIntensity());
dimmableStateRepository.save(initial); dimmableStateRepository.save(initial);

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionOrTriggerSaveRequest;
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.RangeCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeConditionRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeConditionRepository;
@ -29,7 +29,7 @@ public class RangeConditionController {
return rangeConditionRepository.findAllByAutomationId(automationId); return rangeConditionRepository.findAllByAutomationId(automationId);
} }
private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) { private RangeCondition save(RangeCondition newRL, RangeConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator()); newRL.setOperator(s.getOperator());
@ -40,13 +40,13 @@ public class RangeConditionController {
@PostMapping @PostMapping
public RangeCondition create( public RangeCondition create(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeCondition(), booleanTriggerSaveRequest); return save(new RangeCondition(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public RangeCondition update( public RangeCondition update(
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(
rangeConditionRepository rangeConditionRepository

View file

@ -1,6 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeTriggerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionOrTriggerSaveRequest;
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.RangeTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTriggerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTriggerRepository;
@ -29,7 +29,7 @@ public class RangeTriggerController {
return rangeTriggerRepository.findAllByAutomationId(automationId); return rangeTriggerRepository.findAllByAutomationId(automationId);
} }
private RangeTrigger save(RangeTrigger newRL, RangeTriggerSaveRequest s) { private RangeTrigger save(RangeTrigger newRL, RangeConditionOrTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator()); newRL.setOperator(s.getOperator());
@ -40,13 +40,13 @@ public class RangeTriggerController {
@PostMapping @PostMapping
public RangeTrigger create( public RangeTrigger create(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeTrigger(), booleanTriggerSaveRequest); return save(new RangeTrigger(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping
public RangeTrigger update( public RangeTrigger update(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) @Valid @RequestBody RangeConditionOrTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException { throws NotFoundException {
return save( return save(
rangeTriggerRepository rangeTriggerRepository

View file

@ -29,7 +29,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
private RegularLightRepository regularLightRepository; private RegularLightRepository regularLightRepository;
private SceneRepository sceneRepository; private SceneRepository sceneRepository;
private StateRepository<State<?>> stateRepository; private StateRepository<State> stateRepository;
private DeviceService deviceService; private DeviceService deviceService;
@Autowired @Autowired
@ -37,7 +37,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
UserRepository userRepository, UserRepository userRepository,
RegularLightRepository regularLightRepository, RegularLightRepository regularLightRepository,
SceneRepository sceneRepository, SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository, StateRepository<State> stateRepository,
DeviceService deviceService) { DeviceService deviceService) {
super(userRepository, regularLightRepository); super(userRepository, regularLightRepository);
this.regularLightRepository = regularLightRepository; this.regularLightRepository = regularLightRepository;
@ -98,7 +98,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -107,7 +107,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
regularLightRepository regularLightRepository
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,7 +22,7 @@ public class SceneController {
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private UserRepository userRepository; @Autowired private UserRepository userRepository;
@Autowired private SceneService sceneService; @Autowired private SceneService sceneService;
@Autowired private StateRepository<State<?>> stateRepository; @Autowired private StateRepository<State> stateRepository;
@GetMapping @GetMapping
public List<Scene> findAll( public List<Scene> findAll(
@ -78,7 +78,7 @@ public class SceneController {
} }
@PostMapping("/{id}/copyFrom/{copyId}") @PostMapping("/{id}/copyFrom/{copyId}")
public @ResponseBody List<State<?>> copy( public @ResponseBody List<State> copy(
@PathVariable("id") long id, @PathVariable("id") long id,
@PathVariable("copyId") long copyId, @PathVariable("copyId") long copyId,
final Principal principal) final Principal principal)
@ -126,8 +126,8 @@ public class SceneController {
* id). * id).
*/ */
@GetMapping(path = "/{sceneId}/states") @GetMapping(path = "/{sceneId}/states")
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) { public List<State> getStates(@PathVariable("sceneId") long sceneId) {
Iterable<State<?>> states = stateRepository.findBySceneId(sceneId); Iterable<State> states = stateRepository.findBySceneId(sceneId);
return toList(states); return toList(states);
} }
} }

View file

@ -26,7 +26,7 @@ public class SecurityCameraController {
private final DeviceService deviceService; private final DeviceService deviceService;
private final SecurityCameraRepository securityCameraService; private final SecurityCameraRepository securityCameraService;
private final SceneRepository sceneRepository; private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository; private final StateRepository<State> stateRepository;
private final CameraConfigurationService cameraConfigurationService; private final CameraConfigurationService cameraConfigurationService;
@Autowired @Autowired
@ -34,7 +34,7 @@ public class SecurityCameraController {
DeviceService deviceService, DeviceService deviceService,
SecurityCameraRepository securityCameraService, SecurityCameraRepository securityCameraService,
SceneRepository sceneRepository, SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository, StateRepository<State> stateRepository,
CameraConfigurationService cameraConfigurationService) { CameraConfigurationService cameraConfigurationService) {
this.deviceService = deviceService; this.deviceService = deviceService;
this.securityCameraService = securityCameraService; this.securityCameraService = securityCameraService;
@ -80,7 +80,7 @@ public class SecurityCameraController {
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -90,7 +90,7 @@ public class SecurityCameraController {
securityCameraService securityCameraService
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -19,7 +19,7 @@ public class SmartPlugController {
@Autowired private DeviceService deviceService; @Autowired private DeviceService deviceService;
@Autowired private SmartPlugRepository smartPlugRepository; @Autowired private SmartPlugRepository smartPlugRepository;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository; @Autowired private StateRepository<State> stateRepository;
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) { private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) {
newSP.setOn(sp.isOn()); newSP.setOn(sp.isOn());
@ -67,7 +67,7 @@ public class SmartPlugController {
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -77,7 +77,7 @@ public class SmartPlugController {
smartPlugRepository smartPlugRepository
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,9 +22,9 @@ public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateRepository; @Autowired private SwitchableStateRepository switchableStateRepository;
@PutMapping @PutMapping
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss) public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException { throws NotFoundException {
final SwitchableState<?> initial = final SwitchableState initial =
switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new); switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setOn(ss.isOn()); initial.setOn(ss.isOn());
switchableStateRepository.save(initial); switchableStateRepository.save(initial);

View file

@ -21,7 +21,7 @@ public class ThermostatController {
@Autowired private ThermostatRepository thermostatRepository; @Autowired private ThermostatRepository thermostatRepository;
@Autowired private ThermostatPopulationService thermostatService; @Autowired private ThermostatPopulationService thermostatService;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository; @Autowired private StateRepository<State> stateRepository;
private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) { private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) {
newT.setTargetTemperature(t.getTargetTemperature()); newT.setTargetTemperature(t.getTargetTemperature());
@ -63,7 +63,7 @@ public class ThermostatController {
} }
@PostMapping("/{id}/state") @PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding( public State sceneBinding(
@PathVariable("id") long deviceId, @PathVariable("id") long deviceId,
@RequestParam long sceneId, @RequestParam long sceneId,
final Principal principal) final Principal principal)
@ -73,7 +73,7 @@ public class ThermostatController {
thermostatRepository thermostatRepository
.findByIdAndUsername(deviceId, principal.getName()) .findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState(); State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId()); s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -158,7 +158,7 @@ public class UserAccountController {
final ConfirmationToken token = final ConfirmationToken token =
confirmationTokenRepository.findByConfirmToken(resetRequest.getConfirmationToken()); confirmationTokenRepository.findByConfirmToken(resetRequest.getConfirmationToken());
if (token == null || !token.getResetPassword()) { if (token == null || !token.isResetPassword()) {
throw new EmailTokenNotFoundException(); throw new EmailTokenNotFoundException();
} }
@ -186,7 +186,7 @@ public class UserAccountController {
final ConfirmationToken token = final ConfirmationToken token =
confirmationTokenRepository.findByConfirmToken(confirmationToken); confirmationTokenRepository.findByConfirmToken(confirmationToken);
if (token != null && !token.getResetPassword()) { if (token != null && !token.isResetPassword()) {
token.getUser().setEnabled(true); token.getUser().setEnabled(true);
userRepository.save(token.getUser()); userRepository.save(token.getUser());
response.sendRedirect(emailConfig.getRegistrationRedirect()); response.sendRedirect(emailConfig.getRegistrationRedirect());

View file

@ -1,14 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import java.util.List; import java.util.List;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
@ -36,7 +28,7 @@ public class AutomationFastUpdateRequest {
} }
public static class RangeTriggerDTO extends TriggerDTO { public static class RangeTriggerDTO extends TriggerDTO {
@NotNull RangeTrigger.Operator operator; @NotNull Operator operator;
@NotNull double range; @NotNull double range;
@Override @Override
@ -85,7 +77,7 @@ public class AutomationFastUpdateRequest {
public static class RangeConditionDTO extends ConditionDTO { public static class RangeConditionDTO extends ConditionDTO {
@NotNull RangeCondition.Operator operator; @NotNull Operator operator;
@NotNull double range; @NotNull double range;
@Override @Override

View file

@ -4,7 +4,7 @@ import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
@Data @Data
public class BooleanTriggerSaveRequest { public class BooleanConditionOrTriggerSaveRequest {
private long id; private long id;

View file

@ -1,42 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
public class BooleanConditionSaveRequest {
@NotNull private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
@NotNull private boolean on;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -1,11 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Operator;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
@Data @Data
public class RangeTriggerSaveRequest { public class RangeConditionOrTriggerSaveRequest {
private long id; private long id;
@ -13,7 +13,7 @@ public class RangeTriggerSaveRequest {
@NotNull private Long automationId; @NotNull private Long automationId;
@NotNull private RangeTrigger.Operator operator; @NotNull private Operator operator;
@NotNull private double range; @NotNull private double range;
} }

View file

@ -1,54 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator;
import javax.validation.constraints.NotNull;
public class RangeConditionSaveRequest {
@NotNull private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
@NotNull private RangeCondition.Operator operator;
@NotNull private double range;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public Operator getOperator() {
return operator;
}
public void setOperator(Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(double range) {
this.range = range;
}
}

View file

@ -5,9 +5,11 @@ import java.math.BigDecimal;
import javax.persistence.EnumType; import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@Data @Data
@AllArgsConstructor
public class SensorSaveRequest { public class SensorSaveRequest {
/** The type of this sensor */ /** The type of this sensor */
@NotNull @NotNull

View file

@ -6,8 +6,9 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import lombok.Data;
@Data
@Entity @Entity
public class Automation { public class Automation {
@ -22,7 +23,6 @@ public class Automation {
@GsonExclude @GsonExclude
private User user; private User user;
@NotNull
@Column(name = "user_id", nullable = false) @Column(name = "user_id", nullable = false)
@GsonExclude @GsonExclude
private Long userId; private Long userId;
@ -36,49 +36,5 @@ public class Automation {
@OneToMany(mappedBy = "automation", orphanRemoval = true, cascade = CascadeType.REMOVE) @OneToMany(mappedBy = "automation", orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Condition<?>> conditions = new HashSet<>(); private Set<Condition<?>> conditions = new HashSet<>();
@NotNull @NotEmpty private String name; @NotEmpty private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set<ScenePriority> getScenes() {
return scenes;
}
public Set<Trigger<?>> getTriggers() {
return triggers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Set<Condition<?>> getConditions() {
return conditions;
}
} }

View file

@ -2,10 +2,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity @Entity
public class BooleanCondition extends Condition<BooleanTriggerable> { public class BooleanCondition extends Condition<BooleanTriggerable> {
@Getter
@Setter
@Column(name = "switchable_on") @Column(name = "switchable_on")
private boolean on; private boolean on;
@ -13,14 +17,6 @@ public class BooleanCondition extends Condition<BooleanTriggerable> {
super("booleanCondition"); super("booleanCondition");
} }
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
@Override @Override
public boolean triggered() { public boolean triggered() {
return this.getDevice().readTriggerState() == isOn(); return this.getDevice().readTriggerState() == isOn();

View file

@ -2,29 +2,21 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity @Entity
public class BooleanTrigger extends Trigger<BooleanTriggerable> { public class BooleanTrigger extends Trigger<BooleanTriggerable> {
@Column(name = "switchable_on") @Column(name = "switchable_on")
@Getter
@Setter
private boolean on; private boolean on;
public BooleanTrigger() { public BooleanTrigger() {
super("booleanTrigger"); super("booleanTrigger");
} }
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public boolean check(boolean on) {
return this.on == on;
}
@Override @Override
public boolean triggered() { public boolean triggered() {
return getDevice().readTriggerState() == isOn(); return getDevice().readTriggerState() == isOn();

View file

@ -10,7 +10,9 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Transient; import javax.persistence.Transient;
import lombok.Data;
@Data
@Entity @Entity
public abstract class Condition<D> { public abstract class Condition<D> {
@ -52,42 +54,6 @@ public abstract class Condition<D> {
public abstract boolean triggered(); public abstract boolean triggered();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public D getDevice() {
return device;
}
public void setDevice(D device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Automation getAutomation() {
return automation;
}
public void setAutomation(Automation automation) {
this.automation = automation;
}
public Long getAutomationId() {
return automationId;
}
public Condition<D> setAutomationId(Long automationId) { public Condition<D> setAutomationId(Long automationId) {
this.automationId = automationId; this.automationId = automationId;
return this; return this;

View file

@ -12,7 +12,9 @@ import javax.persistence.JoinColumn;
import javax.persistence.OneToOne; import javax.persistence.OneToOne;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import lombok.Data;
@Data
@Entity @Entity
public class ConfirmationToken { public class ConfirmationToken {
@ -40,47 +42,4 @@ public class ConfirmationToken {
confirmToken = UUID.randomUUID().toString(); confirmToken = UUID.randomUUID().toString();
resetPassword = false; resetPassword = false;
} }
/** Constructor for hibernate reflective stuff things whatever */
public ConfirmationToken() {}
public Long getId() {
return id;
}
public String getConfirmToken() {
return confirmToken;
}
public Date getCreatedDate() {
return (Date) createdDate.clone();
}
public User getUser() {
return user;
}
public void setId(Long id) {
this.id = id;
}
public void setConfirmToken(String confirmToken) {
this.confirmToken = confirmToken;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = (Date) createdDate.clone();
}
public void setUser(User user) {
this.user = user;
}
public boolean getResetPassword() {
return resetPassword;
}
public void setResetPassword(boolean resetPassword) {
this.resetPassword = resetPassword;
}
} }

View file

@ -6,9 +6,11 @@ import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.Data;
/** Generic abstraction for a smart home device */ /** Generic abstraction for a smart home device */
@Entity @Entity
@Data
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.JOINED)
public abstract class Device { public abstract class Device {
@ -65,7 +67,7 @@ public abstract class Device {
@OneToMany(mappedBy = "device", orphanRemoval = true) @OneToMany(mappedBy = "device", orphanRemoval = true)
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
private Set<State<?>> states; private Set<State> states;
@Transient @GsonExclude private Long fromHostId = null; @Transient @GsonExclude private Long fromHostId = null;
@ -73,88 +75,8 @@ public abstract class Device {
@Transient @GsonExclude private boolean deleted = false; @Transient @GsonExclude private boolean deleted = false;
public Device(String kind, FlowType flowType) { protected Device(String kind, FlowType flowType) {
this.kind = kind; this.kind = kind;
this.flowType = flowType; this.flowType = flowType;
} }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public Set<Trigger<? extends Device>> getTriggers() {
return triggers;
}
public void setTriggers(Set<Trigger<? extends Device>> triggers) {
this.triggers = triggers;
}
public Long getRoomId() {
return roomId;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public FlowType getFlowType() {
return flowType;
}
public Set<State<?>> getStates() {
return states;
}
public void setStates(Set<State<?>> states) {
this.states = states;
}
public Long getFromHostId() {
return fromHostId;
}
public void setFromHostId(Long fromHostId) {
this.fromHostId = fromHostId;
}
public boolean isFromGuest() {
return fromGuest;
}
public void setFromGuest(boolean fromGuest) {
this.fromGuest = fromGuest;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
} }

View file

@ -7,7 +7,8 @@ import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import lombok.Getter;
import lombok.Setter;
@Entity @Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@ -20,23 +21,22 @@ public class Dimmable extends Switchable implements RangeTriggerable {
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude @SocketGsonExclude
@Getter
@Setter
private Set<Dimmer> dimmers = new HashSet<>(); private Set<Dimmer> dimmers = new HashSet<>();
/** The light intensity value. Goes from 0 (off) to 100 (on) */ /** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull
@Column(nullable = false) @Column(nullable = false)
@Min(0) @Min(0)
@Max(100) @Max(100)
@Getter
private Integer intensity = 0; private Integer intensity = 0;
@NotNull
@Column(nullable = false) @Column(nullable = false)
@Getter
@Setter
private Integer oldIntensity = 100; private Integer oldIntensity = 100;
public Integer getIntensity() {
return intensity;
}
/** /**
* Sets the intensity to a certain level. Out of bound values are corrected to the respective * Sets the intensity to a certain level. Out of bound values are corrected to the respective
* extremums. An intensity level of 0 turns the light off, but keeps the old intensity level * extremums. An intensity level of 0 turns the light off, but keeps the old intensity level
@ -66,19 +66,13 @@ public class Dimmable extends Switchable implements RangeTriggerable {
intensity = on ? oldIntensity : 0; intensity = on ? oldIntensity : 0;
} }
public Set<Dimmer> getDimmers() { public void readStateAndSet(DimmableState state) {
return this.dimmers;
}
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
setIntensity(state.getIntensity()); setIntensity(state.getIntensity());
} }
@Override public State cloneState() {
public State<? extends Dimmable> cloneState() { final DimmableState newState = new DimmableState();
final DimmableState<Dimmable> newState = new DimmableState<>();
newState.setDeviceId(getId()); newState.setDeviceId(getId());
newState.setDevice(this);
newState.setIntensity(getIntensity()); newState.setIntensity(getIntensity());
return newState; return newState;
} }

View file

@ -3,32 +3,32 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import lombok.Getter;
import lombok.Setter;
/** Represent a state for an IDimmable device */ /** Represent a state for an IDimmable device */
@Entity @Entity
public class DimmableState<T extends Dimmable> extends State<T> { public class DimmableState extends State {
public void setDevice(Dimmable device) {
setInnerDevice(device);
}
/** The light intensity value. Goes from 0 (off) to 100 (on) */ /** The light intensity value. Goes from 0 (off) to 100 (on) */
@Min(0) @Min(0)
@Max(100) @Max(100)
@Getter
@Setter
private int intensity = 0; private int intensity = 0;
public int getIntensity() {
return intensity;
}
public void setIntensity(int dimAmount) {
this.intensity = dimAmount;
}
@Override @Override
public void apply() { public void apply() {
getDevice().readStateAndSet(this); ((Dimmable) getDevice()).readStateAndSet(this);
} }
@Override @Override
protected State<T> copy() { protected DimmableState copy() {
final DimmableState<T> d = new DimmableState<>(); final DimmableState d = new DimmableState();
d.setIntensity(intensity); d.setIntensity(intensity);
return d; return d;
} }

View file

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

View file

@ -10,7 +10,7 @@ import javax.persistence.*;
@Entity @Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dimmer extends InputDevice implements Connectable<Dimmable> { public abstract class Dimmer extends InputDevice implements Connectable<Dimmable> {
public Dimmer(String kind) { protected Dimmer(String kind) {
super(kind); super(kind);
} }

View file

@ -2,22 +2,18 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
/** Represents a motion sensor device */ /** Represents a motion sensor device */
@Entity @Entity
public class MotionSensor extends InputDevice implements BooleanTriggerable { public class MotionSensor extends InputDevice implements BooleanTriggerable {
@Getter
@Setter
@Column(nullable = false) @Column(nullable = false)
private boolean detected; private boolean detected;
public boolean isDetected() {
return detected;
}
public void setDetected(boolean detected) {
this.detected = detected;
}
public MotionSensor() { public MotionSensor() {
super("motionSensor"); super("motionSensor");
} }

View file

@ -0,0 +1,33 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName;
public enum Operator {
@SerializedName("EQUAL")
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL;
boolean checkAgainst(double value, double range) {
switch (this) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
default:
throw new IllegalStateException();
}
}
}

View file

@ -14,10 +14,10 @@ public abstract class OutputDevice extends Device {
} }
/** /**
* Creates a State<?> object initialized to point at this device and with values copied from * Creates a State object initialized to point at this device and with values copied from this
* this device's state * device's state
* *
* @return a new State<?> object * @return a new State object
*/ */
public abstract State<? extends OutputDevice> cloneState(); public abstract State cloneState();
} }

View file

@ -1,10 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger.Operator;
import com.google.gson.annotations.SerializedName;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.NotNull; import lombok.Getter;
import lombok.Setter;
@Entity @Entity
public class RangeCondition extends Condition<RangeTriggerable> { public class RangeCondition extends Condition<RangeTriggerable> {
@ -13,57 +12,18 @@ public class RangeCondition extends Condition<RangeTriggerable> {
super("rangeCondition"); super("rangeCondition");
} }
public enum Operator { @Getter
@SerializedName("EQUAL") @Setter
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL
}
@Column(nullable = false) @Column(nullable = false)
private RangeCondition.Operator operator; private Operator operator;
@NotNull @Getter
@Setter
@Column(nullable = false) @Column(nullable = false)
private double range; private double range;
public RangeCondition.Operator getOperator() {
return operator;
}
public void setOperator(RangeCondition.Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(Double range) {
this.range = range;
}
@Override @Override
public boolean triggered() { public boolean triggered() {
double value = getDevice().readTriggerState(); return operator.checkAgainst(getDevice().readTriggerState(), range);
switch (operator) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
}
throw new IllegalStateException();
} }
} }

View file

@ -1,8 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
@Entity @Entity
public class RangeTrigger extends Trigger<RangeTriggerable> { public class RangeTrigger extends Trigger<RangeTriggerable> {
@ -11,56 +12,18 @@ public class RangeTrigger extends Trigger<RangeTriggerable> {
super("rangeTrigger"); super("rangeTrigger");
} }
@Override @Getter
public boolean triggered() { @Setter
double value = getDevice().readTriggerState();
switch (operator) {
case EQUAL:
return value == range;
case LESS:
return value < range;
case GREATER:
return value > range;
case GREATER_EQUAL:
return value >= range;
case LESS_EQUAL:
return value <= range;
}
throw new IllegalStateException();
}
public enum Operator {
@SerializedName("EQUAL")
EQUAL,
@SerializedName("LESS")
LESS,
@SerializedName("GREATER")
GREATER,
@SerializedName("LESS_EQUAL")
LESS_EQUAL,
@SerializedName("GREATER_EQUAL")
GREATER_EQUAL
}
@Column(nullable = false) @Column(nullable = false)
private Operator operator; private Operator operator;
@Getter
@Setter
@Column(nullable = false) @Column(nullable = false)
private double range; private double range;
public Operator getOperator() { @Override
return operator; public boolean triggered() {
} return operator.checkAgainst(getDevice().readTriggerState(), range);
public void setOperator(Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(Double range) {
this.range = range;
} }
} }

View file

@ -2,7 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.NotNull; import lombok.Getter;
import lombok.Setter;
/** Represents a standard non-dimmable light */ /** Represents a standard non-dimmable light */
@Entity @Entity
@ -10,22 +11,12 @@ public class RegularLight extends Switchable implements BooleanTriggerable {
/** Whether the light is on or not */ /** Whether the light is on or not */
@Column(name = "light_on", nullable = false) @Column(name = "light_on", nullable = false)
@NotNull @Getter
@Setter
boolean on; boolean on;
public RegularLight() { public RegularLight() {
super("regularLight"); super("regularLight");
this.on = false;
}
@Override
public boolean isOn() {
return on;
}
@Override
public void setOn(boolean on) {
this.on = on;
} }
@Override @Override

View file

@ -5,7 +5,6 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull;
/** Represents a room in the house owned by the user */ /** Represents a room in the house owned by the user */
@Entity @Entity
@ -42,12 +41,10 @@ public class Room {
* User that owns the house this room is in as a foreign key id. To use when updating and * User that owns the house this room is in as a foreign key id. To use when updating and
* inserting from a REST call. * inserting from a REST call.
*/ */
@NotNull
@Column(name = "user_id", nullable = false) @Column(name = "user_id", nullable = false)
private Long userId; private Long userId;
/** The user given name of this room (e.g. 'Master bedroom') */ /** The user given name of this room (e.g. 'Master bedroom') */
@NotNull
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;

View file

@ -5,12 +5,13 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import lombok.Data;
/** /**
* Represent a collection of state changes to devices even in different rooms but belonging to the * Represent a collection of state changes to devices even in different rooms but belonging to the
* same user * same user
*/ */
@Data
@Entity @Entity
public class Scene { public class Scene {
@ -25,80 +26,21 @@ public class Scene {
@GsonExclude @GsonExclude
private User user; private User user;
@NotNull
@Column(name = "user_id", nullable = false) @Column(name = "user_id", nullable = false)
@GsonExclude @GsonExclude
private Long userId; private Long userId;
@OneToMany(mappedBy = "scene", orphanRemoval = true) @OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude @GsonExclude
private Set<State<?>> states = new HashSet<>(); private Set<State> states = new HashSet<>();
/** The user given name of this room (e.g. 'Master bedroom') */ /** The user given name of this room (e.g. 'Master bedroom') */
@NotNull
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(nullable = false)
@NotNull
private Icon icon; private Icon icon;
/** Determines whether a guest can access this scene */ /** Determines whether a guest can access this scene */
@Column private boolean guestAccessEnabled; @Column private boolean guestAccessEnabled;
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public boolean isGuestAccessEnabled() {
return guestAccessEnabled;
}
public void setGuestAccessEnabled(boolean guestAccessEnabled) {
this.guestAccessEnabled = guestAccessEnabled;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set<State<?>> getStates() {
return states;
}
public void setStates(Set<State<?>> states) {
this.states = states;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
} }

View file

@ -11,7 +11,6 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.PreRemove; import javax.persistence.PreRemove;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Entity @Entity
public class ScenePriority { public class ScenePriority {
@ -30,7 +29,6 @@ public class ScenePriority {
@Column(name = "automation_id", nullable = false) @Column(name = "automation_id", nullable = false)
private Long automationId; private Long automationId;
@NotNull
@Min(0) @Min(0)
@Column(nullable = false) @Column(nullable = false)
private Integer priority; private Integer priority;

View file

@ -2,7 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
@Entity @Entity
public class SecurityCamera extends Switchable implements BooleanTriggerable { public class SecurityCamera extends Switchable implements BooleanTriggerable {
@ -13,7 +12,6 @@ public class SecurityCamera extends Switchable implements BooleanTriggerable {
} }
@Column(name = "camera_on", nullable = false) @Column(name = "camera_on", nullable = false)
@NotNull
private boolean on; private boolean on;
@Column(name = "video", nullable = false) @Column(name = "video", nullable = false)

View file

@ -3,7 +3,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
/** A smart plug that can be turned either on or off */ /** A smart plug that can be turned either on or off */
@Entity @Entity
@ -14,7 +13,6 @@ public class SmartPlug extends Switchable implements BooleanTriggerable {
/** The total amount of power that the smart plug has consumed represented in W/h */ /** The total amount of power that the smart plug has consumed represented in W/h */
@Column(precision = 13, scale = 3) @Column(precision = 13, scale = 3)
@NotNull
private BigDecimal totalConsumption = BigDecimal.ZERO; private BigDecimal totalConsumption = BigDecimal.ZERO;
/** Whether the smart plug is on */ /** Whether the smart plug is on */

View file

@ -3,7 +3,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import lombok.Getter;
import lombok.Setter;
/** /**
* Represents instructions on how to change the state of a particular device. Many states (plus * Represents instructions on how to change the state of a particular device. Many states (plus
@ -12,95 +13,66 @@ import javax.validation.constraints.NotNull;
@Entity @Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})}) @Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class State<D extends OutputDevice> { public abstract class State {
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
@Getter
private OutputDevice device;
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true) @Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
@Getter
private long id; private long id;
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
private D device;
/** /**
* The device this state belongs in, as a foreign key id. To use when updating and inserting * The device this state belongs in, as a foreign key id. To use when updating and inserting
* from a REST call. * from a REST call.
*/ */
@Column(name = "device_id", nullable = false) @Column(name = "device_id", nullable = false)
@NotNull @Getter
@Setter
private Long deviceId; private Long deviceId;
@ManyToOne @ManyToOne
@JoinColumn(name = "scene_id", updatable = false, insertable = false) @JoinColumn(name = "scene_id", updatable = false, insertable = false)
@GsonExclude @GsonExclude
@Getter
@Setter
private Scene scene; private Scene scene;
@Column(name = "scene_id", nullable = false) @Column(name = "scene_id", nullable = false)
@NotNull @Getter
@Setter
private Long sceneId; private Long sceneId;
protected void setInnerDevice(OutputDevice device) {
this.device = device;
}
/** Sets the state of the connected device to the state represented by this object. */ /** Sets the state of the connected device to the state represented by this object. */
public abstract void apply(); public abstract void apply();
/** Creates a perfect copy of this state, except for the id field and the sceneId/scene */ /** Creates a perfect copy of this state, except for the id field and the sceneId/scene */
protected abstract State<D> copy(); protected abstract State copy();
public State<D> copyToSceneId(Long sceneId) { public State copyToSceneId(Long sceneId) {
final State<D> s = copy(); final State s = copy();
s.setDeviceId(this.deviceId); s.setDeviceId(this.deviceId);
s.device = this.device;
s.setSceneId(sceneId); s.setSceneId(sceneId);
s.setScene(this.scene);
return s; return s;
} }
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public D getDevice() {
return device;
}
public void setDevice(D device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
@PreRemove @PreRemove
public void removeDeviceAndScene() { public void removeDeviceAndScene() {
this.setScene(null);
this.setSceneId(null); this.setSceneId(null);
this.setScene(null);
this.setDevice(null);
this.setDeviceId(null); this.setDeviceId(null);
this.device = null;
} }
} }

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

@ -45,15 +45,13 @@ public abstract class Switchable extends OutputDevice {
return inputs; return inputs;
} }
public void readStateAndSet(SwitchableState<? extends Switchable> state) { public void readStateAndSet(SwitchableState state) {
setOn(state.isOn()); setOn(state.isOn());
} }
@Override public State cloneState() {
public State<? extends Switchable> cloneState() { final SwitchableState newState = new SwitchableState();
final SwitchableState<Switchable> newState = new SwitchableState<>();
newState.setDeviceId(getId()); newState.setDeviceId(getId());
newState.setDevice(this);
newState.setOn(isOn()); newState.setOn(isOn());
return newState; return newState;
} }

View file

@ -2,30 +2,30 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
/** Represents a state for a Switchable device */ /** Represents a state for a Switchable device */
@Entity @Entity
public class SwitchableState<T extends Switchable> extends State<T> { public class SwitchableState extends State {
public void setDevice(Switchable device) {
setInnerDevice(device);
}
@Column(name = "switchable_on") @Column(name = "switchable_on")
@Getter
@Setter
private boolean on; private boolean on;
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
@Override @Override
public void apply() { public void apply() {
getDevice().readStateAndSet(this); ((Switchable) getDevice()).readStateAndSet(this);
} }
@Override @Override
protected State<T> copy() { protected SwitchableState copy() {
final SwitchableState<T> d = new SwitchableState<>(); final SwitchableState d = new SwitchableState();
d.setOn(on); d.setOn(on);
return d; return d;
} }

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

@ -5,7 +5,6 @@ import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
/** A thermostat capable of controlling cooling and heating. */ /** A thermostat capable of controlling cooling and heating. */
@Entity @Entity
@ -72,7 +71,7 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
Sensor.TYPICAL_VALUES.get(Sensor.SensorType.TEMPERATURE); Sensor.TYPICAL_VALUES.get(Sensor.SensorType.TEMPERATURE);
/** State of this thermostat */ /** State of this thermostat */
@Column @NotNull private Thermostat.Mode mode; @Column private Thermostat.Mode mode;
@Transient private BigDecimal measuredTemperature; @Transient private BigDecimal measuredTemperature;

View file

@ -11,7 +11,7 @@ public class SceneService {
private final DevicePopulationService devicePopulationService; private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService; private final DevicePropagationService devicePropagationService;
private final StateRepository<State<?>> stateRepository; private final StateRepository<State> stateRepository;
private final SceneRepository sceneRepository; private final SceneRepository sceneRepository;
public Scene findByValidatedId(Long id) { public Scene findByValidatedId(Long id) {
@ -22,7 +22,7 @@ public class SceneService {
public SceneService( public SceneService(
DevicePopulationService devicePopulationService, DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService, DevicePropagationService devicePropagationService,
StateRepository<State<?>> stateRepository, StateRepository<State> stateRepository,
SceneRepository sceneRepository) { SceneRepository sceneRepository) {
this.devicePopulationService = devicePopulationService; this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService; this.devicePropagationService = devicePropagationService;
@ -33,7 +33,7 @@ public class SceneService {
private List<Device> copyStatesToDevices(Scene fromScene) { private List<Device> copyStatesToDevices(Scene fromScene) {
final List<Device> updated = new ArrayList<>(fromScene.getStates().size()); final List<Device> updated = new ArrayList<>(fromScene.getStates().size());
for (final State<?> s : fromScene.getStates()) { for (final State s : fromScene.getStates()) {
s.apply(); s.apply();
updated.add(s.getDevice()); updated.add(s.getDevice());
} }
@ -54,9 +54,9 @@ public class SceneService {
return updated; return updated;
} }
public List<State<?>> copyStates(Scene to, Scene from) { public List<State> copyStates(Scene to, Scene from) {
final ArrayList<State<?>> states = new ArrayList<>(); final ArrayList<State> states = new ArrayList<>();
for (final State<?> s : from.getStates()) { for (final State s : from.getStates()) {
states.add(stateRepository.save(s.copyToSceneId(to.getId()))); states.add(stateRepository.save(s.copyToSceneId(to.getId())));
} }
return states; return states;

View file

@ -0,0 +1,77 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
import java.math.BigDecimal;
import java.security.Principal;
import lombok.SneakyThrows;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@DisplayName("The sensor controller")
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
public class SensorControllerTests {
@InjectMocks private SensorController sensorController;
@Mock private DeviceService deviceService;
@Mock private Principal mockPrincipal;
@BeforeEach
public void setup() {
when(mockPrincipal.getName()).thenReturn("user");
}
private void checkSensorAgainstRequest(final Sensor toCheck, final SensorSaveRequest request) {
assertThat(toCheck).isNotNull();
assertThat(toCheck.getSensor()).isEqualTo(request.getSensor());
assertThat(toCheck.getValue()).isEqualTo(request.getValue());
assertThat(toCheck.getName()).isEqualTo(request.getName());
assertThat(toCheck.getRoomId()).isEqualTo(request.getRoomId());
}
@DisplayName("when creating should return the same object")
@Test
@SneakyThrows(NotFoundException.class)
public void testCreate() {
doNothing().when(deviceService).throwIfRoomNotOwned(anyLong(), eq("user"));
when(deviceService.saveAsOwner(any(Sensor.class), eq("user")))
.thenAnswer(i -> i.getArguments()[0]);
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
final SensorSaveRequest toSend =
new SensorSaveRequest(
Sensor.SensorType.TEMPERATURE, BigDecimal.ZERO, 42L, "Test sensor");
final Sensor created = sensorController.create(toSend, mockPrincipal);
checkSensorAgainstRequest(created, toSend);
}
@DisplayName("when deleting an existant id should succeed")
@Test
@SneakyThrows(NotFoundException.class)
public void testDelete() {
doNothing().when(deviceService).deleteByIdAsOwner(eq(42L), eq("user"));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
sensorController.deleteById(42L, mockPrincipal);
}
}