From 22bde518ba23fd55ac4f42fb6c5f6a84b3dc42c3 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Thu, 23 Dec 2021 21:21:09 -0500 Subject: [PATCH] Improve update logic --- src/panel/plex.cpp | 58 ++++++++++++++++++++++--------------------- src/panel/weather.cpp | 19 ++++++++++---- src/panel/weather.hpp | 2 +- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/panel/plex.cpp b/src/panel/plex.cpp index 0667727..dc76386 100644 --- a/src/panel/plex.cpp +++ b/src/panel/plex.cpp @@ -73,12 +73,11 @@ void plex::update(){ //fetch updates //CURL object has been setup in the constructor - CURLcode res = curl_easy_perform(api_curl); + curl_easy_perform(api_curl); //parse the result json_doc.Parse(json_string.c_str()); - - std::cerr << json_string << "\n"; + entries.clear(); //update internal state rapidjson::Value& curr_entry = json_doc["response"]["data"]["data"]; @@ -101,6 +100,7 @@ void plex::update(){ std::cerr << entries[i].state << "\n"; } + json_string.clear(); } /////////////////////////////////////// @@ -130,35 +130,37 @@ void plex::update_texture(){ constexpr int GAP_SIZE = 10; //draw the outline rectangles - tgt.x = GAP_SIZE; - tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; - tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); - tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); - SDL_RenderFillRect(board::getRenderer(), &tgt); + { + tgt.x = GAP_SIZE; + tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; + tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); + tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); + SDL_RenderFillRect(board::getRenderer(), &tgt); - tgt.x = GAP_SIZE; - tgt.y = (SCREEN_HEIGHT / 2) + GAP_SIZE; - tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); - tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); - SDL_RenderFillRect(board::getRenderer(), &tgt); + tgt.x = GAP_SIZE; + tgt.y = (SCREEN_HEIGHT / 2) + GAP_SIZE; + tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); + tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); + SDL_RenderFillRect(board::getRenderer(), &tgt); - tgt.x = (SCREEN_WIDTH / 3) + GAP_SIZE; - tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; - tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); - tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); - SDL_RenderFillRect(board::getRenderer(), &tgt); + tgt.x = (SCREEN_WIDTH / 3) + GAP_SIZE; + tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; + tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); + tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); + SDL_RenderFillRect(board::getRenderer(), &tgt); - tgt.x = (SCREEN_WIDTH / 3) + GAP_SIZE; - tgt.y = (SCREEN_HEIGHT / 2) + GAP_SIZE; - tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); - tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); - SDL_RenderFillRect(board::getRenderer(), &tgt); + tgt.x = (SCREEN_WIDTH / 3) + GAP_SIZE; + tgt.y = (SCREEN_HEIGHT / 2) + GAP_SIZE; + tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); + tgt.h = (SCREEN_HEIGHT / 2) - DEF_OVERLAY_BAR_HEIGHT - (2*GAP_SIZE); + SDL_RenderFillRect(board::getRenderer(), &tgt); - tgt.x = ((2*SCREEN_WIDTH) / 3) + GAP_SIZE; - tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; - tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); - tgt.h = (SCREEN_HEIGHT) - (2*DEF_OVERLAY_BAR_HEIGHT) - (2*GAP_SIZE); - SDL_RenderFillRect(board::getRenderer(), &tgt); + tgt.x = ((2*SCREEN_WIDTH) / 3) + GAP_SIZE; + tgt.y = DEF_OVERLAY_BAR_HEIGHT + GAP_SIZE; + tgt.w = (SCREEN_WIDTH / 3) - (2*GAP_SIZE); + tgt.h = (SCREEN_HEIGHT) - (2*DEF_OVERLAY_BAR_HEIGHT) - (2*GAP_SIZE); + SDL_RenderFillRect(board::getRenderer(), &tgt); + } //draw info for first box { diff --git a/src/panel/weather.cpp b/src/panel/weather.cpp index df9bb4b..804edf4 100644 --- a/src/panel/weather.cpp +++ b/src/panel/weather.cpp @@ -90,6 +90,11 @@ void weather::draw(){ void weather::update() { std::cerr << "WEATHER::UPDATE\n"; _last_update = std::chrono::high_resolution_clock::now(); + temp_today = 0; temp_tommorow = 0; temp_day_after = 0; + weather_today = nullptr; + weather_tommorow = nullptr; + weather_day_after = nullptr; + //fetch updates //do curl setup and cleanup @@ -102,16 +107,18 @@ void weather::update() { curl_easy_setopt(api_curl, CURLOPT_WRITEFUNCTION, dashboard::panel::weather::curl_callback); curl_easy_setopt(api_curl, CURLOPT_WRITEDATA, &json_string); - CURLcode res = curl_easy_perform(api_curl); + curl_easy_perform(api_curl); curl_easy_cleanup(api_curl); api_curl = nullptr; } - //prase the response - json_doc.Parse(json_string.c_str()); + //parse the response + //We clean it up at then end because its wasting space if we dont + json_doc = new rapidjson::Document(); + json_doc->Parse(json_string.c_str()); //update internal state - const rapidjson::Value& curr_entry = json_doc["dataseries"]; + const rapidjson::Value& curr_entry = (*json_doc)["dataseries"]; //get all entries weather_today = &weather_string.at(curr_entry[0]["weather"].GetString()); weather_tommorow = &weather_string.at(curr_entry[7]["weather"].GetString()); @@ -127,6 +134,9 @@ void weather::update() { temp_tommorow = (temp_tommorow * 1.8) + 32; temp_day_after = (temp_day_after * 1.8) + 32; } + + json_string.clear(); + delete json_doc; } /////////////////////////////////////// @@ -176,7 +186,6 @@ void weather::update_texture(){ { "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt); //icon - std::cerr << weather_today->second << "\n"; tgt.w = (SCREEN_WIDTH / 3) - 50; tgt.h = tgt.w; tgt.y = DEF_OVERLAY_BAR_HEIGHT + 25; diff --git a/src/panel/weather.hpp b/src/panel/weather.hpp index 17f382c..eec22aa 100644 --- a/src/panel/weather.hpp +++ b/src/panel/weather.hpp @@ -51,7 +51,7 @@ namespace dashboard::panel { CURL* api_curl; std::string json_string; - rapidjson::Document json_doc; + rapidjson::Document* json_doc; std::chrono::time_point _last_update; std::chrono::milliseconds _update_interval;