<commit_before>/*
 Pricing.cpp - Class to obtain and store energy prices
 Copyright (c) 2016 <NAME>.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <Arduino.h>
#include "Pricing.h"
#include <ESP8266HTTPClient.h>
#include "../Clock.h"
#include "../config.h"
#include "ArduinoJson.h"

extern Config* config;

#define DEBUG_PRICING false

bool Pricing::getTomorrowPrices(unsigned long date)
{
  HTTPClient http;
  String url(config->pricing_address);
  unsigned long tomorrow = date+86400; // 86400s = 1 dia
  url.concat(Clock::getHumanDate(tomorrow));
  if (!http.begin(url.c_str()))
    return false;

  int httpCode = http.GET();
  if(httpCode != HTTP_CODE_OK)
    return false;

  String body = http.getString();
  http.end();

  DynamicJsonBuffer jsonBuffer;
  JsonArray& json = jsonBuffer.parseArray(body);

  if (!json.success() || json.size() != 24)
    return false;

  // We save remaining days from the day before (today)
  // as it must be at least 20:00 we only need lasts 3 prices
  for (int i = 0; i < 4; i++)
    _price[i] = _price[i+24];

  // 4-27 are the actual prices we are saving
  for (int i = 0; i < 24; i++)
    _price[i+3] = (uint8_t)(json.get<uint32_t>(i)/100);


  lastUpdate = date+86400; // 86400s = 1 dia
  _date = tomorrow;
  return true;
}

bool Pricing::getTodayPrices(unsigned long date)
{
  HTTPClient http;
  String url(config->pricing_address);
  url.concat(Clock::getHumanDate(date));

  if (!http.begin(url.c_str()))
    return false;

  int httpCode = http.GET();
  if(httpCode != HTTP_CODE_OK)
    return false;

  String body = http.getString();
  http.end();

  DynamicJsonBuffer jsonBuffer;
  JsonArray& json = jsonBuffer.parseArray(body);

  if (!json.success() || json.size() != 24)
    return false;

  // 4-27 are the actual prices we are saving
  // 0-3 are prices of the day before
  for (int i = 0; i < 24; i++)
  {
    _price[i+3] = (uint8_t)(json.get<uint32_t>(i)/100);
    Serial.println(_price[i+3]);
  }

  lastUpdate = date;
  _date = date;
  return true;
}

bool Pricing::update()
{
  unsigned long today = Clock::getDayInSeconds();

  if (lastUpdate > today)
    return true; // we already have tomorrow prices (nothing to do)

  if (lastUpdate < today)
  {
    if (!getTodayPrices(today))
      return false;
  }

  // We should be able to obtain tomorrow prices after 20:00
  if ((Clock::getUnixTime() % 86400L) / 3600 > 20)
  {
    getTomorrowPrices(today);
  }

  return true;<commit_msg>Remove debug print<commit_after>/*
 Pricing.cpp - Class to obtain and store energy prices
 Copyright (c) 2016 <NAME>.  All right reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <Arduino.h>
#include "Pricing.h"
#include <ESP8266HTTPClient.h>
#include "../Clock.h"
#include "../config.h"
#include "ArduinoJson.h"

extern Config* config;

#define DEBUG_PRICING false

bool Pricing::getTomorrowPrices(unsigned long date)
{
  HTTPClient http;
  String url(config->pricing_address);
  unsigned long tomorrow = date+86400; // 86400s = 1 dia
  url.concat(Clock::getHumanDate(tomorrow));
  if (!http.begin(url.c_str()))
    return false;

  int httpCode = http.GET();
  if(httpCode != HTTP_CODE_OK)
    return false;

  String body = http.getString();
  http.end();

  DynamicJsonBuffer jsonBuffer;
  JsonArray& json = jsonBuffer.parseArray(body);

  if (!json.success() || json.size() != 24)
    return false;

  // We save remaining days from the day before (today)
  // as it must be at least 20:00 we only need lasts 3 prices
  for (int i = 0; i < 4; i++)
    _price[i] = _price[i+24];

  // 4-27 are the actual prices we are saving
  for (int i = 0; i < 24; i++)
    _price[i+3] = (uint8_t)(json.get<uint32_t>(i)/100);


  lastUpdate = date+86400; // 86400s = 1 dia
  _date = tomorrow;
  return true;
}

bool Pricing::getTodayPrices(unsigned long date)
{
  HTTPClient http;
  String url(config->pricing_address);
  url.concat(Clock::getHumanDate(date));

  if (!http.begin(url.c_str()))
    return false;

  int httpCode = http.GET();
  if(httpCode != HTTP_CODE_OK)
    return false;

  String body = http.getString();
  http.end();

  DynamicJsonBuffer jsonBuffer;
  JsonArray& json = jsonBuffer.parseArray(body);

  if (!json.success() || json.size() != 24)
    return false;

  // 4-27 are the actual prices we are saving
  // 0-3 are prices of the day before
  for (int i = 0; i < 24; i++)
    _price[i+3] = (uint8_t)(json.get<uint32_t>(i)/100);

  lastUpdate = date;
  _date = date;
  return true;
}

bool Pricing::update()
{
  unsigned long today = Clock::getDayInSeconds();

  if (lastUpdate > today)
    return true; // we already have tomorrow prices (nothing to do)

  if (lastUpdate < today)
  {
    if (!getTodayPrices(today))
      return false;
  }

  // We should be able to obtain tomorrow prices after 20:00
  if ((Clock::getUnixTime() % 86400L) / 3600 > 20)
  {
    getTomorrowPrices(today);
  }

  return true;
{'fraction_non_alphanumeric': 0.11513893324816353, 'fraction_numerical': 0.03177898435004791, 'mean_word_length': 3.5815654718361376, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '782687', 'n_tokens_mistral': 2195, 'n_tokens_neox': 1981, 'n_words': 718}