Code review
This commit is contained in:
parent
570b1e8b29
commit
37ea9591cf
3 changed files with 56 additions and 37 deletions
|
@ -5,8 +5,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.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SceneService;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -26,20 +26,21 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/scene")
|
@RequestMapping("/scene")
|
||||||
public class SceneController {
|
public class SceneController {
|
||||||
|
|
||||||
@Autowired SceneRepository sceneService;
|
@Autowired private SceneRepository sceneRepository;
|
||||||
@Autowired UserRepository userService;
|
@Autowired private SceneService sceneService;
|
||||||
@Autowired StateRepository<State<?>> stateService;
|
@Autowired private UserRepository userService;
|
||||||
@Autowired DeviceRepository<Device> deviceRepository;
|
@Autowired private StateRepository<State<?>> stateService;
|
||||||
|
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Scene> findAll(Principal principal) {
|
public List<Scene> findAll(Principal principal) {
|
||||||
return toList(sceneService.findByUsername(principal.getName()));
|
return toList(sceneRepository.findByUsername(principal.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody Scene findById(@PathVariable("id") long id, Principal principal)
|
public @ResponseBody Scene findById(@PathVariable("id") long id, Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
return sceneService
|
return sceneRepository
|
||||||
.findByIdAndUsername(id, principal.getName())
|
.findByIdAndUsername(id, principal.getName())
|
||||||
.orElseThrow(NotFoundException::new);
|
.orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
@ -56,26 +57,18 @@ public class SceneController {
|
||||||
newScene.setUserId(userId);
|
newScene.setUserId(userId);
|
||||||
newScene.setName(s.getName());
|
newScene.setName(s.getName());
|
||||||
|
|
||||||
return sceneService.save(newScene);
|
return sceneRepository.save(newScene);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{id}/apply")
|
@PostMapping("/{id}/apply")
|
||||||
public @ResponseBody List<Device> apply(@PathVariable("id") long id, final Principal principal)
|
public @ResponseBody List<Device> apply(@PathVariable("id") long id, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
final Scene newScene =
|
final Scene newScene =
|
||||||
sceneService
|
sceneRepository
|
||||||
.findByIdAndUsername(id, principal.getName())
|
.findByIdAndUsername(id, principal.getName())
|
||||||
.orElseThrow(NotFoundException::new);
|
.orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
final List<Device> updated = new ArrayList<>();
|
return sceneService.apply(newScene);
|
||||||
|
|
||||||
for (final State<?> s : newScene.getStates()) {
|
|
||||||
s.apply();
|
|
||||||
updated.add(s.getDevice());
|
|
||||||
}
|
|
||||||
deviceRepository.saveAll(updated);
|
|
||||||
|
|
||||||
return updated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
|
@ -83,7 +76,7 @@ public class SceneController {
|
||||||
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
final Scene newScene =
|
final Scene newScene =
|
||||||
sceneService
|
sceneRepository
|
||||||
.findByIdAndUsername(id, principal.getName())
|
.findByIdAndUsername(id, principal.getName())
|
||||||
.orElseThrow(NotFoundException::new);
|
.orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
|
@ -91,13 +84,13 @@ public class SceneController {
|
||||||
newScene.setName(s.getName());
|
newScene.setName(s.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
return sceneService.save(newScene);
|
return sceneRepository.save(newScene);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void deleteById(@PathVariable("id") long id) {
|
public void deleteById(@PathVariable("id") long id) {
|
||||||
stateService.deleteAllBySceneId(id);
|
stateService.deleteAllBySceneId(id);
|
||||||
sceneService.deleteById(id);
|
sceneRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.TriggerRepository;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -17,11 +11,11 @@ import org.springframework.stereotype.Component;
|
||||||
@Component
|
@Component
|
||||||
public class DeviceService {
|
public class DeviceService {
|
||||||
|
|
||||||
@Autowired DeviceRepository<Device> deviceRepository;
|
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||||
@Autowired AutomationRepository automationRepository;
|
@Autowired private AutomationRepository automationRepository;
|
||||||
@Autowired SceneRepository sceneRepository;
|
@Autowired private SceneRepository sceneRepository;
|
||||||
// @Autowired SceneService sceneService;
|
@Autowired private SceneService sceneService;
|
||||||
@Autowired TriggerRepository<Trigger<? extends Device>> triggerRepository;
|
@Autowired private TriggerRepository<Trigger<? extends Device>> triggerRepository;
|
||||||
|
|
||||||
public <T extends Device> List<T> saveAll(Collection<T> devices) {
|
public <T extends Device> List<T> saveAll(Collection<T> devices) {
|
||||||
return devices.stream().map(this::save).collect(Collectors.toList());
|
return devices.stream().map(this::save).collect(Collectors.toList());
|
||||||
|
@ -36,14 +30,18 @@ public class DeviceService {
|
||||||
triggers.stream()
|
triggers.stream()
|
||||||
.filter(Trigger::triggered)
|
.filter(Trigger::triggered)
|
||||||
.map(Trigger::getAutomationId)
|
.map(Trigger::getAutomationId)
|
||||||
.map(t -> automationRepository.findById(t))
|
.map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new))
|
||||||
.distinct()
|
.distinct()
|
||||||
.map(t -> t.get().getScenes())
|
.map(Automation::getScenes)
|
||||||
.flatMap(Collection::stream)
|
.flatMap(Collection::stream)
|
||||||
.distinct()
|
.distinct()
|
||||||
.sorted(Comparator.comparing(ScenePriority::getPriority))
|
.sorted(Comparator.comparing(ScenePriority::getPriority))
|
||||||
.map(t -> sceneRepository.findById(t.getSceneId()))
|
.map(
|
||||||
.forEach(SceneService::apply);
|
t ->
|
||||||
|
sceneRepository
|
||||||
|
.findById(t.getSceneId())
|
||||||
|
.orElseThrow(IllegalStateException::new))
|
||||||
|
.forEach(sceneService::apply);
|
||||||
|
|
||||||
// map activated -> automations
|
// map activated -> automations
|
||||||
// remove duplicates
|
// remove duplicates
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SceneService {
|
||||||
|
|
||||||
|
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||||
|
|
||||||
|
public List<Device> apply(Scene newScene) {
|
||||||
|
final List<Device> updated = new ArrayList<>();
|
||||||
|
|
||||||
|
for (final State<?> s : newScene.getStates()) {
|
||||||
|
s.apply();
|
||||||
|
updated.add(s.getDevice());
|
||||||
|
}
|
||||||
|
deviceRepository.saveAll(updated);
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue