From f0cc211e30715e2e09653553ae865097c9a4bf7d Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Wed, 6 Mar 2024 22:32:49 +0100 Subject: [PATCH] Unify http client creation --- CYD-Klipper/src/core/data_setup.cpp | 36 ++++------------------- CYD-Klipper/src/core/files_query.cpp | 12 ++------ CYD-Klipper/src/core/http_client.cpp | 26 ++++++++++++++++ CYD-Klipper/src/core/http_client.h | 11 +++++++ CYD-Klipper/src/core/macros_query.cpp | 28 +++--------------- CYD-Klipper/src/ui/gcode_img.cpp | 12 ++++---- CYD-Klipper/src/ui/ip_setup.cpp | 13 ++------ CYD-Klipper/src/ui/panels/print_panel.cpp | 30 ++----------------- 8 files changed, 60 insertions(+), 108 deletions(-) create mode 100644 CYD-Klipper/src/core/http_client.cpp create mode 100644 CYD-Klipper/src/core/http_client.h diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index d60de6e..d104ad3 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -2,11 +2,11 @@ #include "data_setup.h" #include "lvgl.h" #include "../conf/global_config.h" -#include #include #include #include "macros_query.h" #include +#include "http_client.h" const char *printer_state_messages[] = { "Error", @@ -45,22 +45,12 @@ void unfreeze_render_thread(){ void send_gcode(bool wait, const char *gcode) { Serial.printf("Sending gcode: %s\n", gcode); - char buff[256] = {}; - sprintf(buff, "http://%s:%d/printer/gcode/script?script=%s", global_config.klipperHost, global_config.klipperPort, urlEncode(gcode).c_str()); - HTTPClient client; - client.begin(buff); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); - - if (!wait) - { - client.setTimeout(1000); - } + SETUP_HTTP_CLIENT_FULL("/printer/gcode/script?script=" + urlEncode(gcode), false, wait ? 5000 : 750); try { - client.GET(); + int result = client.GET(); + Serial.printf("Send gcode result: %d\n", result); } catch (...) { @@ -75,14 +65,7 @@ int get_slicer_time_estimate_s() delay(10); - char buff[256] = {}; - sprintf(buff, "http://%s:%d/server/files/metadata?filename=%s", global_config.klipperHost, global_config.klipperPort, urlEncode(printer.print_filename).c_str()); - HTTPClient client; - client.useHTTP10(true); - client.begin(buff); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/server/files/metadata?filename=" + urlEncode(printer.print_filename)); int httpCode = client.GET(); @@ -128,14 +111,7 @@ int last_slicer_time_query = -15000; void fetch_printer_data() { freeze_request_thread(); - char buff[256] = {}; - sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan", global_config.klipperHost, global_config.klipperPort); - HTTPClient client; - client.useHTTP10(true); - client.begin(buff); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan") int httpCode = client.GET(); delay(10); diff --git a/CYD-Klipper/src/core/files_query.cpp b/CYD-Klipper/src/core/files_query.cpp index 326623b..57e0983 100644 --- a/CYD-Klipper/src/core/files_query.cpp +++ b/CYD-Klipper/src/core/files_query.cpp @@ -2,9 +2,9 @@ #include "files_query.h" #include "../conf/global_config.h" #include "data_setup.h" -#include #include #include +#include "http_client.h" // Always has +1 entry with a null'd name FILESYSTEM_FILE* last_query = NULL; @@ -27,15 +27,7 @@ FILESYSTEM_FILE* get_files(int limit){ std::list files; auto timer_request = millis(); - char buff[256] = {}; - sprintf(buff, "http://%s:%d/server/files/list", global_config.klipperHost, global_config.klipperPort); - HTTPClient client; - client.useHTTP10(true); - client.setTimeout(5000); - client.begin(buff); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT_FULL("/server/files/list", true, 5000); int httpCode = client.GET(); auto timer_parse = millis(); diff --git a/CYD-Klipper/src/core/http_client.cpp b/CYD-Klipper/src/core/http_client.cpp new file mode 100644 index 0000000..fb135bf --- /dev/null +++ b/CYD-Klipper/src/core/http_client.cpp @@ -0,0 +1,26 @@ +#include "http_client.h" +#include "../conf/global_config.h" + +String get_full_url(String url_part) +{ + return "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + url_part; +} + +void configure_http_client(HTTPClient &client, String url, bool stream, int timeout) +{ + Serial.println(url); + if (stream){ + client.useHTTP10(true); + } + + if (timeout > 0){ + client.setTimeout(timeout); + client.setConnectTimeout(timeout); + } + + client.begin(url); + + if (global_config.auth_configured) { + client.addHeader("X-Api-Key", global_config.klipper_auth); + } +} \ No newline at end of file diff --git a/CYD-Klipper/src/core/http_client.h b/CYD-Klipper/src/core/http_client.h new file mode 100644 index 0000000..d5c6d61 --- /dev/null +++ b/CYD-Klipper/src/core/http_client.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +String get_full_url(String url_part); + +void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000); + +#define SETUP_HTTP_CLIENT(url_part) HTTPClient client; configure_http_client(client, get_full_url(url_part)); + +#define SETUP_HTTP_CLIENT_FULL(url_part, stream, timeout) HTTPClient client; configure_http_client(client, get_full_url(url_part), stream, timeout); \ No newline at end of file diff --git a/CYD-Klipper/src/core/macros_query.cpp b/CYD-Klipper/src/core/macros_query.cpp index 53e639d..fcbf900 100644 --- a/CYD-Klipper/src/core/macros_query.cpp +++ b/CYD-Klipper/src/core/macros_query.cpp @@ -1,10 +1,10 @@ #include "lvgl.h" #include "macros_query.h" #include "./data_setup.h" -#include #include "../conf/global_config.h" #include #include +#include "http_client.h" static char* macros[64] = {0}; static int macros_count = 0; @@ -14,13 +14,7 @@ static bool power_device_states[16] = {0}; static int power_devices_count = 0; static void _macros_query_internal(){ - String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/gcode/help"; - HTTPClient client; - client.useHTTP10(true); - client.begin(url.c_str()); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/printer/gcode/help") int httpCode = client.GET(); if (httpCode == 200){ @@ -55,15 +49,7 @@ void power_devices_clear(){ } void _power_devices_query_internal(){ - String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/machine/device_power/devices"; - HTTPClient client; - client.useHTTP10(true); - client.setTimeout(500); - client.setConnectTimeout(1000); - client.begin(url.c_str()); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/machine/device_power/devices") int httpCode = client.GET(); if (httpCode == 200){ @@ -94,13 +80,7 @@ static void on_state_change(void * s, lv_msg_t * m) { } bool set_power_state(const char* device_name, bool state) { - String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off"); - HTTPClient client; - client.useHTTP10(true); - client.begin(url.c_str()); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off")); if (client.POST("") != 200) return false; diff --git a/CYD-Klipper/src/ui/gcode_img.cpp b/CYD-Klipper/src/ui/gcode_img.cpp index fe4523a..eb7cbe9 100644 --- a/CYD-Klipper/src/ui/gcode_img.cpp +++ b/CYD-Klipper/src/ui/gcode_img.cpp @@ -4,7 +4,7 @@ #include #include #include "../conf/global_config.h" -#include +#include "../core/http_client.h" static unsigned char * data_png = NULL; static char img_filename_path[256] = {0}; @@ -17,11 +17,10 @@ bool has_128_128_gcode(const char* filename) return false; } - String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/thumbnails?filename=" + String(filename); - HTTPClient client; + SETUP_HTTP_CLIENT("/server/files/thumbnails?filename=" + String(filename)); + int httpCode = 0; try { - client.begin(url.c_str()); httpCode = client.GET(); } catch (...){ @@ -72,11 +71,10 @@ lv_obj_t* draw_gcode_img() return NULL; } - HTTPClient client; + SETUP_HTTP_CLIENT_FULL("/server/files/gcodes/" + String(img_filename_path), false, 2000); + int httpCode = 0; try { - String img_url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/gcodes/" + String(img_filename_path); - client.begin(img_url); httpCode = client.GET(); } catch (...){ diff --git a/CYD-Klipper/src/ui/ip_setup.cpp b/CYD-Klipper/src/ui/ip_setup.cpp index 8cfbbbd..fbea738 100644 --- a/CYD-Klipper/src/ui/ip_setup.cpp +++ b/CYD-Klipper/src/ui/ip_setup.cpp @@ -6,6 +6,7 @@ #include "ui_utils.h" #include "../core/macros_query.h" #include "panels/panel.h" +#include "../core/http_client.h" bool connect_ok = false; lv_obj_t * hostEntry; @@ -36,19 +37,11 @@ enum connection_status_t { }; connection_status_t verify_ip(){ - HTTPClient client; - String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/info"; + SETUP_HTTP_CLIENT_FULL("/printer/info", true, 1000); + int httpCode; try { - client.setTimeout(500); - client.setConnectTimeout(1000); - client.begin(url.c_str()); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); - httpCode = client.GET(); - Serial.printf("%d %s\n", httpCode, url.c_str()); if (httpCode == 401) return CONNECT_AUTH_REQUIRED; diff --git a/CYD-Klipper/src/ui/panels/print_panel.cpp b/CYD-Klipper/src/ui/panels/print_panel.cpp index 357861e..d869c53 100644 --- a/CYD-Klipper/src/ui/panels/print_panel.cpp +++ b/CYD-Klipper/src/ui/panels/print_panel.cpp @@ -4,10 +4,11 @@ #include "../../core/files_query.h" #include "../../conf/global_config.h" #include -#include #include "../ui_utils.h" #include "../../core/lv_setup.h" #include "../gcode_img.h" +#include "../../core/http_client.h" +#include FILESYSTEM_FILE* selected_file = NULL; @@ -15,32 +16,7 @@ static void btn_print_file(lv_event_t * e){ lv_obj_t * panel = (lv_obj_t*)lv_event_get_user_data(e); lv_obj_del(panel); - char* buff = (char*)malloc(128 + (strlen(selected_file->name) * 3)); - sprintf(buff, "http://%s:%d/printer/print/start?filename=", global_config.klipperHost, global_config.klipperPort); - - char* ptr = buff + strlen(buff); - int filename_length = strlen(selected_file->name); - for (int i = 0; i < filename_length; i++){ - char c = selected_file->name[i]; - if (c == ' '){ - *ptr = '%'; - ptr++; - *ptr = '2'; - ptr++; - *ptr = '0'; - } else { - *ptr = c; - } - ptr++; - } - - *ptr = 0; - - HTTPClient client; - client.begin(buff); - - if (global_config.auth_configured) - client.addHeader("X-Api-Key", global_config.klipper_auth); + SETUP_HTTP_CLIENT("/printer/print/start?filename=" + urlEncode(selected_file->name)); int httpCode = client.POST(""); Serial.printf("Print start: HTTP %d\n", httpCode);