diff --git a/src/service.cc b/src/service.cc index ff50562..4bf83b4 100644 --- a/src/service.cc +++ b/src/service.cc @@ -1,6 +1,7 @@ #include "service.h" #include "errors.h" +#include "logging.h" namespace taranis { @@ -13,6 +14,8 @@ const std::string onecall_path{"/data/3.0/onecall"}; void Service::fetch_data(const std::string &town, const std::string &country, const std::string &language, const std::string &units) { + BOOST_LOG_TRIVIAL(debug) << "Fetching weather data"; + this->returned_value = this->request_onecall_api(town, country, language, units); @@ -67,6 +70,8 @@ std::vector Service::get_alerts() const { std::pair Service::identify_lonlat(const std::string &town, const std::string &country) { + BOOST_LOG_TRIVIAL(debug) << "Identifying longitude and latitude"; + if (this->town != town or this->country != country) { this->town = town; this->country = country; @@ -79,6 +84,8 @@ Service::identify_lonlat(const std::string &town, const std::string &country) { } void Service::request_lonlat() { + BOOST_LOG_TRIVIAL(debug) << "Requesting Geocoding API"; + std::stringstream url; url << openweather::url << openweather::geo_path << "?" << "q=" << this->town; @@ -106,6 +113,8 @@ Json::Value Service::request_onecall_api(const std::string &town, const std::string &country, const std::string &language, const std::string &units) { + BOOST_LOG_TRIVIAL(debug) << "Requesting Onecall API"; + const auto lonlat = this->identify_lonlat(town, country); std::stringstream url; @@ -133,19 +142,21 @@ Json::Value Service::send_get_request(const std::string &url) { } Condition Service::extract_condition(const Json::Value &value) { - const auto date = static_cast(value.get("dt", 0).asInt()); - const auto sunrise = static_cast(value.get("sunrise", 0).asInt()); - const auto sunset = static_cast(value.get("sunset", 0).asInt()); + BOOST_LOG_TRIVIAL(debug) << "Extracting weather condition from JSON value"; + + const auto date = static_cast(value.get("dt", 0).asLargestInt()); + const auto sunrise = static_cast(value.get("sunrise", 0).asLargestInt()); + const auto sunset = static_cast(value.get("sunset", 0).asLargestInt()); const auto temperature = value.get("temp", NAN).asDouble(); const auto felt_temperature = value.get("feels_like", NAN).asDouble(); - const auto pressure = value.get("pressure", NAN).asInt(); - const auto humidity = value.get("humidity", NAN).asInt(); + const auto pressure = value.get("pressure", 0).asInt(); + const auto humidity = value.get("humidity", 0).asInt(); const auto uv_index = value.get("uvi", NAN).asDouble(); - const auto clouds = value.get("clouds", NAN).asInt(); - const auto visibility = value.get("visibility", NAN).asInt(); + const auto clouds = value.get("clouds", 0).asInt(); + const auto visibility = value.get("visibility", 0).asInt(); const auto probability_of_precipitation = value.get("pop", NAN).asDouble(); const auto wind_speed = value.get("wind_speed", NAN).asDouble(); - const auto wind_degree = value.get("wind_deg", NAN).asInt(); + const auto wind_degree = value.get("wind_deg", 0).asInt(); const auto wind_gust = value.get("wind_gust", NAN).asDouble(); Condition condition{date, @@ -190,19 +201,21 @@ Condition Service::extract_condition(const Json::Value &value) { } DailyCondition Service::extract_daily_condition(const Json::Value &value) { - const auto date = static_cast(value.get("dt", 0).asInt()); - const auto sunrise = static_cast(value.get("sunrise", 0).asInt()); - const auto sunset = static_cast(value.get("sunset", 0).asInt()); - const auto moonrise = static_cast(value.get("moonrise", 0).asInt()); - const auto moonset = static_cast(value.get("moonset", 0).asInt()); + BOOST_LOG_TRIVIAL(debug) << "Extracting daily condition from JSON value"; + + const auto date = static_cast(value.get("dt", 0).asLargestInt()); + const auto sunrise = static_cast(value.get("sunrise", 0).asLargestInt()); + const auto sunset = static_cast(value.get("sunset", 0).asLargestInt()); + const auto moonrise = static_cast(value.get("moonrise", 0).asLargestInt()); + const auto moonset = static_cast(value.get("moonset", 0).asLargestInt()); const auto moon_phase = value.get("moon_phase", NAN).asDouble(); - const auto pressure = value.get("pressure", NAN).asInt(); - const auto humidity = value.get("humidity", NAN).asInt(); + const auto pressure = value.get("pressure", 0).asInt(); + const auto humidity = value.get("humidity", 0).asInt(); const auto dew_point = value.get("dew_point", NAN).asDouble(); const auto wind_speed = value.get("wind_speed", NAN).asDouble(); - const auto wind_degree = value.get("wind_deg", NAN).asInt(); + const auto wind_degree = value.get("wind_deg", 0).asInt(); const auto wind_gust = value.get("wind_gust", NAN).asDouble(); - const auto clouds = value.get("clouds", NAN).asInt(); + const auto clouds = value.get("clouds", 0).asInt(); const auto probability_of_precipitation = value.get("pop", NAN).asDouble(); const auto uv_index = value.get("uvi", NAN).asDouble(); const auto rain = value.get("rain", NAN).asDouble(); @@ -251,13 +264,15 @@ DailyCondition Service::extract_daily_condition(const Json::Value &value) { } std::vector Service::extract_alerts(const Json::Value &value) { + BOOST_LOG_TRIVIAL(debug) << "Extracting alerts from JSON value"; + std::vector alerts; for (auto &alert_value : value) { const Alert alert{alert_value.get("sender_name", "").asString(), alert_value.get("event", "").asString(), - static_cast(alert_value.get("start", 0).asInt()), - static_cast(alert_value.get("end", 0).asInt()), + static_cast(alert_value.get("start", 0).asLargestInt()), + static_cast(alert_value.get("end", 0).asLargestInt()), alert_value.get("description", "").asString()}; alerts.push_back(alert); }