Pass data json from Connect FE to upload.

Parse filename and return it back to the json in correct form.
This commit is contained in:
David Kocik 2024-06-07 14:26:27 +02:00 committed by Lukas Matena
parent a03facbeb4
commit 43620b8dde
5 changed files with 82 additions and 38 deletions

View File

@ -5916,26 +5916,29 @@ void Plater::connect_gcode()
*/
const Preset* selected_printer_preset = &wxGetApp().preset_bundle->printers.get_selected_preset();
const std::string set_ready = p->user_account->get_keyword_from_json(dialog_msg, "set_ready");
const std::string position = p->user_account->get_keyword_from_json(dialog_msg, "position");
const std::string wait_until = p->user_account->get_keyword_from_json(dialog_msg, "wait_until");
const std::string filename = p->user_account->get_keyword_from_json(dialog_msg, "filename");
const std::string printer_uuid = p->user_account->get_keyword_from_json(dialog_msg, "printer_uuid");
const std::string team_id = p->user_account->get_keyword_from_json(dialog_msg, "team_id");
std::string data_subtree = p->user_account->get_print_data_from_json(dialog_msg, "data");
if (filename.empty() || team_id.empty() || data_subtree.empty()) {
std::string msg = _u8L("Failed to read response from Connect server. Upload is canceled.");
BOOST_LOG_TRIVIAL(error) << msg;
BOOST_LOG_TRIVIAL(error) << "Response: " << dialog_msg;
show_error(this, msg);
return;
}
PhysicalPrinter ph_printer("connect_temp_printer", wxGetApp().preset_bundle->physical_printers.default_config(), *selected_printer_preset);
ph_printer.config.set_key_value("host_type", new ConfigOptionEnum<PrintHostType>(htPrusaConnectNew));
// use existing structures to pass data
ph_printer.config.opt_string("printhost_apikey") = team_id;
ph_printer.config.opt_string("print_host") = printer_uuid;
DynamicPrintConfig* physical_printer_config = &ph_printer.config;
PrintHostJob upload_job(physical_printer_config);
assert(!upload_job.empty());
upload_job.upload_data.set_ready = set_ready;
upload_job.upload_data.position = position;
upload_job.upload_data.wait_until = wait_until;
upload_job.upload_data.data_json = data_subtree;
upload_job.upload_data.upload_path = boost::filesystem::path(filename);
p->export_gcode(fs::path(), false, std::move(upload_job));

View File

