From 09c08580caa261eec23372b7674060261c3950e7 Mon Sep 17 00:00:00 2001 From: omenem Date: Thu, 7 May 2020 19:01:35 +0200 Subject: [PATCH] Everything should be implemented --- .../smarthut/models/ConditionRepository.java | 2 + .../smarthut/service/DeviceService.java | 77 +++++++++++++++++-- 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java index 346dfd5..cb6c1c4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java @@ -7,4 +7,6 @@ import org.springframework.data.repository.query.Param; public interface ConditionRepository> extends CrudRepository { List findAllByDeviceId(@Param("deviceId") long deviceId); + + List findAllByAutomationId(@Param("automationId") long automationId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index d2b02a6..f25c72f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -5,6 +5,7 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.List; @@ -22,6 +23,7 @@ public class DeviceService { @Autowired private SceneRepository sceneRepository; @Autowired private SceneService sceneService; @Autowired private TriggerRepository> triggerRepository; + @Autowired private ConditionRepository> conditionRepository; @Autowired private RoomRepository roomRepository; @Autowired private EagerUserRepository userRepository; @Autowired private SensorSocketEndpoint endpoint; @@ -43,12 +45,75 @@ public class DeviceService { final long deviceId = device.getId(); + // find the conditions that this device state change is triggering + List> triggeredConditions = + conditionRepository + .findAllByDeviceId(deviceId) + .stream() + .filter(Condition::triggered) + .collect(Collectors.toList()); + + List automationsWithMetConditions = new ArrayList<>(); + + // this condition is connected to an automation obviously, and this automation might be + // connected to many other conditions, I need to find them all and make sure all of them are + // in their "TRUE" state. + // if that's the case I need to remember that this automation needs to be triggered + for (Condition condition : triggeredConditions) { + + Automation a = + automationRepository + .findById(condition.getAutomationId()) + .orElseThrow(IllegalStateException::new); + List> conditions = + conditionRepository.findAllByAutomationId(a.getId()); + + if (conditions.size() + == conditions + .stream() + .filter(Condition::triggered) + .collect(Collectors.toList()) + .size()) { + automationsWithMetConditions.add(a); + } + } + List> triggers = triggerRepository.findAllByDeviceId(deviceId); - triggers.stream() - .filter(Trigger::triggered) - .map(Trigger::getAutomationId) - .map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new)) + // these are all the automations that are triggered, now I need to check if they are + // associated + // with some conditions, if they are I need to check that they are present also in the + // automationsToTrigger list. + // there are two cases: + // 1. this automation has been triggered and it is not connected to any condition, therefore + // this can be kept in the list + // 2. this automation has been triggered, it is connected to one or more conditions, but it + // is not present in the automationsToTrigger list, therefore it has to be discarded + List triggeredAutomationsTMP = + triggers.stream() + .filter(Trigger::triggered) + .map(Trigger::getAutomationId) + .map( + t -> + automationRepository + .findById(t) + .orElseThrow(IllegalStateException::new)) + .distinct() + .collect(Collectors.toList()); + + List triggeredAutomations = new ArrayList<>(); + for (Automation a : triggeredAutomationsTMP) { + if (conditionRepository.findAllByAutomationId(a.getId()).size() > 0) { + if (automationsWithMetConditions.contains(a)) { + triggeredAutomations.add(a); + } + } else { + triggeredAutomations.add(a); + } + } + + triggeredAutomations + .stream() .distinct() .map(Automation::getScenes) .flatMap(Collection::stream) @@ -126,7 +191,9 @@ public class DeviceService { throws NotFoundException { final User currentUser = userRepository.findByUsername(guestUsername); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); - if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); + if (!host.getGuests().contains(currentUser)) { + throw new NotFoundException(); + } renameIfDuplicate(device, host.getUsername()); device = deviceRepository.save(device);