statusweather: reformat
This commit is contained in:
parent
fee14c4585
commit
c9c99cf37c
@ -38,32 +38,31 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
struct weather
|
struct weather {
|
||||||
{
|
|
||||||
float temperature{-273.15};
|
float temperature{-273.15};
|
||||||
std::string icon{"❗"};
|
std::string icon{"❗"};
|
||||||
bool old{true};
|
bool old{true};
|
||||||
} __attribute__((aligned(64))) weather; // NOLINT(cert-err58-cpp)
|
} __attribute__((aligned(64))) weather; // NOLINT(cert-err58-cpp)
|
||||||
|
|
||||||
std::mutex mutex_weather;
|
std::mutex mutex_weather;
|
||||||
|
|
||||||
std::string map_icon(const std::string_view icon_id)
|
std::string map_icon(const std::string_view icon_id) {
|
||||||
{
|
|
||||||
// <https://openweathermap.org/weather-conditions>
|
// <https://openweathermap.org/weather-conditions>
|
||||||
// NOTE: There does not seem to be weather emojis with the moon in unicode.
|
// NOTE: There does not seem to be weather emojis with the moon in unicode.
|
||||||
const std::map<std::uint8_t, std::pair<std::string, std::string>> icons{
|
const std::map<std::uint8_t, std::pair<std::string, std::string>> icons{
|
||||||
{{1, {"🌞", "🌝"}}, // clear sky
|
{{1, {"🌞", "🌝"}}, // clear sky
|
||||||
{2, {"🌤", "🌤"}}, // few clouds
|
{2, {"🌤", "🌤"}}, // few clouds
|
||||||
{3, {"⛅", "⛅"}}, // scattered clouds
|
{3, {"⛅", "⛅"}}, // scattered clouds
|
||||||
{4, {"☁", "☁"}}, // broken clouds
|
{4, {"☁", "☁"}}, // broken clouds
|
||||||
{9, {"🌧", "🌧"}}, // shower rain
|
{9, {"🌧", "🌧"}}, // shower rain
|
||||||
{10, {"🌧", "🌧"}}, // rain
|
{10, {"🌧", "🌧"}}, // rain
|
||||||
{10, {"🌩", "🌩"}}, // thunderstorm
|
{10, {"🌩", "🌩"}}, // thunderstorm
|
||||||
{13, {"🌨", "🌨"}}, // snow
|
{13, {"🌨", "🌨"}}, // snow
|
||||||
{50, {"🌫", "🌫"}}}}; // mist
|
{50, {"🌫", "🌫"}}}
|
||||||
|
}; // mist
|
||||||
const auto icon{icons.find(std::stoul(icon_id.data()))};
|
const auto icon{icons.find(std::stoul(icon_id.data()))};
|
||||||
|
|
||||||
if (icon != icons.end())
|
if (icon != icons.end()) {
|
||||||
{
|
|
||||||
if (*icon_id.rbegin() == 'n') // Night
|
if (*icon_id.rbegin() == 'n') // Night
|
||||||
{
|
{
|
||||||
return icon->second.second;
|
return icon->second.second;
|
||||||
@ -73,8 +72,7 @@ std::string map_icon(const std::string_view icon_id)
|
|||||||
return "❔";
|
return "❔";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<std::string, std::string> get_options()
|
std::tuple<std::string, std::string> get_options() {
|
||||||
{
|
|
||||||
namespace po = boost::program_options;
|
namespace po = boost::program_options;
|
||||||
|
|
||||||
po::options_description options("Options");
|
po::options_description options("Options");
|
||||||
@ -99,18 +97,14 @@ std::tuple<std::string, std::string> get_options()
|
|||||||
vm["city"].as<std::string>());
|
vm["city"].as<std::string>());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fetch_weather()
|
bool fetch_weather() {
|
||||||
{
|
|
||||||
using fmt::format;
|
using fmt::format;
|
||||||
|
|
||||||
std::string api_key;
|
std::string api_key;
|
||||||
std::string city;
|
std::string city;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
std::tie(api_key, city) = get_options();
|
std::tie(api_key, city) = get_options();
|
||||||
}
|
} catch (std::runtime_error &e) {
|
||||||
catch (std::runtime_error &e)
|
|
||||||
{
|
|
||||||
std::cout << R"(<span color="red">)" << e.what() << "</span>"
|
std::cout << R"(<span color="red">)" << e.what() << "</span>"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -127,43 +121,35 @@ bool fetch_weather()
|
|||||||
RestClient::disable();
|
RestClient::disable();
|
||||||
|
|
||||||
const std::lock_guard<std::mutex> guard(mutex_weather);
|
const std::lock_guard<std::mutex> guard(mutex_weather);
|
||||||
if (response.code == 200)
|
if (response.code == 200) {
|
||||||
{
|
|
||||||
const auto json{nlohmann::json::parse(response.body)};
|
const auto json{nlohmann::json::parse(response.body)};
|
||||||
weather.temperature = json[0]["main"]["temp"].get<float>();
|
weather.temperature = json[0]["main"]["temp"].get<float>();
|
||||||
weather.icon = map_icon(
|
weather.icon = map_icon(
|
||||||
json[0]["weather"][0]["icon"].get<std::string_view>());
|
json[0]["weather"][0]["icon"].get<std::string_view>());
|
||||||
weather.old = false;
|
weather.old = false;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
weather.old = true;
|
weather.old = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_weather()
|
void print_weather() {
|
||||||
{
|
|
||||||
using fmt::format;
|
using fmt::format;
|
||||||
|
|
||||||
const std::lock_guard<std::mutex> guard(mutex_weather);
|
const std::lock_guard<std::mutex> guard(mutex_weather);
|
||||||
const std::string color{[]
|
const std::string color{[] {
|
||||||
{
|
if (weather.temperature > 25.0) {
|
||||||
if (weather.temperature > 25.0)
|
return "#ff2200";
|
||||||
{
|
}
|
||||||
return "#ff2200";
|
if (weather.temperature < 0.0) {
|
||||||
}
|
return "#aaffff";
|
||||||
if (weather.temperature < 0.0)
|
}
|
||||||
{
|
if (weather.temperature < 10.0) {
|
||||||
return "#aaffff";
|
return "#44ddff";
|
||||||
}
|
}
|
||||||
if (weather.temperature < 10.0)
|
return "#66ff66";
|
||||||
{
|
}()};
|
||||||
return "#44ddff";
|
|
||||||
}
|
|
||||||
return "#66ff66";
|
|
||||||
}()};
|
|
||||||
|
|
||||||
std::cout << format(R"(<big>{0:s}</big> )"
|
std::cout << format(R"(<big>{0:s}</big> )"
|
||||||
R"(<span color="{1:s}">{2:.1Lf}°C</span>{3:s})",
|
R"(<span color="{1:s}">{2:.1Lf}°C</span>{3:s})",
|
||||||
@ -172,25 +158,20 @@ void print_weather()
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(std::atomic<bool> &cancelled)
|
void update(std::atomic<bool> &cancelled) {
|
||||||
{
|
|
||||||
using clock = std::chrono::system_clock;
|
using clock = std::chrono::system_clock;
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
while (!cancelled)
|
while (!cancelled) {
|
||||||
{
|
if (fetch_weather()) {
|
||||||
if (fetch_weather())
|
|
||||||
{
|
|
||||||
print_weather();
|
print_weather();
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_until(clock::now() + 30min);
|
std::this_thread::sleep_until(clock::now() + 30min);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
try {
|
||||||
try
|
|
||||||
{
|
|
||||||
std::locale::global(std::locale(""));
|
std::locale::global(std::locale(""));
|
||||||
// TODO: Implement clean shutdown.
|
// TODO: Implement clean shutdown.
|
||||||
std::atomic<bool> cancelled{false};
|
std::atomic<bool> cancelled{false};
|
||||||
@ -202,15 +183,12 @@ int main()
|
|||||||
{
|
{
|
||||||
if (line == "1") // Left mouse button.
|
if (line == "1") // Left mouse button.
|
||||||
{
|
{
|
||||||
if (fetch_weather())
|
if (fetch_weather()) {
|
||||||
{
|
|
||||||
print_weather();
|
print_weather();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (const std::exception &e) {
|
||||||
catch (const std::exception &e)
|
|
||||||
{
|
|
||||||
std::cout << R"(<span color="red">)" << e.what() << "</span>"
|
std::cout << R"(<span color="red">)" << e.what() << "</span>"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user