Everything should be implemented
This commit is contained in:
parent
1bde4344be
commit
09c08580ca
2 changed files with 74 additions and 5 deletions
|
@ -7,4 +7,6 @@ import org.springframework.data.repository.query.Param;
|
|||
public interface ConditionRepository<T extends Condition<?>> extends CrudRepository<T, Long> {
|
||||
|
||||
List<T> findAllByDeviceId(@Param("deviceId") long deviceId);
|
||||
|
||||
List<T> findAllByAutomationId(@Param("automationId") long automationId);
|
||||
}
|
||||
|
|
|
@ -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<Trigger<? extends Device>> triggerRepository;
|
||||
@Autowired private ConditionRepository<Condition<? extends Device>> 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<Condition<? extends Device>> triggeredConditions =
|
||||
conditionRepository
|
||||
.findAllByDeviceId(deviceId)
|
||||
.stream()
|
||||
.filter(Condition::triggered)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Automation> 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<? extends Device> condition : triggeredConditions) {
|
||||
|
||||
Automation a =
|
||||
automationRepository
|
||||
.findById(condition.getAutomationId())
|
||||
.orElseThrow(IllegalStateException::new);
|
||||
List<Condition<? extends Device>> conditions =
|
||||
conditionRepository.findAllByAutomationId(a.getId());
|
||||
|
||||
if (conditions.size()
|
||||
== conditions
|
||||
.stream()
|
||||
.filter(Condition::triggered)
|
||||
.collect(Collectors.toList())
|
||||
.size()) {
|
||||
automationsWithMetConditions.add(a);
|
||||
}
|
||||
}
|
||||
|
||||
List<Trigger<? extends Device>> triggers = triggerRepository.findAllByDeviceId(deviceId);
|
||||
|
||||
// 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<Automation> triggeredAutomationsTMP =
|
||||
triggers.stream()
|
||||
.filter(Trigger::triggered)
|
||||
.map(Trigger::getAutomationId)
|
||||
.map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new))
|
||||
.map(
|
||||
t ->
|
||||
automationRepository
|
||||
.findById(t)
|
||||
.orElseThrow(IllegalStateException::new))
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Automation> 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);
|
||||
|
|
Loading…
Reference in a new issue