<commit_before>/*
 * Copyright 2016 The Winston Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.s13g.winston.lib.nest;

import com.s13g.winston.lib.nest.data.HvacState;
import com.s13g.winston.lib.nest.data.ThermostatData;
import com.s13g.winston.lib.temperature.Temperature;

import java.util.Optional;

/**
 * A nest thermostat that can be queried to the latest data.
 */
public class Thermostat {
  private final String mId;
  private final NestController mNestController;

  private boolean mLastRefreshSuccess;
  private long mLastRefreshTime;
  private String mName;
  private Temperature mAmbientTemperature;
  private Temperature mTargetTemperature;
  private float mHumidty;
  private HvacState mHvacState;

  public Thermostat(String id, NestController nestController) {
    mId = id;
    mNestController = nestController;
  }

  /**
   * Refreshes the thermostat if the last refresh is older than the given age.
   *
   * @param maxAgeMillis if the thermostat data has been refreshed less than this time ago, it will
   * not be refreshed again.
   */
  public Thermostat refresh(long maxAgeMillis) {
    boolean dataExpired = mLastRefreshTime + maxAgeMillis < System.currentTimeMillis();
    if (maxAgeMillis < 0 || !dataExpired) {
      return this;
    }
    mNestController.refresh();
    mLastRefreshSuccess = false;
    for (ThermostatData data : mNestController.getThermostats()) {
      if (data.id.equals(mId)) {
        mName = data.name;
        mAmbientTemperature = data.ambientTemperature;
        mTargetTemperature = data.targetTemperature;
        mHumidty = (float) data.humidity;
        mHvacState = data.hvacState;
        mLastRefreshSuccess = true;
        mLastRefreshTime = System.currentTimeMillis();
        break;
      }
    }
    return this;
  }

  public Optional<String> getName() {
    return returnIfRefreshSuccessful(mName);
  }

  public Optional<Temperature> getAmbientTemperature() {
    return returnIfRefreshSuccessful(mAmbientTemperature);
  }

  public Optional<Temperature> getTargetTemperature() {
    return returnIfRefreshSuccessful(mTargetTemperature);
  }

  public void setTargetTemperature(Temperature temperature) {
    mNestController.setTemperature(mId, temperature);
  }

  public Optional<Float> getHumidity() {
    return returnIfRefreshSuccessful(mHumidty);
  }

  public Optional<HvacState> getHvacState() {
    return returnIfRefreshSuccessful(mHvacState);
  }

  private <T> Optional<T> returnIfRefreshSuccessful(T value) {
    if (!mLastRefreshSuccess) {
      return Optional.empty();
    }
    return Optional.of(value);
  }
}<commit_msg>Refactor of Thermostat class, also added isOnline getter.<commit_after>/*
 * Copyright 2016 The Winston Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.s13g.winston.lib.nest;

import com.s13g.winston.lib.nest.data.HvacState;
import com.s13g.winston.lib.nest.data.ThermostatData;
import com.s13g.winston.lib.temperature.Temperature;

import java.util.Optional;

/**
 * A nest thermostat that can be queried to the latest data.
 */
public class Thermostat {
  private final String mId;
  private final NestController mNestController;

  private boolean mLastRefreshSuccess;
  private long mLastRefreshTime;
  private ThermostatData mLatestData;

  public Thermostat(String id, NestController nestController) {
    mId = id;
    mNestController = nestController;
  }

  /**
   * Refreshes the thermostat if the last refresh is older than the given age.
   *
   * @param maxAgeMillis if the thermostat data has been refreshed less than this time ago, it will
   * not be refreshed again.
   */
  public Thermostat refresh(long maxAgeMillis) {
    boolean dataExpired = mLastRefreshTime + maxAgeMillis < System.currentTimeMillis();
    if (maxAgeMillis < 0 || !dataExpired) {
      return this;
    }
    mNestController.refresh();
    mLastRefreshSuccess = false;
    for (ThermostatData data : mNestController.getThermostats()) {
      if (data.id.equals(mId)) {
        mLatestData = data;
        mLastRefreshSuccess = true;
        mLastRefreshTime = System.currentTimeMillis();
        break;
      }
    }
    return this;
  }

  public Optional<String> getName() {
    return returnIfRefreshSuccessful(mLatestData.name);
  }

  public Optional<Temperature> getAmbientTemperature() {
    return returnIfRefreshSuccessful(mLatestData.ambientTemperature);
  }

  public Optional<Temperature> getTargetTemperature() {
    return returnIfRefreshSuccessful(mLatestData.targetTemperature);
  }

  public void setTargetTemperature(Temperature temperature) {
    mNestController.setTemperature(mId, temperature);
  }

  public Optional<Float> getHumidity() {
    return returnIfRefreshSuccessful((float) mLatestData.humidity);
  }

  public Optional<HvacState> getHvacState() {
    return returnIfRefreshSuccessful(mLatestData.hvacState);
  }

  public Optional<Boolean> getIsOnline() {
    return returnIfRefreshSuccessful(mLatestData.isOnline);
  }

  private <T> Optional<T> returnIfRefreshSuccessful(T value) {
    if (!mLastRefreshSuccess) {
      return Optional.empty();
    }
    return Optional.of(value);
  }
}
{'fraction_non_alphanumeric': 0.08208594881699662, 'fraction_numerical': 0.005472396587799775, 'mean_word_length': 3.8170542635658915, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373802', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1805, 'n_words': 518}