@ -399,6 +399,43 @@ std::string UserAccount::get_keyword_from_json(const std::string& json, const st
return out;
}
std::string UserAccount::get_print_data_from_json(const std::string &json, const std::string &keyword) const
{
// copy subtree string f.e.
// { "<keyword>": {"param1": "something", "filename":"abcd.gcode", "param3":true}, "something_else" : 0 }
// into: {"param1": "something", "filename":"%1%", "param3":true, "size":%2%}
// yes there will be 2 placeholders for later format
// this will fail if not flat subtree
size_t start_of_keyword = json.find("\""+keyword+"\"");
if (start_of_keyword == std::string::npos)
return {};
size_t start_of_sub = json.find('{', start_of_keyword);
if (start_of_sub == std::string::npos)
return {};
size_t end_of_sub = json.find('}', start_of_sub);
if (end_of_sub == std::string::npos)
return {};
size_t start_of_filename = json.find("\"filename\"", start_of_sub);
if (start_of_filename == std::string::npos)
return {};
size_t filename_doubledot = json.find(':', start_of_filename);
if (filename_doubledot == std::string::npos)
return {};
size_t start_of_filename_data = json.find('\"', filename_doubledot);
if (start_of_filename_data == std::string::npos)
return {};
size_t end_of_filename_data = json.find('\"', start_of_filename_data + 1);
if (end_of_filename_data == std::string::npos)
return {};
size_t size = json.size();
std::string result = json.substr(start_of_sub, start_of_filename_data - start_of_sub + 1);
result += "%1%";
result += json.substr(end_of_filename_data, end_of_sub - end_of_filename_data);
result += ",\"size\":%2%}";
return result;
}
void UserAccount::fill_supported_printer_models_from_json(const std::string& json, std::vector<std::string>& result) const
{
try {

View File

@ -69,6 +69,7 @@ public:
// standalone utility methods
std::string get_nozzle_from_json(const std::string& message) const;
std::string get_keyword_from_json(const std::string& json, const std::string& keyword) const;
std::string get_print_data_from_json(const std::string &json, const std::string &keyword) const;
void fill_supported_printer_models_from_json(const std::string& json, std::vector<std::string>& result) const;
void fill_material_from_json(const std::string& json, std::vector<std::string>& result) const;

View File

@ -42,9 +42,7 @@ struct PrintHostUpload
PrintHostPostUploadAction post_action { PrintHostPostUploadAction::None };
std::string set_ready;
std::string position;
std::string wait_until;
std::string data_json;
};
class PrintHost

View File

@ -88,25 +88,30 @@ bool PrusaConnectNew::init_upload(PrintHostUpload upload_data, std::string& out)
boost::system::error_code ec;
boost::uintmax_t size = boost::filesystem::file_size(upload_data.source_path, ec);
const std::string name = get_name();
const std::string file_size = std::to_string(size);
const std::string access_token = GUI::wxGetApp().plater()->get_user_account()->get_access_token();
//const std::string upload_path = upload_data.upload_path.generic_string();
const std::string upload_filename = upload_data.upload_path.filename().string();
std::string url = GUI::format("%1%/app/users/teams/%2%/uploads", get_host(), m_team_id);
const std::string request_body_json = GUI::format(
"{"
"\"filename\": \"%1%\", "
"\"size\": %2%, "
"\"path\": \"%3%\", "
"\"force\": true, "
"\"printer_uuid\": \"%4%\""
"}"
, upload_filename
, file_size
, upload_data.upload_path.generic_string()
, m_uuid
);
std::string request_body_json = upload_data.data_json;
// GUI::format(
// "{"
// "\"filename\": \"%1%\", "
// "\"size\": %2%, "
// "\"path\": \"%3%\", "
// "\"force\": true, "
// "\"printer_uuid\": \"%4%\""
// "}"
// , upload_filename
// , file_size
// , upload_data.upload_path.generic_string()
// , m_uuid
//);
// replace plaholder filename
assert(request_body_json.find("%1%") != std::string::npos);
assert(request_body_json.find("%2%") != std::string::npos);
request_body_json = GUI::format(request_body_json, upload_filename, size);
BOOST_LOG_TRIVIAL(info) << "Register upload to "<< name<<". Url: " << url << "\nBody: " << request_body_json;
Http http = Http::post(std::move(url));
http.header("Authorization", "Bearer " + access_token)
@ -156,20 +161,20 @@ bool PrusaConnectNew::upload(PrintHostUpload upload_data, ProgressFn progress_fn
}
const std::string name = get_name();
const std::string access_token = GUI::wxGetApp().plater()->get_user_account()->get_access_token();
const std::string escaped_upload_path = upload_data.storage + "/" + escape_path_by_element(upload_data.upload_path.string());
const std::string set_ready = upload_data.set_ready.empty() ? "" : "&set_ready=" + upload_data.set_ready;
const std::string position = upload_data.position.empty() ? "" : "&position=" + upload_data.position;
const std::string wait_until = upload_data.wait_until.empty() ? "" : "&wait_until=" + upload_data.wait_until;
// const std::string escaped_upload_path = upload_data.storage + "/" + escape_path_by_element(upload_data.upload_path.string());
// const std::string set_ready = upload_data.set_ready.empty() ? "" : "&set_ready=" + upload_data.set_ready;
// const std::string position = upload_data.position.empty() ? "" : "&position=" + upload_data.position;
// const std::string wait_until = upload_data.wait_until.empty() ? "" : "&wait_until=" + upload_data.wait_until;
const std::string url = GUI::format(
"%1%/app/teams/%2%/files/raw"
"?upload_id=%3%"
"&force=true"
"&printer_uuid=%4%"
"&path=%5%"
"%6%"
"%7%"
"%8%"
, get_host(), m_team_id, upload_id, m_uuid, escaped_upload_path, set_ready, position, wait_until);
// "&force=true"
// "&printer_uuid=%4%"
// "&path=%5%"
// "%6%"
// "%7%"
// "%8%"
, get_host(), m_team_id, upload_id/*, m_uuid, escaped_upload_path, set_ready, position, wait_until*/);
bool res = true;
BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%")