Skip to content

Commit

Permalink
Fix NAN being serialized as int
Browse files Browse the repository at this point in the history
Refs: #52
  • Loading branch information
orontee committed Oct 5, 2023
1 parent 8060df1 commit 4c54668
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions src/service.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "service.h"

#include "errors.h"
#include "logging.h"

namespace taranis {

Expand All @@ -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);

Expand Down Expand Up @@ -67,6 +70,8 @@ std::vector<Alert> Service::get_alerts() const {

std::pair<long double, long double>
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<time_t>(value.get("dt", 0).asInt());
const auto sunrise = static_cast<time_t>(value.get("sunrise", 0).asInt());
const auto sunset = static_cast<time_t>(value.get("sunset", 0).asInt());
BOOST_LOG_TRIVIAL(debug) << "Extracting weather condition from JSON value";

const auto date = static_cast<time_t>(value.get("dt", 0).asLargestInt());
const auto sunrise = static_cast<time_t>(value.get("sunrise", 0).asLargestInt());
const auto sunset = static_cast<time_t>(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,
Expand Down Expand Up @@ -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<time_t>(value.get("dt", 0).asInt());
const auto sunrise = static_cast<time_t>(value.get("sunrise", 0).asInt());
const auto sunset = static_cast<time_t>(value.get("sunset", 0).asInt());
const auto moonrise = static_cast<time_t>(value.get("moonrise", 0).asInt());
const auto moonset = static_cast<time_t>(value.get("moonset", 0).asInt());
BOOST_LOG_TRIVIAL(debug) << "Extracting daily condition from JSON value";

const auto date = static_cast<time_t>(value.get("dt", 0).asLargestInt());
const auto sunrise = static_cast<time_t>(value.get("sunrise", 0).asLargestInt());
const auto sunset = static_cast<time_t>(value.get("sunset", 0).asLargestInt());
const auto moonrise = static_cast<time_t>(value.get("moonrise", 0).asLargestInt());
const auto moonset = static_cast<time_t>(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();
Expand Down Expand Up @@ -251,13 +264,15 @@ DailyCondition Service::extract_daily_condition(const Json::Value &value) {
}

std::vector<Alert> Service::extract_alerts(const Json::Value &value) {
BOOST_LOG_TRIVIAL(debug) << "Extracting alerts from JSON value";

std::vector<Alert> alerts;

for (auto &alert_value : value) {
const Alert alert{alert_value.get("sender_name", "").asString(),
alert_value.get("event", "").asString(),
static_cast<time_t>(alert_value.get("start", 0).asInt()),
static_cast<time_t>(alert_value.get("end", 0).asInt()),
static_cast<time_t>(alert_value.get("start", 0).asLargestInt()),
static_cast<time_t>(alert_value.get("end", 0).asLargestInt()),
alert_value.get("description", "").asString()};
alerts.push_back(alert);
}
Expand Down

0 comments on commit 4c54668

Please sign in to comment.