Rework network loop to make one request

This commit is contained in:
suchmememanyskill
2023-11-12 23:31:08 +01:00
parent 86fca4ca16
commit ee9b48850d
6 changed files with 142 additions and 133 deletions

View File

@@ -5,155 +5,159 @@
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char * printer_state_messages[] = {
const char *printer_state_messages[] = {
"Error",
"Idle",
"Printing"
};
"Printing"};
Printer printer = {0};
void send_gcode(bool wait, const char* gcode){
void send_gcode(bool wait, const char *gcode)
{
char buff[256] = {};
sprintf(buff, "http://%s:%d/printer/gcode/script?script=%s", global_config.klipperHost, global_config.klipperPort, gcode);
HTTPClient client;
client.begin(buff);
if (!wait){
if (!wait)
{
client.setTimeout(1000);
}
try {
try
{
client.GET();
}
catch (...){
catch (...)
{
Serial.println("Failed to send gcode");
}
}
// TODO: Merge with other request, see https://moonraker.readthedocs.io/en/latest/printer_objects/#webhooks
void fetch_printer_state(){
char buff[91] = {};
sprintf(buff, "http://%s:%d/printer/info", global_config.klipperHost, global_config.klipperPort);
HTTPClient client;
client.begin(buff);
int httpCode = client.GET();
if (httpCode == 200) {
String payload = client.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
auto result = doc["result"];
bool update = false;
const char* state = result["state"];
if (strcmp(state, "ready") == 0 && printer.state == PRINTER_STATE_ERROR){
printer.state = PRINTER_STATE_IDLE;
update = true;
}
else if (strcmp(state, "shutdown") == 0 && printer.state != PRINTER_STATE_ERROR) {
printer.state = PRINTER_STATE_ERROR;
update = true;
}
const char* message = result["state_message"];
if (printer.state_message == NULL || strcmp(printer.state_message, message)) {
if (printer.state_message != NULL){
free(printer.state_message);
}
printer.state_message = (char*)malloc(strlen(message) + 1);
strcpy(printer.state_message, message);
update = true;
}
if (update)
lv_msg_send(DATA_PRINTER_STATE, &printer);
}
else {
Serial.printf("Failed to fetch printer state: %d\n", httpCode);
}
}
char filename_buff[512] = {0};
void fetch_printer_data(){
void fetch_printer_data()
{
char buff[256] = {};
sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats", global_config.klipperHost, global_config.klipperPort);
sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks", global_config.klipperHost, global_config.klipperPort);
HTTPClient client;
client.begin(buff);
int httpCode = client.GET();
if (httpCode == 200) {
if (httpCode == 200)
{
String payload = client.getString();
DynamicJsonDocument doc(4096);
deserializeJson(doc, payload);
auto status = doc["result"]["status"];
if (status.containsKey("extruder")){
printer.extruder_temp = status["extruder"]["temperature"];
printer.extruder_target_temp = status["extruder"]["target"];
bool can_extrude = status["extruder"]["can_extrude"];
printer.can_extrude = can_extrude == true;
}
if (status.containsKey("heater_bed")){
printer.bed_temp = status["heater_bed"]["temperature"];
printer.bed_target_temp = status["heater_bed"]["target"];
}
if (status.containsKey("toolhead")){
printer.position[0] = status["toolhead"]["position"][0];
printer.position[1] = status["toolhead"]["position"][1];
printer.position[2] = status["toolhead"]["position"][2];
const char * homed_axis = status["toolhead"]["homed_axes"];
printer.homed_axis = strcmp(homed_axis, "xyz") == 0;
}
if (status.containsKey("gcode_move")){
bool absolute_coords = status["gcode_move"]["absolute_coordinates"];
printer.absolute_coords = absolute_coords == true;
}
if (status.containsKey("virtual_sdcard")){
printer.print_progress = status["virtual_sdcard"]["progress"];
}
bool emit_state_update = false;
int printer_state = printer.state;
if (status.containsKey("print_stats")){
const char* filename = status["print_stats"]["filename"];
strcpy(filename_buff, filename);
printer.print_filename = filename_buff;
printer.elapsed_time_s = status["print_stats"]["print_duration"];
printer.filament_used_mm = status["print_stats"]["filament_used"];
if (status.containsKey("webhooks"))
{
const char *state = status["webhooks"]["state"];
const char *message = status["webhooks"]["state_message"];
const char* state = status["print_stats"]["state"];
if (strcmp(state, "printing") == 0){
printer_state = PRINTER_STATE_PRINTING;
}
else if (strcmp(state, "paused") == 0){
printer_state = PRINTER_STATE_PAUSED;
}
else if (strcmp(state, "complete") == 0 || strcmp(state, "cancelled") == 0 || strcmp(state, "standby") == 0){
if (strcmp(state, "ready") == 0 && printer.state == PRINTER_STATE_ERROR)
{
printer_state = PRINTER_STATE_IDLE;
}
else if (strcmp(state, "shutdown") == 0 && printer.state != PRINTER_STATE_ERROR)
{
printer_state = PRINTER_STATE_ERROR;
}
if (printer.state_message == NULL || strcmp(printer.state_message, message))
{
if (printer.state_message != NULL)
{
free(printer.state_message);
}
printer.state_message = (char *)malloc(strlen(message) + 1);
strcpy(printer.state_message, message);
emit_state_update = true;
}
}
// TODO: make a call to /server/files/metadata to get more accurate time estimates
// https://moonraker.readthedocs.io/en/latest/web_api/#server-administration
if (printer_state != PRINTER_STATE_ERROR)
{
if (status.containsKey("extruder"))
{
printer.extruder_temp = status["extruder"]["temperature"];
printer.extruder_target_temp = status["extruder"]["target"];
bool can_extrude = status["extruder"]["can_extrude"];
printer.can_extrude = can_extrude == true;
}
if (printer.state == PRINTER_STATE_PRINTING && printer.print_progress > 0){
printer.remaining_time_s = (printer.elapsed_time_s / printer.print_progress) - printer.elapsed_time_s;
if (status.containsKey("heater_bed"))
{
printer.bed_temp = status["heater_bed"]["temperature"];
printer.bed_target_temp = status["heater_bed"]["target"];
}
if (status.containsKey("toolhead"))
{
printer.position[0] = status["toolhead"]["position"][0];
printer.position[1] = status["toolhead"]["position"][1];
printer.position[2] = status["toolhead"]["position"][2];
const char *homed_axis = status["toolhead"]["homed_axes"];
printer.homed_axis = strcmp(homed_axis, "xyz") == 0;
}
if (status.containsKey("gcode_move"))
{
bool absolute_coords = status["gcode_move"]["absolute_coordinates"];
printer.absolute_coords = absolute_coords == true;
}
if (status.containsKey("virtual_sdcard"))
{
printer.print_progress = status["virtual_sdcard"]["progress"];
}
if (status.containsKey("print_stats"))
{
const char *filename = status["print_stats"]["filename"];
strcpy(filename_buff, filename);
printer.print_filename = filename_buff;
printer.elapsed_time_s = status["print_stats"]["print_duration"];
printer.filament_used_mm = status["print_stats"]["filament_used"];
const char *state = status["print_stats"]["state"];
if (strcmp(state, "printing") == 0)
{
printer_state = PRINTER_STATE_PRINTING;
}
else if (strcmp(state, "paused") == 0)
{
printer_state = PRINTER_STATE_PAUSED;
}
else if (strcmp(state, "complete") == 0 || strcmp(state, "cancelled") == 0 || strcmp(state, "standby") == 0)
{
printer_state = PRINTER_STATE_IDLE;
}
}
// TODO: make a call to /server/files/metadata to get more accurate time estimates
// https://moonraker.readthedocs.io/en/latest/web_api/#server-administration
if (printer.state == PRINTER_STATE_PRINTING && printer.print_progress > 0)
{
printer.remaining_time_s = (printer.elapsed_time_s / printer.print_progress) - printer.elapsed_time_s;
}
lv_msg_send(DATA_PRINTER_DATA, &printer);
}
lv_msg_send(DATA_PRINTER_DATA, &printer);
if (printer.state != printer_state){
if (printer.state != printer_state || emit_state_update)
{
printer.state = printer_state;
lv_msg_send(DATA_PRINTER_STATE, &printer);
}
}
else {
else
{
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
}
}
@@ -161,19 +165,18 @@ void fetch_printer_data(){
long last_data_update = 0;
const long data_update_interval = 1500;
void data_loop(){
void data_loop()
{
if (millis() - last_data_update < data_update_interval)
return;
last_data_update = millis();
fetch_printer_state();
if (printer.state != PRINTER_STATE_ERROR)
fetch_printer_data();
}
void data_setup(){
printer.print_filename = filename_buff;
fetch_printer_state();
fetch_printer_data();
}
void data_setup()
{
printer.print_filename = filename_buff;
fetch_printer_data();
}

View File

@@ -44,19 +44,15 @@ static void update_printer_data_time(lv_event_t * e){
unsigned long minutes = (time % 3600) / 60;
unsigned long seconds = (time % 3600) % 60;
if (hours > 99){
lv_label_set_text(label, ">99h");
return;
}
if (hours >= 1){
sprintf(time_buffer, "%02luh%02lum", hours, minutes);
if (hours >= 10){
sprintf(time_buffer, "%luh", hours);
} else if (hours >= 1){
sprintf(time_buffer, "%luh%02lum", hours, minutes);
} else {
sprintf(time_buffer, "%02lum%02lus", minutes, seconds);
sprintf(time_buffer, "%02lum", minutes);
}
lv_label_set_text(label, time_buffer);
}
static void btn_click_files(lv_event_t * e){

View File

@@ -107,7 +107,7 @@ static void stepper_state_update(lv_event_t * e){
lv_label_set_text(label, printer.homed_axis ? LV_SYMBOL_HOME " Steppers locked" : LV_SYMBOL_EYE_CLOSE " Steppers unlocked");
}
void move_panel_data(lv_obj_t* panel){
void move_panel_init(lv_obj_t* panel){
lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE);
const int button_size = 40;
const int button_size_vertical = 40;
@@ -205,11 +205,5 @@ void move_panel_data(lv_obj_t* panel){
y_pos += 60;
}
}
void move_panel_init(lv_obj_t* panel){
move_panel_data(panel);
lv_msg_send(DATA_PRINTER_DATA, &printer);
}

View File

@@ -3,7 +3,7 @@
#include "../../core/data_setup.h"
void print_panel_init(lv_obj_t* panel){
if (printer.state == PRINTER_STATE_PRINTING || printer.state == PRINTER_STATE_PAUSED || true){
if (printer.state == PRINTER_STATE_PRINTING || printer.state == PRINTER_STATE_PAUSED){
progress_panel_init(panel);
return;
}

View File

@@ -35,15 +35,15 @@ static void update_printer_data_percentage(lv_event_t * e){
}
static void btn_click_stop(lv_event_t * e){
// TODO: Implement
send_gcode(true, "CANCEL_PRINT");
}
static void btn_click_pause(lv_event_t * e){
// TODO: Implement
send_gcode(true, "PAUSE");
}
static void btn_click_resume(lv_event_t * e){
// TODO: Implement
send_gcode(true, "RESUME");
}
void progress_panel_init(lv_obj_t* panel){
@@ -117,4 +117,6 @@ void progress_panel_init(lv_obj_t* panel){
lv_label_set_text(label, LV_SYMBOL_PAUSE);
lv_obj_center(label);
}
lv_msg_send(DATA_PRINTER_DATA, &printer);
}

View File

@@ -79,16 +79,28 @@ static void show_keyboard_with_bed(lv_event_t * e){
}
static void cooldown_temp(lv_event_t * e){
if (printer.state == PRINTER_STATE_PRINTING){
return;
}
send_gcode(true, "M104%20S0");
send_gcode(true, "M140%20S0");
}
static void btn_extrude(lv_event_t * e){
if (printer.state == PRINTER_STATE_PRINTING){
return;
}
send_gcode(true, "M83");
send_gcode(true, "G1%20E25%20F300");
}
static void btn_retract(lv_event_t * e){
if (printer.state == PRINTER_STATE_PRINTING){
return;
}
send_gcode(true, "M83");
send_gcode(true, "G1%20E-25%20F300");
}
@@ -148,4 +160,6 @@ void temp_panel_init(lv_obj_t* panel){
label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_UP " Retract");
lv_obj_center(label);
lv_msg_send(DATA_PRINTER_DATA, &printer);
}