Added X-Session-Key to DuetSoftwareFramework

This commit is contained in:
David Kocik 2023-04-17 14:36:47 +02:00
parent bda6f874f7
commit 2a4e09a2ea
2 changed files with 20 additions and 4 deletions

View File

@ -81,6 +81,8 @@ bool Duet::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn e
auto http = (dsf ? Http::put(std::move(upload_cmd)) : Http::post(std::move(upload_cmd))); auto http = (dsf ? Http::put(std::move(upload_cmd)) : Http::post(std::move(upload_cmd)));
if (dsf) { if (dsf) {
http.set_put_body(upload_data.source_path); http.set_put_body(upload_data.source_path);
if (connect_msg.empty())
http.header("X-Session-Key", boost::nowide::narrow(connect_msg));
} else { } else {
http.set_post_body(upload_data.source_path); http.set_post_body(upload_data.source_path);
} }
@ -140,7 +142,20 @@ Duet::ConnectionType Duet::connect(wxString &msg) const
msg = format_error(body, error, status); msg = format_error(body, error, status);
}) })
.on_complete([&](std::string body, unsigned) { .on_complete([&](std::string body, unsigned) {
res = ConnectionType::dsf; try {
pt::ptree root;
std::istringstream iss(body);
pt::read_json(iss, root);
auto key = root.get_optional<std::string>("sessionKey");
if (key)
msg = boost::nowide::widen(*key);
res = ConnectionType::dsf;
}
catch (const std::exception&) {
BOOST_LOG_TRIVIAL(error) << "Failed to parse serverKey from Duet reply to Connect request: " << body;
msg = format_error(body, L("Failed to parse a Connect reply"), 0);
res = ConnectionType::error;
}
}) })
.perform_sync(); .perform_sync();
}) })
@ -205,8 +220,9 @@ std::string Duet::get_upload_url(const std::string &filename, ConnectionType con
std::string Duet::get_connect_url(const bool dsfUrl) const std::string Duet::get_connect_url(const bool dsfUrl) const
{ {
if (dsfUrl) { if (dsfUrl) {
return (boost::format("%1%machine/status") return (boost::format("%1%machine/connect?password=%2%")
% get_base_url()).str(); % get_base_url()
% (password.empty() ? "reprap" : password)).str();
} else { } else {
return (boost::format("%1%rr_connect?password=%2%&%3%") return (boost::format("%1%rr_connect?password=%2%&%3%")
% get_base_url() % get_base_url()

View File

@ -35,7 +35,7 @@ public:
std::string get_host() const override { return host; } std::string get_host() const override { return host; }
private: private:
enum class ConnectionType { rrf, dsf, error }; enum class ConnectionType { rrf, dsf, error }; // rrf = RepRapFirmware, dsf = DuetSoftwareFramework
std::string host; std::string host;
std::string password; std::string password;