mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-07-29 15:02:00 +08:00
Merge branch 'dk_timer'
This commit is contained in:
commit
65f1a25854
@ -342,7 +342,7 @@ template<class T>
|
||||
struct NilValueTempl<T, std::enable_if_t<std::is_enum_v<T>, void>>
|
||||
{
|
||||
using NilType = T;
|
||||
static constexpr auto value = static_cast<T>(std::numeric_limits<std::underlying_type_t<T>>::max());
|
||||
static constexpr auto value = std::numeric_limits<std::underlying_type_t<T>>::max();
|
||||
};
|
||||
|
||||
template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_floating_point_v<T>, void>> {
|
||||
|
@ -519,7 +519,7 @@ struct Plater::priv
|
||||
void on_3dcanvas_mouse_dragging_finished(SimpleEvent&);
|
||||
|
||||
void show_action_buttons(const bool is_ready_to_slice) const;
|
||||
|
||||
bool can_show_upload_to_connect() const;
|
||||
// Set the bed shape to a single closed 2D polygon(array of two element arrays),
|
||||
// triangulate the bed and store the triangles into m_bed.m_triangles,
|
||||
// fills the m_bed.m_grid_lines and sets m_bed.m_origin.
|
||||
@ -889,6 +889,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
|
||||
});
|
||||
|
||||
this->q->Bind(EVT_UA_ID_USER_SUCCESS, [this](UserAccountSuccessEvent& evt) {
|
||||
// There are multiple handlers and we want to notify all
|
||||
evt.Skip();
|
||||
std::string username;
|
||||
if (user_account->on_user_id_success(evt.data, username)) {
|
||||
// login notification
|
||||
@ -984,6 +986,10 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
|
||||
this->notification_manager->close_notification_of_type(NotificationType::SelectFilamentFromConnect);
|
||||
this->notification_manager->push_notification(NotificationType::SelectFilamentFromConnect, NotificationManager::NotificationLevel::WarningNotificationLevel, msg);
|
||||
});
|
||||
|
||||
this->q->Bind(EVT_UA_REFRESH_TIME, [this](UserAccountTimeEvent& evt) {
|
||||
this->user_account->set_refresh_time(evt.data);
|
||||
});
|
||||
}
|
||||
|
||||
wxGetApp().other_instance_message_handler()->init(this->q);
|
||||
@ -3549,6 +3555,24 @@ bool Plater::priv::can_layers_editing() const
|
||||
return layers_height_allowed();
|
||||
}
|
||||
|
||||
bool Plater::priv::can_show_upload_to_connect() const
|
||||
{
|
||||
if (!user_account->is_logged()) {
|
||||
return false;
|
||||
}
|
||||
const Preset& selected_printer = wxGetApp().preset_bundle->printers.get_selected_preset();
|
||||
std::string vendor_id;
|
||||
if (selected_printer.vendor ) {
|
||||
vendor_id = selected_printer.vendor->id;
|
||||
} else if (std::string inherits = selected_printer.inherits(); !inherits.empty()) {
|
||||
const Preset* parent = wxGetApp().preset_bundle->printers.find_preset(inherits);
|
||||
if (parent && parent->vendor) {
|
||||
vendor_id = parent->vendor->id;
|
||||
}
|
||||
}
|
||||
return vendor_id.compare(0, 5, "Prusa") == 0;
|
||||
}
|
||||
|
||||
void Plater::priv::show_action_buttons(const bool ready_to_slice_) const
|
||||
{
|
||||
// Cache this value, so that the callbacks from the RemovableDriveManager may repeat that value when calling show_action_buttons().
|
||||
@ -3559,7 +3583,7 @@ void Plater::priv::show_action_buttons(const bool ready_to_slice_) const
|
||||
DynamicPrintConfig* selected_printer_config = wxGetApp().preset_bundle->physical_printers.get_selected_printer_config();
|
||||
const auto print_host_opt = selected_printer_config ? selected_printer_config->option<ConfigOptionString>("print_host") : nullptr;
|
||||
const bool send_gcode_shown = print_host_opt != nullptr && !print_host_opt->value.empty();
|
||||
const bool connect_gcode_shown = print_host_opt == nullptr && user_account->is_logged();
|
||||
const bool connect_gcode_shown = print_host_opt == nullptr && can_show_upload_to_connect();
|
||||
// when a background processing is ON, export_btn and/or send_btn are showing
|
||||
if (get_config_bool("background_processing"))
|
||||
{
|
||||
@ -5910,26 +5934,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));
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
||||
@ -78,6 +79,8 @@ public:
|
||||
std::string get_current_printer_uuid_from_connect(const std::string& selected_printer_id) const;
|
||||
|
||||
void set_current_printer_data(const std::string& data) { m_current_printer_data_json_from_connect = data; }
|
||||
|
||||
void set_refresh_time(int seconds) { m_communication->set_refresh_time(seconds); }
|
||||
private:
|
||||
void set_username(const std::string& username);
|
||||
|
||||
|
@ -132,22 +132,47 @@ bool load_secret(const std::string& opt, std::string& usr, std::string& psswd)
|
||||
}
|
||||
|
||||
UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, AppConfig* app_config)
|
||||
: m_evt_handler(evt_handler)
|
||||
: wxEvtHandler()
|
||||
, m_evt_handler(evt_handler)
|
||||
, m_app_config(app_config)
|
||||
, m_polling_timer(new wxTimer(this))
|
||||
, m_token_timer(new wxTimer(this))
|
||||
{
|
||||
std::string access_token, refresh_token, shared_session_key;
|
||||
Bind(wxEVT_TIMER, &UserAccountCommunication::on_token_timer, this, m_token_timer->GetId());
|
||||
Bind(wxEVT_TIMER, &UserAccountCommunication::on_polling_timer, this, m_polling_timer->GetId());
|
||||
|
||||
std::string access_token, refresh_token, shared_session_key, next_timeout;
|
||||
if (is_secret_store_ok()) {
|
||||
std::string key0, key1;
|
||||
load_secret("access_token", key0, access_token);
|
||||
load_secret("refresh_token", key1, refresh_token);
|
||||
assert(key0 == key1);
|
||||
std::string key0, key1, key2, tokens;
|
||||
if (load_secret("tokens", key0, tokens)) {
|
||||
std::vector<std::string> token_list;
|
||||
boost::split(token_list, tokens, boost::is_any_of("|"), boost::token_compress_off);
|
||||
assert(token_list.empty() || token_list.size() == 3);
|
||||
access_token = token_list.size() > 0 ? token_list[0] : std::string();
|
||||
refresh_token = token_list.size() > 1 ? token_list[1] : std::string();
|
||||
next_timeout = token_list.size() > 2 ? token_list[2] : std::string();
|
||||
} else {
|
||||
load_secret("access_token", key0, access_token);
|
||||
load_secret("refresh_token", key1, refresh_token);
|
||||
load_secret("access_token_timeout", key2, next_timeout);
|
||||
assert(key0 == key1);
|
||||
}
|
||||
shared_session_key = key0;
|
||||
|
||||
} else {
|
||||
access_token = m_app_config->get("access_token");
|
||||
refresh_token = m_app_config->get("refresh_token");
|
||||
shared_session_key = m_app_config->get("shared_session_key");
|
||||
next_timeout = m_app_config->get("access_token_timeout");
|
||||
}
|
||||
bool has_token = !access_token.empty() && !refresh_token.empty();
|
||||
long long next = next_timeout.empty() ? 0 : std::stoll(next_timeout);
|
||||
long long remain_time = next - std::time(nullptr);
|
||||
if (remain_time <= 0) {
|
||||
access_token.clear();
|
||||
} else {
|
||||
set_refresh_time((int)remain_time);
|
||||
}
|
||||
bool has_token = !refresh_token.empty();
|
||||
m_session = std::make_unique<UserAccountSession>(evt_handler, access_token, refresh_token, shared_session_key, m_app_config->get_bool("connect_polling"));
|
||||
init_session_thread();
|
||||
// perform login at the start, but only with tokens
|
||||
@ -157,6 +182,8 @@ UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, Ap
|
||||
|
||||
UserAccountCommunication::~UserAccountCommunication()
|
||||
{
|
||||
m_token_timer->Stop();
|
||||
m_polling_timer->Stop();
|
||||
if (m_thread.joinable()) {
|
||||
// Stop the worker thread, if running.
|
||||
{
|
||||
@ -165,7 +192,7 @@ UserAccountCommunication::~UserAccountCommunication()
|
||||
m_thread_stop = true;
|
||||
}
|
||||
m_thread_stop_condition.notify_all();
|
||||
// Wait for the worker thread to stop.
|
||||
// Wait for the worker thread to stop
|
||||
m_thread.join();
|
||||
}
|
||||
}
|
||||
@ -176,13 +203,19 @@ void UserAccountCommunication::set_username(const std::string& username)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_session_mutex);
|
||||
if (is_secret_store_ok()) {
|
||||
save_secret("access_token", m_session->get_shared_session_key(), m_remember_session ? m_session->get_access_token() : std::string());
|
||||
save_secret("refresh_token", m_session->get_shared_session_key(), m_remember_session ? m_session->get_refresh_token() : std::string());
|
||||
std::string tokens;
|
||||
if (m_remember_session) {
|
||||
tokens = m_session->get_access_token() +
|
||||
"|" + m_session->get_refresh_token() +
|
||||
"|" + std::to_string(m_session->get_next_token_timeout());
|
||||
}
|
||||
save_secret("tokens", m_session->get_shared_session_key(), tokens);
|
||||
}
|
||||
else {
|
||||
m_app_config->set("access_token", m_remember_session ? m_session->get_access_token() : std::string());
|
||||
m_app_config->set("refresh_token", m_remember_session ? m_session->get_refresh_token() : std::string());
|
||||
m_app_config->set("shared_session_key", m_remember_session ? m_session->get_shared_session_key() : std::string());
|
||||
m_app_config->set("access_token_timeout", m_remember_session ? GUI::format("%1%", m_session->get_next_token_timeout()) : "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -271,6 +304,7 @@ void UserAccountCommunication::do_clear()
|
||||
m_session->clear();
|
||||
}
|
||||
set_username({});
|
||||
m_token_timer->Stop();
|
||||
}
|
||||
|
||||
void UserAccountCommunication::on_login_code_recieved(const std::string& url_message)
|
||||
@ -346,16 +380,28 @@ void UserAccountCommunication::enqueue_printer_data_action(const std::string& uu
|
||||
}
|
||||
wakeup_session_thread();
|
||||
}
|
||||
|
||||
void UserAccountCommunication::enqueue_refresh()
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_session_mutex);
|
||||
if (!m_session->is_initialized()) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
|
||||
return;
|
||||
}
|
||||
m_session->enqueue_refresh({});
|
||||
}
|
||||
wakeup_session_thread();
|
||||
}
|
||||
|
||||
void UserAccountCommunication::init_session_thread()
|
||||
{
|
||||
assert(m_polling_timer);
|
||||
m_polling_timer->Start(10000);
|
||||
m_thread = std::thread([this]() {
|
||||
for (;;) {
|
||||
// Wait for 5 seconds or wakeup call
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(m_thread_stop_mutex);
|
||||
m_thread_stop_condition.wait_for(lck, std::chrono::seconds(10), [this] { return m_thread_stop || m_thread_wakeup; });
|
||||
m_thread_stop_condition.wait_for(lck, std::chrono::seconds(88888), [this] { return m_thread_stop || m_thread_wakeup; });
|
||||
}
|
||||
if (m_thread_stop)
|
||||
// Stop the worker thread.
|
||||
@ -375,8 +421,10 @@ void UserAccountCommunication::init_session_thread()
|
||||
|
||||
void UserAccountCommunication::on_activate_window(bool active)
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(m_thread_stop_mutex);
|
||||
m_window_is_active = active;
|
||||
{
|
||||
std::lock_guard<std::mutex> lck(m_thread_stop_mutex);
|
||||
m_window_is_active = active;
|
||||
}
|
||||
}
|
||||
|
||||
void UserAccountCommunication::wakeup_session_thread()
|
||||
@ -388,6 +436,26 @@ void UserAccountCommunication::wakeup_session_thread()
|
||||
m_thread_stop_condition.notify_all();
|
||||
}
|
||||
|
||||
void UserAccountCommunication::set_refresh_time(int seconds)
|
||||
{
|
||||
assert(m_token_timer);
|
||||
m_token_timer->Stop();
|
||||
int miliseconds = std::max(seconds * 1000 - 66666, 60000);
|
||||
m_token_timer->StartOnce(miliseconds);
|
||||
}
|
||||
|
||||
void UserAccountCommunication::on_token_timer(wxTimerEvent& evt)
|
||||
{
|
||||
enqueue_refresh();
|
||||
}
|
||||
void UserAccountCommunication::on_polling_timer(wxTimerEvent& evt)
|
||||
{
|
||||
if (!m_window_is_active) {
|
||||
return;
|
||||
}
|
||||
wakeup_session_thread();
|
||||
}
|
||||
|
||||
std::string CodeChalengeGenerator::generate_chalenge(const std::string& verifier)
|
||||
{
|
||||
std::string code_challenge;
|
||||
|
@ -27,7 +27,8 @@ private:
|
||||
std::string sha256(const std::string& input);
|
||||
};
|
||||
|
||||
class UserAccountCommunication {
|
||||
class UserAccountCommunication : public wxEvtHandler
|
||||
{
|
||||
public:
|
||||
UserAccountCommunication(wxEvtHandler* evt_handler, AppConfig* app_config);
|
||||
~UserAccountCommunication();
|
||||
@ -44,6 +45,7 @@ public:
|
||||
void enqueue_avatar_action(const std::string& url);
|
||||
void enqueue_test_connection();
|
||||
void enqueue_printer_data_action(const std::string& uuid);
|
||||
void enqueue_refresh();
|
||||
|
||||
// Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread.
|
||||
//
|
||||
@ -64,6 +66,10 @@ public:
|
||||
void set_polling_enabled(bool enabled);
|
||||
// we have map of uuids and printer_models - set polling action to lightweight STATUS action
|
||||
void on_uuid_map_success();
|
||||
|
||||
void set_refresh_time(int seconds);
|
||||
void on_token_timer(wxTimerEvent& evt);
|
||||
void on_polling_timer(wxTimerEvent& evt);
|
||||
private:
|
||||
std::unique_ptr<UserAccountSession> m_session;
|
||||
std::thread m_thread;
|
||||
@ -73,6 +79,8 @@ private:
|
||||
bool m_thread_stop { false };
|
||||
bool m_thread_wakeup{ false };
|
||||
bool m_window_is_active{ true };
|
||||
wxTimer* m_polling_timer;
|
||||
|
||||
std::string m_code_verifier;
|
||||
wxEvtHandler* m_evt_handler;
|
||||
AppConfig* m_app_config;
|
||||
@ -80,11 +88,14 @@ private:
|
||||
std::string m_username;
|
||||
bool m_remember_session { true }; // if default is true, on every login Remember me will be checked.
|
||||
|
||||
wxTimer* m_token_timer;
|
||||
wxEvtHandler* m_timer_evt_handler;
|
||||
|
||||
void wakeup_session_thread();
|
||||
void init_session_thread();
|
||||
void login_redirect();
|
||||
std::string client_id() const { return "oamhmhZez7opFosnwzElIgE2oGgI2iJORSkw587O"; }
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
@ -29,6 +29,7 @@ wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, UserAccountSuccessEvent
|
||||
wxDEFINE_EVENT(EVT_UA_FAIL, UserAccountFailEvent);
|
||||
wxDEFINE_EVENT(EVT_UA_RESET, UserAccountFailEvent);
|
||||
wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL, UserAccountFailEvent);
|
||||
wxDEFINE_EVENT(EVT_UA_REFRESH_TIME, UserAccountTimeEvent);
|
||||
|
||||
void UserActionPost::perform(/*UNUSED*/ wxEvtHandler* evt_handler, /*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) const
|
||||
{
|
||||
@ -124,6 +125,7 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
||||
{
|
||||
// Data we need
|
||||
std::string access_token, refresh_token, shared_session_key;
|
||||
int expires_in = 300;
|
||||
try {
|
||||
std::stringstream ss(body);
|
||||
pt::ptree ptree;
|
||||
@ -132,6 +134,7 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
||||
const auto access_token_optional = ptree.get_optional<std::string>("access_token");
|
||||
const auto refresh_token_optional = ptree.get_optional<std::string>("refresh_token");
|
||||
const auto shared_session_key_optional = ptree.get_optional<std::string>("shared_session_key");
|
||||
const auto expires_in_optional = ptree.get_optional<int>("expires_in");
|
||||
|
||||
if (access_token_optional)
|
||||
access_token = *access_token_optional;
|
||||
@ -139,6 +142,9 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
||||
refresh_token = *refresh_token_optional;
|
||||
if (shared_session_key_optional)
|
||||
shared_session_key = *shared_session_key_optional;
|
||||
assert(expires_in_optional);
|
||||
if (expires_in_optional)
|
||||
expires_in = *expires_in_optional;
|
||||
}
|
||||
catch (const std::exception&) {
|
||||
std::string msg = "Could not parse server response after code exchange.";
|
||||
@ -163,7 +169,9 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
||||
m_access_token = access_token;
|
||||
m_refresh_token = refresh_token;
|
||||
m_shared_session_key = shared_session_key;
|
||||
m_next_token_timeout = std::time(nullptr) + expires_in;
|
||||
enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID, nullptr, nullptr, {});
|
||||
wxQueueEvent(p_evt_handler, new UserAccountTimeEvent(EVT_UA_REFRESH_TIME, expires_in));
|
||||
}
|
||||
|
||||
void UserAccountSession::code_exchange_fail_callback(const std::string& body)
|
||||
@ -181,6 +189,7 @@ void UserAccountSession::enqueue_test_with_refresh()
|
||||
m_priority_action_queue.push({ UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN, nullptr, std::bind(&UserAccountSession::enqueue_refresh, this, std::placeholders::_1), {} });
|
||||
}
|
||||
|
||||
|
||||
void UserAccountSession::enqueue_refresh(const std::string& body)
|
||||
{
|
||||
assert(!m_refresh_token.empty());
|
||||
|
@ -16,6 +16,7 @@ namespace GUI {
|
||||
using OpenPrusaAuthEvent = Event<wxString>;
|
||||
using UserAccountSuccessEvent = Event<std::string>;
|
||||
using UserAccountFailEvent = Event<std::string>;
|
||||
using UserAccountTimeEvent = Event<int>;
|
||||
wxDECLARE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent);
|
||||
wxDECLARE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent);
|
||||
wxDECLARE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent);
|
||||
@ -27,7 +28,7 @@ wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, UserAccountSuccessEven
|
||||
wxDECLARE_EVENT(EVT_UA_FAIL, UserAccountFailEvent); // Soft fail - clears only after some number of fails
|
||||
wxDECLARE_EVENT(EVT_UA_RESET, UserAccountFailEvent); // Hard fail - clears all
|
||||
wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL, UserAccountFailEvent); // Failed to get data for printer to select, soft fail, action does not repeat
|
||||
|
||||
wxDECLARE_EVENT(EVT_UA_REFRESH_TIME, UserAccountTimeEvent);
|
||||
|
||||
typedef std::function<void(const std::string& body)> UserActionSuccessFn;
|
||||
typedef std::function<void(const std::string& body)> UserActionFailFn;
|
||||
@ -144,18 +145,19 @@ public:
|
||||
void enqueue_action(UserAccountActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input);
|
||||
// Special enques, that sets callbacks.
|
||||
void enqueue_test_with_refresh();
|
||||
void enqueue_refresh(const std::string& body);
|
||||
|
||||
void process_action_queue();
|
||||
bool is_initialized() { return !m_access_token.empty() || !m_refresh_token.empty(); }
|
||||
std::string get_access_token() const { return m_access_token; }
|
||||
std::string get_refresh_token() const { return m_refresh_token; }
|
||||
std::string get_shared_session_key() const { return m_shared_session_key; }
|
||||
|
||||
long long get_next_token_timeout() const {return m_next_token_timeout; }
|
||||
|
||||
//void set_polling_enabled(bool enabled) {m_polling_action = enabled ? UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS : UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY; }
|
||||
void set_polling_action(UserAccountActionID action) { m_polling_action = action; }
|
||||
private:
|
||||
|
||||
void enqueue_refresh(const std::string& body);
|
||||
|
||||
void refresh_fail_callback(const std::string& body);
|
||||
void cancel_queue();
|
||||
void code_exchange_fail_callback(const std::string& body);
|
||||
@ -172,6 +174,7 @@ private:
|
||||
std::string m_access_token;
|
||||
std::string m_refresh_token;
|
||||
std::string m_shared_session_key;
|
||||
long long m_next_token_timeout;
|
||||
|
||||
std::queue<ActionQueueData> m_action_queue;
|
||||
std::queue<ActionQueueData> m_priority_action_queue;
|
||||
|
@ -22,7 +22,7 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, const wxString& url, std::v
|
||||
if (webView) {
|
||||
wxString correct_url = url.empty() ? wxString("") : wxURI(url).BuildURI();
|
||||
|
||||
#ifdef __WIN32_
|
||||
#ifdef __WIN32__
|
||||
webView->SetUserAgent(SLIC3R_APP_FULL_NAME);
|
||||
webView->Create(parent, wxID_ANY, correct_url, wxDefaultPosition, wxDefaultSize);
|
||||
//We register the wxfs:// protocol for testing purposes
|
||||
|
@ -15,6 +15,11 @@
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
// if set to 1 the fetch() JS function gets override to include JWT in authorization header
|
||||
// if set to 0, the /slicer/login is invoked from WebKit (passing JWT token only to this request)
|
||||
// to set authorization cookie for all WebKit requests to Connect
|
||||
#define AUTH_VIA_FETCH_OVERRIDE 0
|
||||
|
||||
|
||||
namespace pt = boost::property_tree;
|
||||
|
||||
@ -102,6 +107,7 @@ WebViewPanel::WebViewPanel(wxWindow *parent, const wxString& default_url, const
|
||||
// Connect the webview events
|
||||
Bind(wxEVT_WEBVIEW_ERROR, &WebViewPanel::on_error, this, m_browser->GetId());
|
||||
Bind(wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, &WebViewPanel::on_script_message, this, m_browser->GetId());
|
||||
Bind(wxEVT_WEBVIEW_NAVIGATING, &WebViewPanel::on_navigation_request, this, m_browser->GetId());
|
||||
|
||||
#ifdef DEBUG_URL_PANEL
|
||||
// Connect the button events
|
||||
@ -248,6 +254,10 @@ void WebViewPanel::on_script_message(wxWebViewEvent& evt)
|
||||
{
|
||||
}
|
||||
|
||||
void WebViewPanel::on_navigation_request(wxWebViewEvent &evt)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoked when user selects the "View Source" menu item
|
||||
*/
|
||||
@ -463,13 +473,11 @@ SourceViewDialog::SourceViewDialog(wxWindow* parent, wxString source) :
|
||||
|
||||
ConnectRequestHandler::ConnectRequestHandler()
|
||||
{
|
||||
m_actions["REQUEST_ACCESS_TOKEN"] = std::bind(&ConnectRequestHandler::on_connect_action_request_access_token, this);
|
||||
m_actions["REQUEST_CONFIG"] = std::bind(&ConnectRequestHandler::on_connect_action_request_config, this);
|
||||
m_actions["WEBAPP_READY"] = std::bind(&ConnectRequestHandler::on_connect_action_webapp_ready, this);
|
||||
m_actions["SELECT_PRINTER"] = std::bind(&ConnectRequestHandler::on_connect_action_select_printer, this);
|
||||
m_actions["PRINT"] = std::bind(&ConnectRequestHandler::on_connect_action_print, this);
|
||||
// obsolete
|
||||
//m_actions["REQUEST_SELECTED_PRINTER"] = std::bind(&ConnectRequestHandler::on_connect_action_print, this);
|
||||
m_actions["REQUEST_CONFIG"] = std::bind(&ConnectRequestHandler::on_connect_action_request_config, this, std::placeholders::_1);
|
||||
m_actions["WEBAPP_READY"] = std::bind(&ConnectRequestHandler::on_connect_action_webapp_ready,this, std::placeholders::_1);
|
||||
m_actions["SELECT_PRINTER"] = std::bind(&ConnectRequestHandler::on_connect_action_select_printer, this, std::placeholders::_1);
|
||||
m_actions["PRINT"] = std::bind(&ConnectRequestHandler::on_connect_action_print, this, std::placeholders::_1);
|
||||
m_actions["REQUEST_OPEN_IN_BROWSER"] = std::bind(&ConnectRequestHandler::on_connect_action_request_open_in_browser, this, std::placeholders::_1);
|
||||
}
|
||||
ConnectRequestHandler::~ConnectRequestHandler()
|
||||
{
|
||||
@ -484,9 +492,8 @@ void ConnectRequestHandler::handle_message(const std::string& message)
|
||||
{"action":"REQUEST_ACCESS_TOKEN"}
|
||||
*/
|
||||
std::string action_string;
|
||||
m_message_data = message;
|
||||
try {
|
||||
std::stringstream ss(m_message_data);
|
||||
std::stringstream ss(message);
|
||||
pt::ptree ptree;
|
||||
pt::read_json(ss, ptree);
|
||||
// v1:
|
||||
@ -505,23 +512,16 @@ void ConnectRequestHandler::handle_message(const std::string& message)
|
||||
}
|
||||
assert(m_actions.find(action_string) != m_actions.end()); // this assert means there is a action that has no handling.
|
||||
if (m_actions.find(action_string) != m_actions.end()) {
|
||||
m_actions[action_string]();
|
||||
m_actions[action_string](message);
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectRequestHandler::resend_config()
|
||||
{
|
||||
on_connect_action_request_config();
|
||||
on_connect_action_request_config({});
|
||||
}
|
||||
|
||||
void ConnectRequestHandler::on_connect_action_request_access_token()
|
||||
{
|
||||
std::string token = wxGetApp().plater()->get_user_account()->get_access_token();
|
||||
wxString script = GUI::format_wxstr("window._prusaConnect_v1.setAccessToken(\'%1%\')", token);
|
||||
run_script_bridge(script);
|
||||
}
|
||||
|
||||
void ConnectRequestHandler::on_connect_action_request_config()
|
||||
void ConnectRequestHandler::on_connect_action_request_config(const std::string& message_data)
|
||||
{
|
||||
/*
|
||||
accessToken?: string;
|
||||
@ -535,15 +535,104 @@ void ConnectRequestHandler::on_connect_action_request_config()
|
||||
const std::string dark_mode = wxGetApp().dark_mode() ? "DARK" : "LIGHT";
|
||||
wxString language = GUI::wxGetApp().current_language_code();
|
||||
language = language.SubString(0, 1);
|
||||
const std::string init_options = GUI::format("{\"accessToken\": \"%1%\" , \"clientVersion\": \"%2%\", \"colorMode\": \"%3%\", \"language\": \"%4%\"}", token, SLIC3R_VERSION, dark_mode, language);
|
||||
const std::string init_options = GUI::format("{\"accessToken\": \"%4%\",\"clientVersion\": \"%1%\", \"colorMode\": \"%2%\", \"language\": \"%3%\"}", SLIC3R_VERSION, dark_mode, language, token );
|
||||
wxString script = GUI::format_wxstr("window._prusaConnect_v1.init(%1%)", init_options);
|
||||
run_script_bridge(script);
|
||||
|
||||
}
|
||||
void ConnectRequestHandler::on_connect_action_request_open_in_browser(const std::string& message_data)
|
||||
{
|
||||
try {
|
||||
std::stringstream ss(message_data);
|
||||
pt::ptree ptree;
|
||||
pt::read_json(ss, ptree);
|
||||
if (const auto url = ptree.get_optional<std::string>("url"); url) {
|
||||
wxGetApp().open_browser_with_warning_dialog(GUI::from_u8(*url));
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
BOOST_LOG_TRIVIAL(error) << "Could not parse _prusaConnect message. " << e.what();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ConnectWebViewPanel::ConnectWebViewPanel(wxWindow* parent)
|
||||
: WebViewPanel(parent, L"https://connect.prusa3d.com/", { "_prusaSlicer" }, "connect_loading")
|
||||
{
|
||||
//m_browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new WebViewHandler("https")));
|
||||
|
||||
Plater* plater = wxGetApp().plater();
|
||||
m_browser->AddUserScript(wxString::Format(
|
||||
|
||||
#if AUTH_VIA_FETCH_OVERRIDE
|
||||
/*
|
||||
* Notes:
|
||||
* - The fetch() function has two distinct prototypes (i.e. input args):
|
||||
* 1. fetch(url: string, options: object | undefined)
|
||||
* 2. fetch(req: Request, options: object | undefined)
|
||||
* - For some reason I can't explain the headers can be extended only via Request object
|
||||
* i.e. the fetch prototype (2). So we need to convert (1) call into (2) before
|
||||
*
|
||||
*/
|
||||
R"(
|
||||
if (window.__fetch === undefined) {
|
||||
window.__fetch = fetch;
|
||||
window.fetch = function(req, opts = {}) {
|
||||
if (typeof req === 'string') {
|
||||
req = new Request(req, opts);
|
||||
opts = {};
|
||||
}
|
||||
if (window.__access_token && (req.url[0] == '/' || req.url.indexOf('prusa3d.com') > 0)) {
|
||||
req.headers.set('Authorization', 'Bearer ' + window.__access_token);
|
||||
console.log('Header updated: ', req.headers.get('Authorization'));
|
||||
console.log('AT Version: ', __access_token_version);
|
||||
}
|
||||
//console.log('Injected fetch used', req, opts);
|
||||
return __fetch(req, opts);
|
||||
};
|
||||
}
|
||||
window.__access_token = '%s';
|
||||
window.__access_token_version = 0;
|
||||
)",
|
||||
#else
|
||||
R"(
|
||||
console.log('Preparing login');
|
||||
window.fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
|
||||
.then((resp) => {
|
||||
console.log('Login resp', resp);
|
||||
resp.text().then((json) => console.log('Login resp body', json));
|
||||
});
|
||||
)",
|
||||
#endif
|
||||
plater->get_user_account()->get_access_token()
|
||||
));
|
||||
plater->Bind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
|
||||
}
|
||||
|
||||
ConnectWebViewPanel::~ConnectWebViewPanel()
|
||||
{
|
||||
m_browser->Unbind(EVT_UA_ID_USER_SUCCESS, &ConnectWebViewPanel::on_user_token, this);
|
||||
}
|
||||
|
||||
void ConnectWebViewPanel::on_user_token(UserAccountSuccessEvent& e)
|
||||
{
|
||||
e.Skip();
|
||||
wxString javascript = wxString::Format(
|
||||
#if AUTH_VIA_FETCH_OVERRIDE
|
||||
"window.__access_token = '%s';window.__access_token_version = (window.__access_token_version || 0) + 1;console.log('Updated Auth token', window.__access_token);",
|
||||
#else
|
||||
R"(
|
||||
console.log('Preparing login');
|
||||
window.fetch('/slicer/login', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
|
||||
.then((resp) => {
|
||||
console.log('Login resp', resp);
|
||||
resp.text().then((json) => console.log('Login resp body', json));
|
||||
});
|
||||
)",
|
||||
#endif
|
||||
wxGetApp().plater()->get_user_account()->get_access_token()
|
||||
);
|
||||
//m_browser->AddUserScript(javascript, wxWEBVIEW_INJECT_AT_DOCUMENT_END);
|
||||
m_browser->RunScriptAsync(javascript);
|
||||
}
|
||||
|
||||
void ConnectWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
||||
@ -551,11 +640,37 @@ void ConnectWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
||||
BOOST_LOG_TRIVIAL(debug) << "recieved message from PrusaConnect FE: " << evt.GetString();
|
||||
handle_message(into_u8(evt.GetString()));
|
||||
}
|
||||
|
||||
void ConnectWebViewPanel::on_navigation_request(wxWebViewEvent &evt)
|
||||
{
|
||||
if (evt.GetURL() == m_default_url) {
|
||||
m_reached_default_url = true;
|
||||
return;
|
||||
}
|
||||
if (evt.GetURL() == (GUI::format_wxstr("file:///%1%/web/connection_failed.html", boost::filesystem::path(resources_dir()).generic_string()))) {
|
||||
return;
|
||||
}
|
||||
if (m_reached_default_url && !evt.GetURL().StartsWith(m_default_url)) {
|
||||
BOOST_LOG_TRIVIAL(info) << evt.GetURL() << " does not start with default url. Vetoing.";
|
||||
evt.Veto();
|
||||
}
|
||||
}
|
||||
void ConnectWebViewPanel::logout()
|
||||
{
|
||||
wxString script = L"window._prusaConnect_v1.logout()";
|
||||
run_script(script);
|
||||
|
||||
Plater* plater = wxGetApp().plater();
|
||||
m_browser->RunScript(wxString::Format(
|
||||
R"(
|
||||
console.log('Preparing login');
|
||||
window.fetch('/slicer/logout', {method: 'POST', headers: {Authorization: 'Bearer %s'}})
|
||||
.then((resp) => {
|
||||
console.log('Login resp', resp);
|
||||
resp.text().then((json) => console.log('Login resp body', json));
|
||||
});
|
||||
)",
|
||||
plater->get_user_account()->get_access_token()
|
||||
));
|
||||
}
|
||||
|
||||
void ConnectWebViewPanel::sys_color_changed()
|
||||
@ -563,12 +678,12 @@ void ConnectWebViewPanel::sys_color_changed()
|
||||
resend_config();
|
||||
}
|
||||
|
||||
void ConnectWebViewPanel::on_connect_action_select_printer()
|
||||
void ConnectWebViewPanel::on_connect_action_select_printer(const std::string& message_data)
|
||||
{
|
||||
assert(!m_message_data.empty());
|
||||
wxGetApp().handle_connect_request_printer_select(m_message_data);
|
||||
assert(!message_data.empty());
|
||||
wxGetApp().handle_connect_request_printer_select(message_data);
|
||||
}
|
||||
void ConnectWebViewPanel::on_connect_action_print()
|
||||
void ConnectWebViewPanel::on_connect_action_print(const std::string& message_data)
|
||||
{
|
||||
// PRINT request is not defined for ConnectWebViewPanel
|
||||
assert(true);
|
||||
@ -1065,18 +1180,18 @@ void PrinterPickWebViewDialog::on_script_message(wxWebViewEvent& evt)
|
||||
handle_message(into_u8(evt.GetString()));
|
||||
}
|
||||
|
||||
void PrinterPickWebViewDialog::on_connect_action_select_printer()
|
||||
void PrinterPickWebViewDialog::on_connect_action_select_printer(const std::string& message_data)
|
||||
{
|
||||
// SELECT_PRINTER request is not defined for PrinterPickWebViewDialog
|
||||
assert(true);
|
||||
}
|
||||
void PrinterPickWebViewDialog::on_connect_action_print()
|
||||
void PrinterPickWebViewDialog::on_connect_action_print(const std::string& message_data)
|
||||
{
|
||||
m_ret_val = m_message_data;
|
||||
m_ret_val = message_data;
|
||||
this->EndModal(wxID_OK);
|
||||
}
|
||||
|
||||
void PrinterPickWebViewDialog::on_connect_action_webapp_ready()
|
||||
void PrinterPickWebViewDialog::on_connect_action_webapp_ready(const std::string& message_data)
|
||||
{
|
||||
|
||||
if (Preset::printer_technology(wxGetApp().preset_bundle->printers.get_selected_preset().config) == ptFFF) {
|
||||
|
@ -1,18 +1,24 @@
|
||||
#ifndef slic3r_WebViewDialog_hpp_
|
||||
#define slic3r_WebViewDialog_hpp_
|
||||
|
||||
//#define DEBUG_URL_PANEL
|
||||
|
||||
#include <map>
|
||||
#include <wx/wx.h>
|
||||
#include <wx/event.h>
|
||||
|
||||
#include "UserAccountSession.hpp"
|
||||
|
||||
#ifdef DEBUG_URL_PANEL
|
||||
#include <wx/infobar.h>
|
||||
#endif
|
||||
|
||||
class wxWebView;
|
||||
class wxWebViewEvent;
|
||||
|
||||
namespace Slic3r {
|
||||
namespace GUI {
|
||||
|
||||
//#define DEBUG_URL_PANEL
|
||||
|
||||
class WebViewPanel : public wxPanel
|
||||
{
|
||||
public:
|
||||
@ -47,6 +53,7 @@ public:
|
||||
void on_select_all(wxCommandEvent& evt);
|
||||
void On_enable_context_menu(wxCommandEvent& evt);
|
||||
void On_enable_dev_tools(wxCommandEvent& evt);
|
||||
virtual void on_navigation_request(wxWebViewEvent &evt);
|
||||
|
||||
wxString get_default_url() const { return m_default_url; }
|
||||
void set_default_url(const wxString& url) { m_default_url = url; }
|
||||
@ -167,30 +174,33 @@ public:
|
||||
void resend_config();
|
||||
protected:
|
||||
// action callbacs stored in m_actions
|
||||
virtual void on_connect_action_request_access_token();
|
||||
virtual void on_connect_action_request_config();
|
||||
virtual void on_connect_action_select_printer() = 0;
|
||||
virtual void on_connect_action_print() = 0;
|
||||
virtual void run_script_bridge(const wxString& script) = 0;
|
||||
virtual void on_connect_action_webapp_ready() = 0;
|
||||
|
||||
std::map<std::string, std::function<void(void)>> m_actions;
|
||||
std::string m_message_data;
|
||||
virtual void on_connect_action_request_config(const std::string& message_data);
|
||||
virtual void on_connect_action_request_open_in_browser(const std::string& message_data);
|
||||
virtual void on_connect_action_select_printer(const std::string& message_data) = 0;
|
||||
virtual void on_connect_action_print(const std::string& message_data) = 0;
|
||||
virtual void on_connect_action_webapp_ready(const std::string& message_data) = 0;
|
||||
virtual void run_script_bridge(const wxString &script) = 0;
|
||||
|
||||
std::map<std::string, std::function<void(const std::string&)>> m_actions;
|
||||
};
|
||||
|
||||
class ConnectWebViewPanel : public WebViewPanel, public ConnectRequestHandler
|
||||
{
|
||||
public:
|
||||
ConnectWebViewPanel(wxWindow* parent);
|
||||
~ConnectWebViewPanel() override;
|
||||
void on_script_message(wxWebViewEvent& evt) override;
|
||||
void logout();
|
||||
void sys_color_changed() override;
|
||||
void on_navigation_request(wxWebViewEvent &evt) override;
|
||||
protected:
|
||||
void on_connect_action_select_printer() override;
|
||||
void on_connect_action_print() override;
|
||||
void on_connect_action_webapp_ready() override {}
|
||||
void on_connect_action_select_printer(const std::string& message_data) override;
|
||||
void on_connect_action_print(const std::string& message_data) override;
|
||||
void on_connect_action_webapp_ready(const std::string& message_data) override {}
|
||||
void run_script_bridge(const wxString& script) override {run_script(script); }
|
||||
private:
|
||||
void on_user_token(UserAccountSuccessEvent& e);
|
||||
bool m_reached_default_url {false};
|
||||
};
|
||||
|
||||
class PrinterWebViewPanel : public WebViewPanel
|
||||
@ -220,9 +230,9 @@ public:
|
||||
void on_show(wxShowEvent& evt) override;
|
||||
void on_script_message(wxWebViewEvent& evt) override;
|
||||
protected:
|
||||
void on_connect_action_select_printer() override;
|
||||
void on_connect_action_print() override;
|
||||
void on_connect_action_webapp_ready() override;
|
||||
void on_connect_action_select_printer(const std::string& message_data) override;
|
||||
void on_connect_action_print(const std::string& message_data) override;
|
||||
void on_connect_action_webapp_ready(const std::string& message_data) override;
|
||||
void request_compatible_printers_FFF();
|
||||
void request_compatible_printers_SLA();
|
||||
void run_script_bridge(const wxString& script) override { run_script(script); }
|
||||
|
@ -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
|
||||
|
@ -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%")
|
||||
|
Loading…
x
Reference in New Issue
Block a user