mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 13:43:25 +00:00
Unify http client creation
This commit is contained in:
@@ -2,11 +2,11 @@
|
|||||||
#include "data_setup.h"
|
#include "data_setup.h"
|
||||||
#include "lvgl.h"
|
#include "lvgl.h"
|
||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <esp_task_wdt.h>
|
#include <esp_task_wdt.h>
|
||||||
#include "macros_query.h"
|
#include "macros_query.h"
|
||||||
#include <UrlEncode.h>
|
#include <UrlEncode.h>
|
||||||
|
#include "http_client.h"
|
||||||
|
|
||||||
const char *printer_state_messages[] = {
|
const char *printer_state_messages[] = {
|
||||||
"Error",
|
"Error",
|
||||||
@@ -45,22 +45,12 @@ void unfreeze_render_thread(){
|
|||||||
void send_gcode(bool wait, const char *gcode)
|
void send_gcode(bool wait, const char *gcode)
|
||||||
{
|
{
|
||||||
Serial.printf("Sending gcode: %s\n", 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
|
try
|
||||||
{
|
{
|
||||||
client.GET();
|
int result = client.GET();
|
||||||
|
Serial.printf("Send gcode result: %d\n", result);
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
@@ -75,14 +65,7 @@ int get_slicer_time_estimate_s()
|
|||||||
|
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
||||||
char buff[256] = {};
|
SETUP_HTTP_CLIENT("/server/files/metadata?filename=" + urlEncode(printer.print_filename));
|
||||||
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);
|
|
||||||
|
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
|
|
||||||
@@ -128,14 +111,7 @@ int last_slicer_time_query = -15000;
|
|||||||
void fetch_printer_data()
|
void fetch_printer_data()
|
||||||
{
|
{
|
||||||
freeze_request_thread();
|
freeze_request_thread();
|
||||||
char buff[256] = {};
|
SETUP_HTTP_CLIENT("/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks&fan")
|
||||||
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);
|
|
||||||
|
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
#include "files_query.h"
|
#include "files_query.h"
|
||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
#include "data_setup.h"
|
#include "data_setup.h"
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
|
#include "http_client.h"
|
||||||
|
|
||||||
// Always has +1 entry with a null'd name
|
// Always has +1 entry with a null'd name
|
||||||
FILESYSTEM_FILE* last_query = NULL;
|
FILESYSTEM_FILE* last_query = NULL;
|
||||||
@@ -27,15 +27,7 @@ FILESYSTEM_FILE* get_files(int limit){
|
|||||||
std::list<FILESYSTEM_FILE> files;
|
std::list<FILESYSTEM_FILE> files;
|
||||||
|
|
||||||
auto timer_request = millis();
|
auto timer_request = millis();
|
||||||
char buff[256] = {};
|
SETUP_HTTP_CLIENT_FULL("/server/files/list", true, 5000);
|
||||||
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);
|
|
||||||
|
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
auto timer_parse = millis();
|
auto timer_parse = millis();
|
||||||
|
|||||||
26
CYD-Klipper/src/core/http_client.cpp
Normal file
26
CYD-Klipper/src/core/http_client.cpp
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CYD-Klipper/src/core/http_client.h
Normal file
11
CYD-Klipper/src/core/http_client.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
|
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);
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
#include "lvgl.h"
|
#include "lvgl.h"
|
||||||
#include "macros_query.h"
|
#include "macros_query.h"
|
||||||
#include "./data_setup.h"
|
#include "./data_setup.h"
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <UrlEncode.h>
|
#include <UrlEncode.h>
|
||||||
|
#include "http_client.h"
|
||||||
|
|
||||||
static char* macros[64] = {0};
|
static char* macros[64] = {0};
|
||||||
static int macros_count = 0;
|
static int macros_count = 0;
|
||||||
@@ -14,13 +14,7 @@ static bool power_device_states[16] = {0};
|
|||||||
static int power_devices_count = 0;
|
static int power_devices_count = 0;
|
||||||
|
|
||||||
static void _macros_query_internal(){
|
static void _macros_query_internal(){
|
||||||
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/gcode/help";
|
SETUP_HTTP_CLIENT("/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);
|
|
||||||
|
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
if (httpCode == 200){
|
if (httpCode == 200){
|
||||||
@@ -55,15 +49,7 @@ void power_devices_clear(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _power_devices_query_internal(){
|
void _power_devices_query_internal(){
|
||||||
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/machine/device_power/devices";
|
SETUP_HTTP_CLIENT("/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);
|
|
||||||
|
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
if (httpCode == 200){
|
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) {
|
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");
|
SETUP_HTTP_CLIENT("/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);
|
|
||||||
|
|
||||||
if (client.POST("") != 200)
|
if (client.POST("") != 200)
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <Esp.h>
|
#include <Esp.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
#include <HTTPClient.h>
|
#include "../core/http_client.h"
|
||||||
|
|
||||||
static unsigned char * data_png = NULL;
|
static unsigned char * data_png = NULL;
|
||||||
static char img_filename_path[256] = {0};
|
static char img_filename_path[256] = {0};
|
||||||
@@ -17,11 +17,10 @@ bool has_128_128_gcode(const char* filename)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/server/files/thumbnails?filename=" + String(filename);
|
SETUP_HTTP_CLIENT("/server/files/thumbnails?filename=" + String(filename));
|
||||||
HTTPClient client;
|
|
||||||
int httpCode = 0;
|
int httpCode = 0;
|
||||||
try {
|
try {
|
||||||
client.begin(url.c_str());
|
|
||||||
httpCode = client.GET();
|
httpCode = client.GET();
|
||||||
}
|
}
|
||||||
catch (...){
|
catch (...){
|
||||||
@@ -72,11 +71,10 @@ lv_obj_t* draw_gcode_img()
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPClient client;
|
SETUP_HTTP_CLIENT_FULL("/server/files/gcodes/" + String(img_filename_path), false, 2000);
|
||||||
|
|
||||||
int httpCode = 0;
|
int httpCode = 0;
|
||||||
try {
|
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();
|
httpCode = client.GET();
|
||||||
}
|
}
|
||||||
catch (...){
|
catch (...){
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "ui_utils.h"
|
#include "ui_utils.h"
|
||||||
#include "../core/macros_query.h"
|
#include "../core/macros_query.h"
|
||||||
#include "panels/panel.h"
|
#include "panels/panel.h"
|
||||||
|
#include "../core/http_client.h"
|
||||||
|
|
||||||
bool connect_ok = false;
|
bool connect_ok = false;
|
||||||
lv_obj_t * hostEntry;
|
lv_obj_t * hostEntry;
|
||||||
@@ -36,19 +37,11 @@ enum connection_status_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
connection_status_t verify_ip(){
|
connection_status_t verify_ip(){
|
||||||
HTTPClient client;
|
SETUP_HTTP_CLIENT_FULL("/printer/info", true, 1000);
|
||||||
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/info";
|
|
||||||
int httpCode;
|
int httpCode;
|
||||||
try {
|
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();
|
httpCode = client.GET();
|
||||||
Serial.printf("%d %s\n", httpCode, url.c_str());
|
|
||||||
|
|
||||||
if (httpCode == 401)
|
if (httpCode == 401)
|
||||||
return CONNECT_AUTH_REQUIRED;
|
return CONNECT_AUTH_REQUIRED;
|
||||||
|
|||||||
@@ -4,10 +4,11 @@
|
|||||||
#include "../../core/files_query.h"
|
#include "../../core/files_query.h"
|
||||||
#include "../../conf/global_config.h"
|
#include "../../conf/global_config.h"
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include "../ui_utils.h"
|
#include "../ui_utils.h"
|
||||||
#include "../../core/lv_setup.h"
|
#include "../../core/lv_setup.h"
|
||||||
#include "../gcode_img.h"
|
#include "../gcode_img.h"
|
||||||
|
#include "../../core/http_client.h"
|
||||||
|
#include <UrlEncode.h>
|
||||||
|
|
||||||
FILESYSTEM_FILE* selected_file = NULL;
|
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_t * panel = (lv_obj_t*)lv_event_get_user_data(e);
|
||||||
lv_obj_del(panel);
|
lv_obj_del(panel);
|
||||||
|
|
||||||
char* buff = (char*)malloc(128 + (strlen(selected_file->name) * 3));
|
SETUP_HTTP_CLIENT("/printer/print/start?filename=" + urlEncode(selected_file->name));
|
||||||
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);
|
|
||||||
|
|
||||||
int httpCode = client.POST("");
|
int httpCode = client.POST("");
|
||||||
Serial.printf("Print start: HTTP %d\n", httpCode);
|
Serial.printf("Print start: HTTP %d\n", httpCode);
|
||||||
|
|||||||
Reference in New Issue
Block a user