:3
This commit is contained in:
parent
94d08a02d6
commit
4d1e69bbe5
8 changed files with 167 additions and 133 deletions
|
@ -1,7 +1,7 @@
|
|||
#include "config.h"
|
||||
|
||||
#define DEFINE_GETTER(class_name, type, name) \
|
||||
type class_name::get##name() const { return m_##name; }
|
||||
fn class_name::get##name() const->type { return m_##name; }
|
||||
|
||||
DEFINE_GETTER(Config, const General, General)
|
||||
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
|
||||
|
@ -12,7 +12,7 @@ DEFINE_GETTER(Weather, const Weather::Location, Location)
|
|||
DEFINE_GETTER(Weather, const std::string, ApiKey)
|
||||
DEFINE_GETTER(Weather, const std::string, Units)
|
||||
|
||||
const Config& Config::getInstance() {
|
||||
fn Config::getInstance() -> const Config& {
|
||||
static const auto* INSTANCE =
|
||||
new Config(rfl::toml::load<Config>("./config.toml").value());
|
||||
return *INSTANCE;
|
||||
|
@ -25,14 +25,14 @@ Config::Config(General general, NowPlaying now_playing, Weather weather)
|
|||
|
||||
General::General(std::string name) : m_Name(std::move(name)) {}
|
||||
|
||||
NowPlaying::NowPlaying(bool enable) : m_Enabled(enable) {}
|
||||
NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {}
|
||||
|
||||
Weather::Weather(Location location, std::string api_key, std::string units)
|
||||
: m_Location(std::move(location)),
|
||||
m_ApiKey(std::move(api_key)),
|
||||
m_Units(std::move(units)) {}
|
||||
|
||||
WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
|
||||
fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
|
||||
return {
|
||||
.location = weather.getLocation(),
|
||||
.api_key = weather.getApiKey(),
|
||||
|
@ -40,22 +40,27 @@ WeatherImpl WeatherImpl::from_class(const Weather& weather) noexcept {
|
|||
};
|
||||
}
|
||||
|
||||
Weather WeatherImpl::to_class() const { return {location, api_key, units}; }
|
||||
fn WeatherImpl::to_class() const -> Weather {
|
||||
return {location, api_key, units};
|
||||
}
|
||||
|
||||
GeneralImpl GeneralImpl::from_class(const General& general) noexcept {
|
||||
fn GeneralImpl::from_class(const General& general) noexcept -> GeneralImpl {
|
||||
return {general.getName()};
|
||||
}
|
||||
|
||||
General GeneralImpl::to_class() const { return {name}; }
|
||||
fn GeneralImpl::to_class() const -> General { return {name}; }
|
||||
|
||||
NowPlayingImpl NowPlayingImpl::from_class(
|
||||
const NowPlaying& now_playing) noexcept {
|
||||
// clang-format off
|
||||
fn NowPlayingImpl::from_class(
|
||||
const NowPlaying& now_playing
|
||||
) noexcept -> NowPlayingImpl {
|
||||
return {.enabled = now_playing.getEnabled()};
|
||||
}
|
||||
//clang-format on
|
||||
|
||||
NowPlaying NowPlayingImpl::to_class() const { return {enabled}; }
|
||||
fn NowPlayingImpl::to_class() const -> NowPlaying { return {enabled}; }
|
||||
|
||||
ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
|
||||
fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
|
||||
return {
|
||||
.general = config.getGeneral(),
|
||||
.now_playing = config.getNowPlaying(),
|
||||
|
@ -63,4 +68,6 @@ ConfigImpl ConfigImpl::from_class(const Config& config) noexcept {
|
|||
};
|
||||
}
|
||||
|
||||
Config ConfigImpl::to_class() const { return {general, now_playing, weather}; }
|
||||
fn ConfigImpl::to_class() const -> Config {
|
||||
return {general, now_playing, weather};
|
||||
}
|
||||
|
|
|
@ -10,30 +10,30 @@
|
|||
|
||||
class Weather {
|
||||
public:
|
||||
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
|
||||
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
|
||||
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
|
||||
|
||||
struct Condition {
|
||||
usize id;
|
||||
rfl::Rename<"main", std::string> group;
|
||||
std::string description;
|
||||
rfl::Rename<"icon", std::string> icon_id;
|
||||
std::string icon;
|
||||
std::string main;
|
||||
usize id;
|
||||
};
|
||||
|
||||
struct Main {
|
||||
f64 temp;
|
||||
f64 temp_max;
|
||||
f64 temp_min;
|
||||
f64 feels_like;
|
||||
isize pressure;
|
||||
std::optional<isize> sea_level;
|
||||
f64 feels_like;
|
||||
f64 temp;
|
||||
f64 temp_max;
|
||||
f64 temp_min;
|
||||
isize pressure;
|
||||
percentage humidity;
|
||||
std::optional<isize> grnd_level;
|
||||
percentage humidity;
|
||||
std::optional<isize> sea_level;
|
||||
};
|
||||
|
||||
struct Wind {
|
||||
f64 speed;
|
||||
degrees deg;
|
||||
degrees deg;
|
||||
f64 speed;
|
||||
std::optional<f64> gust;
|
||||
};
|
||||
|
||||
|
@ -44,10 +44,10 @@ class Weather {
|
|||
|
||||
struct Sys {
|
||||
std::string country;
|
||||
usize id;
|
||||
usize sunrise;
|
||||
usize sunset;
|
||||
usize type;
|
||||
usize id;
|
||||
usize sunrise;
|
||||
usize sunset;
|
||||
usize type;
|
||||
};
|
||||
|
||||
struct Clouds {
|
||||
|
@ -60,47 +60,47 @@ class Weather {
|
|||
};
|
||||
|
||||
struct WeatherOutput {
|
||||
isize timezone;
|
||||
isize visibility;
|
||||
Main main;
|
||||
Clouds clouds;
|
||||
Clouds clouds;
|
||||
Main main;
|
||||
Sys sys;
|
||||
Wind wind;
|
||||
isize timezone;
|
||||
isize visibility;
|
||||
rfl::Rename<"coord", Coords> coords;
|
||||
std::optional<Precipitation> rain;
|
||||
std::optional<Precipitation> snow;
|
||||
std::vector<Condition> weather;
|
||||
std::string base;
|
||||
std::string name;
|
||||
Sys sys;
|
||||
usize cod;
|
||||
usize dt;
|
||||
usize id;
|
||||
Wind wind;
|
||||
std::string base;
|
||||
std::string name;
|
||||
std::vector<Condition> weather;
|
||||
usize cod;
|
||||
usize dt;
|
||||
usize id;
|
||||
};
|
||||
|
||||
using Location = std::variant<std::string, Coords>;
|
||||
|
||||
private:
|
||||
Location m_Location;
|
||||
Location m_Location;
|
||||
std::string m_ApiKey;
|
||||
std::string m_Units;
|
||||
|
||||
public:
|
||||
Weather(Location location, std::string api_key, std::string units);
|
||||
|
||||
[[nodiscard]] WeatherOutput getWeatherInfo() const;
|
||||
[[nodiscard]] const Location getLocation() const;
|
||||
[[nodiscard]] const std::string getApiKey() const;
|
||||
[[nodiscard]] const std::string getUnits() const;
|
||||
[[nodiscard]] fn getWeatherInfo() const -> WeatherOutput;
|
||||
[[nodiscard]] fn getLocation() const -> const Location;
|
||||
[[nodiscard]] fn getApiKey() const -> const std::string;
|
||||
[[nodiscard]] fn getUnits() const -> const std::string;
|
||||
};
|
||||
|
||||
struct WeatherImpl {
|
||||
Weather::Location location;
|
||||
std::string api_key;
|
||||
std::string units;
|
||||
std::string api_key;
|
||||
std::string units;
|
||||
|
||||
static WeatherImpl from_class(const Weather& weather) noexcept;
|
||||
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
|
||||
|
||||
[[nodiscard]] Weather to_class() const;
|
||||
[[nodiscard]] fn to_class() const -> Weather;
|
||||
};
|
||||
|
||||
class General {
|
||||
|
@ -110,15 +110,15 @@ class General {
|
|||
public:
|
||||
General(std::string name);
|
||||
|
||||
[[nodiscard]] const std::string getName() const;
|
||||
[[nodiscard]] fn getName() const -> const std::string;
|
||||
};
|
||||
|
||||
struct GeneralImpl {
|
||||
std::string name;
|
||||
|
||||
static GeneralImpl from_class(const General& general) noexcept;
|
||||
static fn from_class(const General& general) noexcept -> GeneralImpl;
|
||||
|
||||
[[nodiscard]] General to_class() const;
|
||||
[[nodiscard]] fn to_class() const -> General;
|
||||
};
|
||||
|
||||
class NowPlaying {
|
||||
|
@ -128,74 +128,79 @@ class NowPlaying {
|
|||
public:
|
||||
NowPlaying(bool enabled);
|
||||
|
||||
[[nodiscard]] bool getEnabled() const;
|
||||
[[nodiscard]] fn getEnabled() const -> bool;
|
||||
};
|
||||
|
||||
struct NowPlayingImpl {
|
||||
bool enabled;
|
||||
|
||||
static NowPlayingImpl from_class(const NowPlaying& now_playing) noexcept;
|
||||
static fn from_class(const NowPlaying& now_playing
|
||||
) noexcept -> NowPlayingImpl;
|
||||
|
||||
[[nodiscard]] NowPlaying to_class() const;
|
||||
[[nodiscard]] fn to_class() const -> NowPlaying;
|
||||
};
|
||||
|
||||
class Config {
|
||||
private:
|
||||
General m_General;
|
||||
General m_General;
|
||||
NowPlaying m_NowPlaying;
|
||||
Weather m_Weather;
|
||||
Weather m_Weather;
|
||||
|
||||
public:
|
||||
Config(General general, NowPlaying now_playing, Weather weather);
|
||||
|
||||
static const Config& getInstance();
|
||||
static fn getInstance() -> const Config&;
|
||||
|
||||
[[nodiscard]] const Weather getWeather() const;
|
||||
[[nodiscard]] const General getGeneral() const;
|
||||
[[nodiscard]] const NowPlaying getNowPlaying() const;
|
||||
[[nodiscard]] fn getWeather() const -> const Weather;
|
||||
[[nodiscard]] fn getGeneral() const -> const General;
|
||||
[[nodiscard]] fn getNowPlaying() const -> const NowPlaying;
|
||||
};
|
||||
|
||||
struct ConfigImpl {
|
||||
General general;
|
||||
General general;
|
||||
NowPlaying now_playing;
|
||||
Weather weather;
|
||||
Weather weather;
|
||||
|
||||
static ConfigImpl from_class(const Config& config) noexcept;
|
||||
static fn from_class(const Config& config) noexcept -> ConfigImpl;
|
||||
|
||||
[[nodiscard]] Config to_class() const;
|
||||
[[nodiscard]] fn to_class() const -> Config;
|
||||
};
|
||||
|
||||
// Parsers for Config classes
|
||||
namespace rfl::parsing {
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
|
||||
: public CustomParser<ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
Weather,
|
||||
WeatherImpl> {};
|
||||
: public CustomParser<
|
||||
ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
Weather,
|
||||
WeatherImpl> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, General, ProcessorsType>
|
||||
: public CustomParser<ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
General,
|
||||
GeneralImpl> {};
|
||||
: public CustomParser<
|
||||
ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
General,
|
||||
GeneralImpl> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, NowPlaying, ProcessorsType>
|
||||
: public CustomParser<ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
NowPlaying,
|
||||
NowPlayingImpl> {};
|
||||
: public CustomParser<
|
||||
ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
NowPlaying,
|
||||
NowPlayingImpl> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, Config, ProcessorsType>
|
||||
: public CustomParser<ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
Config,
|
||||
ConfigImpl> {};
|
||||
: public CustomParser<
|
||||
ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
Config,
|
||||
ConfigImpl> {};
|
||||
} // namespace rfl::parsing
|
||||
|
|
|
@ -8,11 +8,12 @@
|
|||
using WeatherOutput = Weather::WeatherOutput;
|
||||
|
||||
// Function to read cache from file
|
||||
Result<WeatherOutput> ReadCacheFromFile() {
|
||||
fn ReadCacheFromFile() -> Result<WeatherOutput> {
|
||||
const std::string cacheFile = "/tmp/weather_cache.json";
|
||||
std::ifstream ifs(cacheFile);
|
||||
std::ifstream ifs(cacheFile);
|
||||
|
||||
if (!ifs.is_open()) return Error("Cache file not found.");
|
||||
if (!ifs.is_open())
|
||||
return Error("Cache file not found.");
|
||||
|
||||
fmt::println("Reading from cache file...");
|
||||
|
||||
|
@ -32,12 +33,13 @@ Result<WeatherOutput> ReadCacheFromFile() {
|
|||
}
|
||||
|
||||
// Function to write cache to file
|
||||
Result<> WriteCacheToFile(const WeatherOutput& data) {
|
||||
fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
|
||||
const std::string cacheFile = "/tmp/weather_cache.json";
|
||||
fmt::println("Writing to cache file...");
|
||||
std::ofstream ofs(cacheFile);
|
||||
|
||||
if (!ofs.is_open()) return Error("Failed to open cache file for writing.");
|
||||
if (!ofs.is_open())
|
||||
return Error("Failed to open cache file for writing.");
|
||||
|
||||
ofs << rfl::json::write(data);
|
||||
|
||||
|
@ -46,10 +48,8 @@ Result<> WriteCacheToFile(const WeatherOutput& data) {
|
|||
return Ok();
|
||||
}
|
||||
|
||||
size_t WriteCallback(void* contents,
|
||||
size_t size,
|
||||
size_t nmemb,
|
||||
std::string* buffer) {
|
||||
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* buffer)
|
||||
-> size_t {
|
||||
size_t realsize = size * nmemb;
|
||||
|
||||
buffer->append(static_cast<char*>(contents), realsize);
|
||||
|
@ -58,7 +58,7 @@ size_t WriteCallback(void* contents,
|
|||
}
|
||||
|
||||
// Function to make API request
|
||||
Result<WeatherOutput> MakeApiRequest(const std::string& url) {
|
||||
fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
|
||||
fmt::println("Making API request...");
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
|
@ -73,8 +73,9 @@ Result<WeatherOutput> MakeApiRequest(const std::string& url) {
|
|||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res != CURLE_OK)
|
||||
return Error(fmt::format("Failed to perform cURL request: {}",
|
||||
curl_easy_strerror(res)));
|
||||
return Error(fmt::format(
|
||||
"Failed to perform cURL request: {}", curl_easy_strerror(res)
|
||||
));
|
||||
|
||||
fmt::println("Received response from API.");
|
||||
// Parse the JSON response
|
||||
|
@ -88,10 +89,10 @@ Result<WeatherOutput> MakeApiRequest(const std::string& url) {
|
|||
}
|
||||
|
||||
// Core function to get weather information
|
||||
WeatherOutput Weather::getWeatherInfo() const {
|
||||
fn Weather::getWeatherInfo() const -> WeatherOutput {
|
||||
using namespace std::chrono;
|
||||
|
||||
const Location loc = m_Location;
|
||||
const Location loc = m_Location;
|
||||
const std::string apiKey = m_ApiKey;
|
||||
const std::string units = m_Units;
|
||||
|
||||
|
@ -116,15 +117,19 @@ WeatherOutput Weather::getWeatherInfo() const {
|
|||
if (holds_alternative<std::string>(loc)) {
|
||||
const std::string city = get<std::string>(loc);
|
||||
|
||||
const char* location = curl_easy_escape(nullptr, city.c_str(),
|
||||
static_cast<int>(city.length()));
|
||||
const char* location = curl_easy_escape(
|
||||
nullptr, city.c_str(), static_cast<int>(city.length())
|
||||
);
|
||||
|
||||
fmt::println("City: {}", location);
|
||||
|
||||
const std::string apiUrl = fmt::format(
|
||||
"https://api.openweathermap.org/data/2.5/"
|
||||
"weather?q={}&appid={}&units={}",
|
||||
location, apiKey, units);
|
||||
location,
|
||||
apiKey,
|
||||
units
|
||||
);
|
||||
|
||||
result = MakeApiRequest(apiUrl).value();
|
||||
} else {
|
||||
|
@ -135,7 +140,11 @@ WeatherOutput Weather::getWeatherInfo() const {
|
|||
const std::string apiUrl = fmt::format(
|
||||
"https://api.openweathermap.org/data/2.5/"
|
||||
"weather?lat={:.3f}&lon={:.3f}&appid={}&units={}",
|
||||
lat, lon, apiKey, units);
|
||||
lat,
|
||||
lon,
|
||||
apiKey,
|
||||
units
|
||||
);
|
||||
|
||||
result = MakeApiRequest(apiUrl).value();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue