All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.zeroone3010.yahueapi.RoomImpl Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc
Show newest version
package io.github.zeroone3010.yahueapi;

import io.github.zeroone3010.yahueapi.StateBuilderSteps.BrightnessStep;
import io.github.zeroone3010.yahueapi.domain.Group;
import io.github.zeroone3010.yahueapi.domain.GroupState;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.logging.Logger;

import static java.util.stream.Collectors.toSet;

final class RoomImpl implements Room {
  private static final Logger logger = Logger.getLogger("io.github.zeroone3010.yahueapi");

  private final String id;
  private final Supplier> lights;
  private final Set scenes;
  private final String name;
  private final Supplier stateProvider;
  private final Function stateSetter;
  private final GroupType groupType;
  private final Function, String> lightsSetter;

  RoomImpl(final String id,
           final Group group,
           final Supplier> lights,
           final Set scenes,
           final Supplier stateProvider,
           final Function stateSetter,
           final Function, String> lightsSetter) {
    this.id = id;
    this.stateProvider = stateProvider;
    this.stateSetter = stateSetter;
    this.lights = lights;
    this.scenes = scenes;
    this.name = group.getName();
    this.groupType = GroupType.parseTypeString(group.getType());
    this.lightsSetter = lightsSetter;
  }

  @Override
  public String getId() {
    return id;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public Collection getLights() {
    return lights.get();
  }

  @Override
  public Optional getLightByName(final String lightName) {
    return lights.get().stream()
        .filter(light -> Objects.equals(light.getName(), lightName))
        .findFirst();
  }

  @Override
  public boolean isAnyOn() {
    return getGroupState().isAnyOn();
  }

  @Override
  public boolean isAllOn() {
    return getGroupState().isAllOn();
  }

  @Override
  public void turnOn() {
    setState(((StateBuilderSteps.OnOffStep) State.builder()).on());
  }

  @Override
  public void turnOff() {
    setState(((StateBuilderSteps.OnOffStep) State.builder()).off());
  }

  @Override
  public void setState(final State state) {
    final String result = stateSetter.apply(state);
    logger.fine(result);
  }

  @Override
  public void setBrightness(final int brightness) {
    setState(((BrightnessStep) State.builder()).brightness(brightness).keepCurrentState());
  }

  @Override
  public GroupType getType() {
    return groupType;
  }

  @Override
  public Collection getScenes() {
    return scenes;
  }

  @Override
  public Optional getSceneByName(final String sceneName) {
    return scenes.stream()
        .filter(scene -> Objects.equals(scene.getName(), sceneName))
        .findFirst();
  }

  private GroupState getGroupState() {
    return stateProvider.get();
  }

  @Override
  public Collection addLight(final Light newLight) {
    if (newLight == null || newLight.getId() == null) {
      logger.warning("addLight: Given light was null. Doing nothing.");
      return getLights();
    }

    final Set lightIds = new HashSet<>(getLights().stream().map(Light::getId).collect(toSet()));
    lightIds.add(newLight.getId());
    final String result = lightsSetter.apply(lightIds);
    logger.info(result);
    return getLights();
  }

  @Override
  public Collection removeLight(final Light lightToBeRemoved) {
    if (lightToBeRemoved == null || lightToBeRemoved.getId() == null) {
      logger.warning("removeLight: Given light was null. Doing nothing.");
      return getLights();
    }
    final Set lightIds = new HashSet<>(getLights().stream().map(Light::getId).collect(toSet()));
    lightIds.remove(lightToBeRemoved.getId());
    final String result = lightsSetter.apply(lightIds);
    logger.fine(result);
    return getLights();
  }

  @Override
  public String toString() {
    return "Group{" +
        "id='" + id + '\'' +
        ", name='" + name + '\'' +
        ", type='" + groupType + '\'' +
        '}';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy