Improve update logic

This commit is contained in:
Tyler Perkins 2021-12-23 21:21:09 -05:00
parent 5f3f911a1a
commit 22bde518ba
3 changed files with 45 additions and 34 deletions

View File

@ -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
{

View File

@ -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;

View File

@ -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<std::chrono::high_resolution_clock> _last_update;
std::chrono::milliseconds _update_interval;