Skip to content

Commit

Permalink
fix: use id and improve naming (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKoepke authored Jan 8, 2025
1 parent f065805 commit 24f3ca6
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public class DeviceService {
private final AnimationRepository animationRepository;
private final Vertx vertx;

private Set<String> notLights;
private Set<String> ignoreLightIdsOrNamesForCentralFunctions;

@PostConstruct
@Transactional
void setNotLights() {
notLights = basicConfigRepository.findByOrderByModifiedDesc()
void setIgnoreLightIdsOrNamesForCentralFunctions() {
ignoreLightIdsOrNamesForCentralFunctions = basicConfigRepository.findByOrderByModifiedDesc()
.map(BasicConfig::getNotLights)
.map(HashSet::new)
.map(HashSet::new)
.orElse(new HashSet<>());
}

public void registerAControlledLight(Device<?> device) {
notLights.add(device.getId());
ignoreLightIdsOrNamesForCentralFunctions.add(device.getId());
}

public <T extends Device<?>> Optional<T> findDeviceByName(String name, Class<T> clazz) {
Expand Down Expand Up @@ -93,14 +93,9 @@ public <T extends Device<?>> Collection<T> getDevicesOfType(Class<T> clazz) {
.collect(Collectors.toSet());
}

@Transactional
public void turnAllLightsOff() {
var notLights = basicConfigRepository.findByOrderByModifiedDesc()
.map(BasicConfig::getNotLights)
.orElse(new HashSet<>());

getDevicesOfType(SimpleLight.class).stream()
.filter(light -> !notLights.contains(light.getName()))
.filter(this::isLightUsableForCentralFunctions)
.filter(Device::isReachable)
.forEach(light -> {
// see #74, if the commands are cumming to fast, then maybe lights are not correctly off
Expand All @@ -119,7 +114,7 @@ public boolean isAnyLightOn() {
return getDevicesOfType(SimpleLight.class)
.stream()
.filter(Device::isReachable)
.filter(light -> !notLights.contains(light.getName()))
.filter(this::isLightUsableForCentralFunctions)
.anyMatch(SimpleLight::isCurrentStateIsOn);
}

Expand Down Expand Up @@ -168,4 +163,9 @@ public void activeSceneForAllGroups(String sceneName) {
.filter(scene -> scene.getName().equals(sceneName))
.forEach(Scene::activate);
}

private boolean isLightUsableForCentralFunctions(SimpleLight light) {
return !ignoreLightIdsOrNamesForCentralFunctions.contains(light.getId())
&& !ignoreLightIdsOrNamesForCentralFunctions.contains(light.getName());
}
}

0 comments on commit 24f3ca6

Please sign in to comment.