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

io.github.zeroone3010.yahueapi.BasicSensor 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.domain.SensorDto;

import java.net.URL;
import java.time.ZonedDateTime;
import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Logger;

class BasicSensor implements Sensor {
  private static final Logger logger = Logger.getLogger("io.github.zeroone3010.yahueapi");
  private static final String NO_LAST_UPDATED_DATA = "none";

  protected final String id;
  protected final String name;
  protected final String productName;
  protected final URL baseUrl;
  protected final SensorType type;
  private final Supplier> stateProvider;

  BasicSensor(final String id, final SensorDto sensor, final URL url, final Supplier> stateProvider) {
    this.id = id;
    if (sensor == null) {
      throw new HueApiException("Sensor " + id + " cannot be found.");
    }
    this.name = sensor.getName();
    this.productName = sensor.getProductName();
    this.baseUrl = url;
    this.stateProvider = stateProvider;
    this.type = SensorType.parseTypeString(sensor.getType());
  }

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

  @Override
  public String getProductName() {
    return productName;
  }

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

  @Override
  public SensorType getType() {
    return type;
  }

  @Override
  public ZonedDateTime getLastUpdated() {
    final String lastUpdated = readStateValue("lastupdated", String.class);
    if (NO_LAST_UPDATED_DATA.equals(lastUpdated)) {
      return null;
    }
    return TimeUtil.stringTimestampToZonedDateTime(lastUpdated);
  }

  protected  T readStateValue(final String stateValueKey, final Class type) {
    final Map state = stateProvider.get();
      logger.fine(state.toString());
      return type.cast(state.get(stateValueKey));
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy