some cleanup and stuff
This commit is contained in:
parent
138191227f
commit
afbba70f92
13 changed files with 203 additions and 160 deletions
|
@ -1,32 +1,12 @@
|
|||
#include "config.h"
|
||||
|
||||
#define DEFINE_GETTER(class_name, type, name) \
|
||||
fn class_name::get##name() const->type { return m_##name; }
|
||||
|
||||
DEFINE_GETTER(Config, const General, General)
|
||||
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
|
||||
DEFINE_GETTER(Config, const Weather, Weather)
|
||||
DEFINE_GETTER(General, const std::string, Name)
|
||||
DEFINE_GETTER(NowPlaying, bool, Enabled)
|
||||
// -------------
|
||||
// -- Weather --
|
||||
// -------------
|
||||
DEFINE_GETTER(Weather, const Weather::Location, Location)
|
||||
DEFINE_GETTER(Weather, const std::string, ApiKey)
|
||||
DEFINE_GETTER(Weather, const std::string, Units)
|
||||
|
||||
fn Config::getInstance() -> const Config& {
|
||||
static const auto* INSTANCE =
|
||||
new Config(rfl::toml::load<Config>("./config.toml").value());
|
||||
return *INSTANCE;
|
||||
}
|
||||
|
||||
Config::Config(General general, NowPlaying now_playing, Weather weather)
|
||||
: m_General(std::move(general)),
|
||||
m_NowPlaying(now_playing),
|
||||
m_Weather(std::move(weather)) {}
|
||||
|
||||
General::General(std::string name) : m_Name(std::move(name)) {}
|
||||
|
||||
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)),
|
||||
|
@ -43,22 +23,56 @@ fn WeatherImpl::from_class(const Weather& weather) noexcept -> WeatherImpl {
|
|||
fn WeatherImpl::to_class() const -> Weather {
|
||||
return {location, api_key, units};
|
||||
}
|
||||
// ------------
|
||||
|
||||
// -------------
|
||||
// -- General --
|
||||
// -------------
|
||||
DEFINE_GETTER(General, const std::string, Name)
|
||||
|
||||
General::General(std::string name) : m_Name(std::move(name)) {}
|
||||
|
||||
fn GeneralImpl::from_class(const General& general) noexcept -> GeneralImpl {
|
||||
return {general.getName()};
|
||||
}
|
||||
|
||||
fn GeneralImpl::to_class() const -> General { return {name}; }
|
||||
// -------------
|
||||
|
||||
// clang-format off
|
||||
fn NowPlayingImpl::from_class(
|
||||
const NowPlaying& now_playing
|
||||
// ----------------
|
||||
// -- NowPlaying --
|
||||
// ----------------
|
||||
DEFINE_GETTER(NowPlaying, bool, Enabled)
|
||||
|
||||
NowPlaying::NowPlaying(bool enabled) : m_Enabled(enabled) {}
|
||||
|
||||
fn NowPlayingImpl::from_class(const NowPlaying& now_playing
|
||||
) noexcept -> NowPlayingImpl {
|
||||
return {.enabled = now_playing.getEnabled()};
|
||||
}
|
||||
//clang-format on
|
||||
|
||||
fn NowPlayingImpl::to_class() const -> NowPlaying { return {enabled.value_or(false)}; }
|
||||
fn NowPlayingImpl::to_class() const -> NowPlaying {
|
||||
return {enabled.value_or(false)};
|
||||
}
|
||||
// ----------------
|
||||
|
||||
// ------------
|
||||
// -- Config --
|
||||
// ------------
|
||||
DEFINE_GETTER(Config, const General, General)
|
||||
DEFINE_GETTER(Config, const NowPlaying, NowPlaying)
|
||||
DEFINE_GETTER(Config, const Weather, Weather)
|
||||
|
||||
Config::Config(General general, NowPlaying now_playing, Weather weather)
|
||||
: m_General(std::move(general)),
|
||||
m_NowPlaying(now_playing),
|
||||
m_Weather(std::move(weather)) {}
|
||||
|
||||
fn Config::getInstance() -> const Config& {
|
||||
static const auto* INSTANCE =
|
||||
new Config(rfl::toml::load<Config>("./config.toml").value());
|
||||
return *INSTANCE;
|
||||
}
|
||||
|
||||
fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
|
||||
return {
|
||||
|
@ -71,3 +85,4 @@ fn ConfigImpl::from_class(const Config& config) noexcept -> ConfigImpl {
|
|||
fn ConfigImpl::to_class() const -> Config {
|
||||
return {general, now_playing, weather};
|
||||
}
|
||||
// ------------
|
||||
|
|
|
@ -4,104 +4,11 @@
|
|||
#include <rfl.hpp>
|
||||
#include <rfl/toml.hpp>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "util/numtypes.h"
|
||||
#include "util/macros.h"
|
||||
#include "weather.h"
|
||||
|
||||
class Weather {
|
||||
public:
|
||||
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
|
||||
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
|
||||
|
||||
struct Condition {
|
||||
std::string description;
|
||||
std::string icon;
|
||||
std::string main;
|
||||
usize id;
|
||||
};
|
||||
|
||||
struct Main {
|
||||
f64 feels_like;
|
||||
f64 temp;
|
||||
f64 temp_max;
|
||||
f64 temp_min;
|
||||
isize pressure;
|
||||
percentage humidity;
|
||||
std::optional<isize> grnd_level;
|
||||
std::optional<isize> sea_level;
|
||||
};
|
||||
|
||||
struct Wind {
|
||||
degrees deg;
|
||||
f64 speed;
|
||||
std::optional<f64> gust;
|
||||
};
|
||||
|
||||
struct Precipitation {
|
||||
rfl::Rename<"1h", f64> one_hour;
|
||||
rfl::Rename<"3h", f64> three_hours;
|
||||
};
|
||||
|
||||
struct Sys {
|
||||
std::string country;
|
||||
usize id;
|
||||
usize sunrise;
|
||||
usize sunset;
|
||||
usize type;
|
||||
};
|
||||
|
||||
struct Clouds {
|
||||
percentage all;
|
||||
};
|
||||
|
||||
struct Coords {
|
||||
double lat;
|
||||
double lon;
|
||||
};
|
||||
|
||||
struct WeatherOutput {
|
||||
Clouds clouds;
|
||||
isize timezone;
|
||||
isize visibility;
|
||||
Main main;
|
||||
rfl::Rename<"coord", Coords> coords;
|
||||
std::optional<Precipitation> rain;
|
||||
std::optional<Precipitation> snow;
|
||||
std::string base;
|
||||
std::string name;
|
||||
std::vector<Condition> weather;
|
||||
Sys sys;
|
||||
usize cod;
|
||||
usize dt;
|
||||
usize id;
|
||||
Wind wind;
|
||||
};
|
||||
|
||||
using Location = std::variant<std::string, Coords>;
|
||||
|
||||
private:
|
||||
Location m_Location;
|
||||
std::string m_ApiKey;
|
||||
std::string m_Units;
|
||||
|
||||
public:
|
||||
Weather(Location location, std::string api_key, std::string units);
|
||||
|
||||
[[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;
|
||||
|
||||
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
|
||||
|
||||
[[nodiscard]] fn to_class() const -> Weather;
|
||||
};
|
||||
// TODO: Make config values optional and supply defaults
|
||||
|
||||
class General {
|
||||
private:
|
||||
|
@ -168,15 +75,6 @@ struct ConfigImpl {
|
|||
|
||||
// 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> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, General, ProcessorsType>
|
||||
: public CustomParser<
|
||||
|
@ -203,4 +101,4 @@ namespace rfl::parsing {
|
|||
ProcessorsType,
|
||||
Config,
|
||||
ConfigImpl> {};
|
||||
} // namespace rfl::parsing
|
||||
}
|
||||
|
|
|
@ -2,16 +2,16 @@
|
|||
#include <fmt/core.h>
|
||||
#include <rfl/json.hpp>
|
||||
#include <rfl/json/load.hpp>
|
||||
#include <util/result.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "weather.h"
|
||||
|
||||
#include "util/result.h"
|
||||
|
||||
using WeatherOutput = Weather::WeatherOutput;
|
||||
|
||||
// Function to read cache from file
|
||||
fn ReadCacheFromFile() -> Result<WeatherOutput> {
|
||||
const std::string cacheFile = "/tmp/weather_cache.json";
|
||||
std::ifstream ifs(cacheFile);
|
||||
std::ifstream ifs("/tmp/weather_cache.json");
|
||||
|
||||
if (!ifs.is_open())
|
||||
return Error("Cache file not found.");
|
||||
|
@ -35,9 +35,9 @@ fn ReadCacheFromFile() -> Result<WeatherOutput> {
|
|||
|
||||
// Function to write cache to file
|
||||
fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
|
||||
const std::string cacheFile = "/tmp/weather_cache.json";
|
||||
fmt::println("Writing to cache file...");
|
||||
std::ofstream ofs(cacheFile);
|
||||
|
||||
std::ofstream ofs("/tmp/weather_cache.json");
|
||||
|
||||
if (!ofs.is_open())
|
||||
return Error("Failed to open cache file for writing.");
|
||||
|
@ -49,7 +49,8 @@ fn WriteCacheToFile(const WeatherOutput& data) -> Result<> {
|
|||
return Ok();
|
||||
}
|
||||
|
||||
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) -> size_t {
|
||||
fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str)
|
||||
-> size_t {
|
||||
size_t totalSize = size * nmemb;
|
||||
str->append(static_cast<char*>(contents), totalSize);
|
||||
return totalSize;
|
||||
|
@ -59,7 +60,7 @@ fn WriteCallback(void* contents, size_t size, size_t nmemb, std::string* str) ->
|
|||
fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
|
||||
fmt::println("Making API request to URL: {}", url);
|
||||
|
||||
CURL* curl = curl_easy_init();
|
||||
CURL* curl = curl_easy_init();
|
||||
std::string responseBuffer;
|
||||
|
||||
if (curl) {
|
||||
|
@ -70,10 +71,14 @@ fn MakeApiRequest(const std::string& url) -> Result<WeatherOutput> {
|
|||
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. Response size: {}", responseBuffer.size());
|
||||
fmt::println(
|
||||
"Received response from API. Response size: {}", responseBuffer.size()
|
||||
);
|
||||
|
||||
WeatherOutput output =
|
||||
rfl::json::read<WeatherOutput>(responseBuffer).value();
|
||||
|
|
116
src/config/weather.h
Normal file
116
src/config/weather.h
Normal file
|
@ -0,0 +1,116 @@
|
|||
#pragma once
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/toml.hpp>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include "util/macros.h"
|
||||
#include "util/numtypes.h"
|
||||
|
||||
class Weather {
|
||||
public:
|
||||
using degrees = rfl::Validator<u16, rfl::Minimum<0>, rfl::Maximum<360>>;
|
||||
using percentage = rfl::Validator<i8, rfl::Minimum<0>, rfl::Maximum<100>>;
|
||||
|
||||
struct Condition {
|
||||
std::string description;
|
||||
std::string icon;
|
||||
std::string main;
|
||||
usize id;
|
||||
};
|
||||
|
||||
struct Main {
|
||||
f64 feels_like;
|
||||
f64 temp;
|
||||
f64 temp_max;
|
||||
f64 temp_min;
|
||||
isize pressure;
|
||||
percentage humidity;
|
||||
std::optional<isize> grnd_level;
|
||||
std::optional<isize> sea_level;
|
||||
};
|
||||
|
||||
struct Wind {
|
||||
degrees deg;
|
||||
f64 speed;
|
||||
std::optional<f64> gust;
|
||||
};
|
||||
|
||||
struct Precipitation {
|
||||
rfl::Rename<"1h", f64> one_hour;
|
||||
rfl::Rename<"3h", f64> three_hours;
|
||||
};
|
||||
|
||||
struct Sys {
|
||||
std::string country;
|
||||
usize id;
|
||||
usize sunrise;
|
||||
usize sunset;
|
||||
usize type;
|
||||
};
|
||||
|
||||
struct Clouds {
|
||||
percentage all;
|
||||
};
|
||||
|
||||
struct Coords {
|
||||
double lat;
|
||||
double lon;
|
||||
};
|
||||
|
||||
struct WeatherOutput {
|
||||
Clouds clouds;
|
||||
isize timezone;
|
||||
isize visibility;
|
||||
Main main;
|
||||
rfl::Rename<"coord", Coords> coords;
|
||||
std::optional<Precipitation> rain;
|
||||
std::optional<Precipitation> snow;
|
||||
std::string base;
|
||||
std::string name;
|
||||
std::vector<Condition> weather;
|
||||
Sys sys;
|
||||
usize cod;
|
||||
usize dt;
|
||||
usize id;
|
||||
Wind wind;
|
||||
};
|
||||
|
||||
using Location = std::variant<std::string, Coords>;
|
||||
|
||||
private:
|
||||
Location m_Location;
|
||||
std::string m_ApiKey;
|
||||
std::string m_Units;
|
||||
|
||||
public:
|
||||
Weather(Location location, std::string api_key, std::string units);
|
||||
|
||||
[[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;
|
||||
|
||||
static fn from_class(const Weather& weather) noexcept -> WeatherImpl;
|
||||
|
||||
[[nodiscard]] fn to_class() const -> Weather;
|
||||
};
|
||||
|
||||
namespace rfl::parsing {
|
||||
template <class ReaderType, class WriterType, class ProcessorsType>
|
||||
struct Parser<ReaderType, WriterType, Weather, ProcessorsType>
|
||||
: public CustomParser<
|
||||
ReaderType,
|
||||
WriterType,
|
||||
ProcessorsType,
|
||||
Weather,
|
||||
WeatherImpl> {};
|
||||
}
|
|
@ -9,7 +9,7 @@
|
|||
+ (NSString*)macOSVersion;
|
||||
@end
|
||||
#else
|
||||
#include "util/numtypes.h"
|
||||
#include "util/macros.h"
|
||||
|
||||
extern "C" {
|
||||
fn GetCurrentPlayingTitle() -> const char*;
|
||||
|
|
|
@ -80,8 +80,13 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
|
|||
}
|
||||
|
||||
// Dictionary to map macOS versions to their respective names
|
||||
NSDictionary<NSNumber*, NSString*>* versionNames =
|
||||
@{@11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma"};
|
||||
NSDictionary<NSNumber*, NSString*>* versionNames = @{
|
||||
@11 : @"Big Sur",
|
||||
@12 : @"Monterey",
|
||||
@13 : @"Ventura",
|
||||
@14 : @"Sonoma",
|
||||
@15 : @"Sequoia"
|
||||
};
|
||||
|
||||
NSNumber* majorVersionNumber = @(osVersion.majorVersion);
|
||||
NSString* versionName = versionNames[majorVersionNumber];
|
||||
|
@ -96,7 +101,7 @@ using MRMediaRemoteGetNowPlayingInfoFunction = void (*)(
|
|||
}
|
||||
@end
|
||||
|
||||
#include "util/numtypes.h"
|
||||
#include "util/macros.h"
|
||||
|
||||
extern "C" {
|
||||
fn GetCurrentPlayingTitle() -> const char* {
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "util/macros.h"
|
||||
#include "util/numtypes.h"
|
||||
|
||||
u64 GetMemInfo();
|
||||
std::string GetNowPlaying();
|
||||
const char* GetOSVersion();
|
||||
fn GetMemInfo() -> u64;
|
||||
fn GetNowPlaying() -> std::string;
|
||||
fn GetOSVersion() -> const char*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue