wip, implemented very long stream in DeviceService, SceneService is missing though
This commit is contained in:
parent
5c2f828834
commit
570b1e8b29
2 changed files with 23 additions and 15 deletions
|
@ -0,0 +1,5 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface AutomationRepository extends CrudRepository<Automation, Long> {}
|
|
@ -1,10 +1,14 @@
|
|||
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.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.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -13,19 +17,10 @@ import org.springframework.stereotype.Component;
|
|||
@Component
|
||||
public class DeviceService {
|
||||
|
||||
/*
|
||||
Automation entity
|
||||
- name
|
||||
- id
|
||||
- Set<Trigger>
|
||||
- OrderedList<Scene> // define order of application of scenes of the automation
|
||||
// separate AutomationWithOrder id entity:
|
||||
// - application_order_id (1 for first, 2 for second)
|
||||
// - scene
|
||||
// - FK automation
|
||||
*/
|
||||
|
||||
@Autowired DeviceRepository<Device> deviceRepository;
|
||||
@Autowired AutomationRepository automationRepository;
|
||||
@Autowired SceneRepository sceneRepository;
|
||||
// @Autowired SceneService sceneService;
|
||||
@Autowired TriggerRepository<Trigger<? extends Device>> triggerRepository;
|
||||
|
||||
public <T extends Device> List<T> saveAll(Collection<T> devices) {
|
||||
|
@ -38,12 +33,20 @@ public class DeviceService {
|
|||
|
||||
List<Trigger<? extends Device>> triggers = triggerRepository.findAllByDeviceId(deviceId);
|
||||
|
||||
List<Trigger<? extends Device>> triggeredTriggers =
|
||||
triggers.stream().filter(Trigger::triggered).collect(Collectors.toList());
|
||||
triggers.stream()
|
||||
.filter(Trigger::triggered)
|
||||
.map(Trigger::getAutomationId)
|
||||
.map(t -> automationRepository.findById(t))
|
||||
.distinct()
|
||||
.map(t -> t.get().getScenes())
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.sorted(Comparator.comparing(ScenePriority::getPriority))
|
||||
.map(t -> sceneRepository.findById(t.getSceneId()))
|
||||
.forEach(SceneService::apply);
|
||||
|
||||
// map activated -> automations
|
||||
// remove duplicates
|
||||
// sort automations per priority
|
||||
// sort scenes inside automations per priority
|
||||
// apply scenes (SceneService.apply())
|
||||
|
||||
|
|
Loading…
Reference in a new issue