forked from ClusterM/open-bamboo-networking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_presets.cpp
More file actions
411 lines (364 loc) · 15.5 KB
/
cloud_presets.cpp
File metadata and controls
411 lines (364 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "obn/cloud_presets.hpp"
#include "obn/agent.hpp"
#include "obn/bambu_networking.hpp"
#include "obn/cloud_auth.hpp"
#include "obn/http_client.hpp"
#include "obn/json_lite.hpp"
#include "obn/log.hpp"
#include "obn/os_compat.hpp"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <sstream>
namespace obn::cloud_presets {
namespace {
std::string api_base(Agent* a)
{
return obn::cloud::api_host(a->cloud_region());
}
std::string settings_base(Agent* a)
{
return api_base(a) + "/v1/iot-service/api/slicer/setting";
}
// Stringify any json value back to a flat scalar string, matching the
// way PresetCollection::get_differed_values_to_update stored options
// (serialize()). Arrays/objects fall back to compact JSON text so we
// don't lose information (e.g. if the server ever starts returning
// typed values instead of pre-stringified ones).
std::string json_scalar_to_string(const obn::json::Value& v)
{
using K = obn::json::Value::Kind;
switch (v.kind()) {
case K::Null: return {};
case K::Bool: return v.as_bool() ? "1" : "0";
case K::String: return v.as_string();
case K::Number: {
double d = v.as_number();
// Prefer integer rendering when the value has no fractional
// part: Bambu's config options are overwhelmingly ints.
long long as_int = static_cast<long long>(d);
if (static_cast<double>(as_int) == d) {
return std::to_string(as_int);
}
std::ostringstream os;
os.precision(17);
os << d;
return os.str();
}
case K::Array:
case K::Object:
return v.dump();
}
return {};
}
// "2026-04-21 17:11:27" (UTC) -> unix seconds. Returns 0 on parse fail.
std::int64_t parse_iso_utc(const std::string& s)
{
if (s.empty()) return 0;
// strptime is POSIX-only; MSVC has no equivalent. The format is fixed
// ("YYYY-MM-DD HH:MM:SS"), so a tiny sscanf does the job and is more
// portable than dragging in std::get_time / strptime polyfills.
int y = 0, mo = 0, d = 0, h = 0, mi = 0, se = 0;
if (std::sscanf(s.c_str(), "%4d-%2d-%2d %2d:%2d:%2d",
&y, &mo, &d, &h, &mi, &se) != 6) {
return 0;
}
std::tm t{};
t.tm_year = y - 1900;
t.tm_mon = mo - 1;
t.tm_mday = d;
t.tm_hour = h;
t.tm_min = mi;
t.tm_sec = se;
// The struct is interpreted as UTC; timegm()/_mkgmtime() do not apply
// a TZ offset. obn::os::timegm_safe maps to the right one per platform.
auto tt = obn::os::timegm_safe(&t);
if (tt == static_cast<std::time_t>(-1)) return 0;
return static_cast<std::int64_t>(tt);
}
// Parse a server JSON body. Empty / unparseable bodies become an empty
// object, so callers can still call `.find(...)` without null checks.
obn::json::Value parse_body(const std::string& body)
{
std::string err;
auto v = obn::json::parse(body, &err);
if (!v) return obn::json::Value{obn::json::Object{}};
return *v;
}
// Copy a server-side error code (if any) into values_map["code"] so
// Studio's `values_map["code"] == "14"` preset-limit check keeps
// working even on our workaround path.
void propagate_error_code(const obn::json::Value& root,
std::map<std::string, std::string>& values_map)
{
auto c = root.find("code");
if (c.is_number()) {
long long n = c.as_int();
if (n != 0) values_map["code"] = std::to_string(n);
} else if (c.is_string() && !c.as_string().empty()) {
values_map["code"] = c.as_string();
}
}
// Build the top-level POST/PATCH body. Fields that belong in the
// envelope ("name", "type", "version", "base_id", "filament_id") are
// pulled from values_map; everything else goes into the `setting`
// sub-object. Matches the MITM-observed shape of the stock plugin.
std::string build_upload_body(const std::string& name,
const std::map<std::string, std::string>& values_map)
{
// Keys lifted to the envelope rather than dumped into `setting`.
// Studio's values_map also contains them, but the server stores
// them as first-class metadata; leaving them inside `setting`
// works too but deviates from Studio's shape.
static const char* const top_keys[] = {
IOT_JSON_KEY_VERSION, IOT_JSON_KEY_TYPE,
IOT_JSON_KEY_BASE_ID, IOT_JSON_KEY_FILAMENT_ID,
};
auto is_top = [&](const std::string& k) {
for (auto* tk : top_keys) if (k == tk) return true;
return false;
};
auto get_or = [&](const char* k) -> std::string {
auto it = values_map.find(k);
return it == values_map.end() ? std::string{} : it->second;
};
std::ostringstream os;
os << '{';
os << "\"name\":" << obn::json::escape(name);
os << ",\"version\":" << obn::json::escape(get_or(IOT_JSON_KEY_VERSION));
os << ",\"type\":" << obn::json::escape(get_or(IOT_JSON_KEY_TYPE));
os << ",\"base_id\":" << obn::json::escape(get_or(IOT_JSON_KEY_BASE_ID));
// filament_id only exists for filament presets; send empty string
// otherwise so the server's validator doesn't choke on missing
// fields.
os << ",\"filament_id\":" << obn::json::escape(get_or(IOT_JSON_KEY_FILAMENT_ID));
os << ",\"setting\":{";
bool first = true;
for (const auto& [k, v] : values_map) {
if (is_top(k)) continue;
// "code" is an error-reporting slot we write back in-place; it
// must not be uploaded as part of the preset.
if (k == "code") continue;
if (!first) os << ',';
first = false;
os << obn::json::escape(k) << ':' << obn::json::escape(v);
}
os << "}}";
return os.str();
}
// Turn the server response for GET /setting/<id> into the flat
// values_map Studio's loader expects. `user_id` is injected from the
// authenticated session (the server doesn't include it in the
// response).
void fill_values_from_full(const obn::json::Value& root,
const std::string& user_id,
std::map<std::string, std::string>& out)
{
out.clear();
// Metadata from the envelope first, so if a same-named key is
// also inside `setting` the envelope wins.
auto copy_str = [&](const char* key) {
auto v = root.find(key);
if (v.is_string()) out[key] = v.as_string();
else if (v.is_number()) out[key] = std::to_string(v.as_int());
};
copy_str(IOT_JSON_KEY_NAME);
copy_str(IOT_JSON_KEY_TYPE);
copy_str(IOT_JSON_KEY_VERSION);
copy_str(IOT_JSON_KEY_SETTING_ID);
copy_str(IOT_JSON_KEY_BASE_ID);
copy_str(IOT_JSON_KEY_FILAMENT_ID);
// `setting` is a flat {key: string-ish value} map.
const auto& setting = root.find("setting");
if (setting.is_object()) {
for (const auto& [k, v] : setting.as_object()) {
out[k] = json_scalar_to_string(v);
}
}
// Convert "YYYY-MM-DD HH:MM:SS" into the unix-seconds string
// Studio's load_user_preset() wants (std::atoll on the value).
std::string iso = root.find(IOT_JSON_KEY_UPDATE_TIME).as_string();
if (iso.empty()) iso = root.find(IOT_JSON_KEY_UPDATED_TIME).as_string();
std::int64_t unix_ts = parse_iso_utc(iso);
if (unix_ts == 0) {
// If the server ever returns a pre-epoch string or we fail to
// parse, avoid emitting "0" (it triggers Studio's "always
// overwrite" branch). Use 1 as a harmless sentinel.
unix_ts = 1;
}
out[IOT_JSON_KEY_UPDATED_TIME] = std::to_string(unix_ts);
// Studio's loader rejects presets without user_id. The server
// doesn't echo it, so inject our authenticated user_id here.
if (!user_id.empty()) {
out[IOT_JSON_KEY_USER_ID] = user_id;
}
}
} // namespace
int list(Agent* a, const std::string& bundle_version, std::vector<Meta>* out)
{
if (!a || !out) return BAMBU_NETWORK_ERR_INVALID_HANDLE;
out->clear();
auto hdrs = a->cloud_api_http_headers();
if (hdrs.find("Authorization") == hdrs.end()) {
return BAMBU_NETWORK_ERR_GET_SETTING_LIST_FAILED;
}
// GET doesn't need Content-Type; some backends 415 if present.
hdrs.erase("Content-Type");
std::string url = settings_base(a) + "?public=false";
if (!bundle_version.empty()) {
url += "&version=" + obn::http::url_encode(bundle_version);
}
auto resp = obn::http::get_json(url, hdrs);
OBN_INFO("cloud_presets::list http=%ld bytes=%zu",
resp.status_code, resp.body.size());
if (!resp.error.empty() || resp.status_code < 200 || resp.status_code >= 300) {
OBN_WARN("cloud_presets::list failed: %s", resp.error.c_str());
return BAMBU_NETWORK_ERR_GET_SETTING_LIST_FAILED;
}
obn::json::Value root = parse_body(resp.body);
// Response:
// { "message":"success", ..., "print":{"private":[...],"public":[...]},
// "printer":{...}, "filament":{...} }
static const char* const types[] = { IOT_PRINT_TYPE_STRING,
IOT_PRINTER_TYPE_STRING,
IOT_FILAMENT_STRING };
int total = 0;
for (const char* type : types) {
auto priv = root.find(std::string{type} + ".private");
if (!priv.is_array()) continue;
for (const auto& item : priv.as_array()) {
Meta m;
m.type = type;
m.setting_id = item.find(IOT_JSON_KEY_SETTING_ID).as_string();
m.name = item.find(IOT_JSON_KEY_NAME).as_string();
m.version = item.find(IOT_JSON_KEY_VERSION).as_string();
m.base_id = item.find(IOT_JSON_KEY_BASE_ID).as_string();
m.filament_id = item.find(IOT_JSON_KEY_FILAMENT_ID).as_string();
m.inherits = item.find("inherits").as_string();
m.update_time = item.find(IOT_JSON_KEY_UPDATE_TIME).as_string();
m.updated_time_unix = parse_iso_utc(m.update_time);
m.is_public = false;
if (m.setting_id.empty() || m.name.empty()) continue;
out->push_back(std::move(m));
++total;
}
}
OBN_INFO("cloud_presets::list -> %d user presets", total);
return BAMBU_NETWORK_SUCCESS;
}
int get_full(Agent* a,
const std::string& setting_id,
std::map<std::string, std::string>* values_map)
{
if (!a || !values_map || setting_id.empty())
return BAMBU_NETWORK_ERR_INVALID_HANDLE;
auto hdrs = a->cloud_api_http_headers();
if (hdrs.find("Authorization") == hdrs.end())
return BAMBU_NETWORK_ERR_GET_SETTING_LIST_FAILED;
hdrs.erase("Content-Type");
const std::string url = settings_base(a) + "/"
+ obn::http::url_encode(setting_id);
auto resp = obn::http::get_json(url, hdrs);
OBN_DEBUG("cloud_presets::get_full id=%s http=%ld bytes=%zu",
setting_id.c_str(), resp.status_code, resp.body.size());
if (resp.status_code == 400) {
// Server shape: {"message":"missing","code":2,"error":"..."}
OBN_INFO("cloud_presets::get_full %s: not found on server",
setting_id.c_str());
return BAMBU_NETWORK_ERR_GET_SETTING_LIST_FAILED;
}
if (!resp.error.empty() || resp.status_code < 200 || resp.status_code >= 300) {
OBN_WARN("cloud_presets::get_full %s failed: http=%ld err=%s",
setting_id.c_str(), resp.status_code, resp.error.c_str());
return BAMBU_NETWORK_ERR_GET_SETTING_LIST_FAILED;
}
obn::json::Value root = parse_body(resp.body);
fill_values_from_full(root, a->cloud_user_id(), *values_map);
return BAMBU_NETWORK_SUCCESS;
}
std::string create(Agent* a,
const std::string& name,
std::map<std::string, std::string>& values_map,
unsigned int* http_code)
{
if (http_code) *http_code = 0;
if (!a) return {};
auto hdrs = a->cloud_api_http_headers();
if (hdrs.find("Authorization") == hdrs.end()) {
OBN_WARN("cloud_presets::create: not logged in");
return {};
}
std::string body = build_upload_body(name, values_map);
auto resp = obn::http::post_json(settings_base(a), body, hdrs);
if (http_code) *http_code = static_cast<unsigned int>(resp.status_code);
OBN_INFO("cloud_presets::create name='%s' http=%ld bytes=%zu",
name.c_str(), resp.status_code, resp.body.size());
obn::json::Value root = parse_body(resp.body);
propagate_error_code(root, values_map);
if (!resp.error.empty() || resp.status_code < 200 || resp.status_code >= 300) {
return {};
}
std::string new_id = root.find(IOT_JSON_KEY_SETTING_ID).as_string();
std::string iso = root.find(IOT_JSON_KEY_UPDATE_TIME).as_string();
if (iso.empty()) iso = root.find(IOT_JSON_KEY_UPDATED_TIME).as_string();
std::int64_t ts = parse_iso_utc(iso);
if (ts > 0) values_map[IOT_JSON_KEY_UPDATED_TIME] = std::to_string(ts);
return new_id;
}
int update(Agent* a,
const std::string& setting_id,
const std::string& name,
std::map<std::string, std::string>& values_map,
unsigned int* http_code)
{
if (http_code) *http_code = 0;
if (!a || setting_id.empty()) return BAMBU_NETWORK_ERR_PUT_SETTING_FAILED;
auto hdrs = a->cloud_api_http_headers();
if (hdrs.find("Authorization") == hdrs.end()) {
OBN_WARN("cloud_presets::update: not logged in");
return BAMBU_NETWORK_ERR_PUT_SETTING_FAILED;
}
std::string body = build_upload_body(name, values_map);
obn::http::Request req;
req.method = obn::http::Method::PATCH;
req.url = settings_base(a) + "/" + obn::http::url_encode(setting_id);
req.body = body;
req.headers = hdrs;
auto resp = obn::http::perform(req);
if (http_code) *http_code = static_cast<unsigned int>(resp.status_code);
OBN_INFO("cloud_presets::update id=%s http=%ld bytes=%zu",
setting_id.c_str(), resp.status_code, resp.body.size());
obn::json::Value root = parse_body(resp.body);
propagate_error_code(root, values_map);
if (!resp.error.empty() || resp.status_code < 200 || resp.status_code >= 300) {
return BAMBU_NETWORK_ERR_PUT_SETTING_FAILED;
}
std::string iso = root.find(IOT_JSON_KEY_UPDATE_TIME).as_string();
if (iso.empty()) iso = root.find(IOT_JSON_KEY_UPDATED_TIME).as_string();
std::int64_t ts = parse_iso_utc(iso);
if (ts > 0) values_map[IOT_JSON_KEY_UPDATED_TIME] = std::to_string(ts);
return BAMBU_NETWORK_SUCCESS;
}
int del(Agent* a, const std::string& setting_id)
{
if (!a || setting_id.empty()) return BAMBU_NETWORK_ERR_DEL_SETTING_FAILED;
auto hdrs = a->cloud_api_http_headers();
if (hdrs.find("Authorization") == hdrs.end()) {
OBN_WARN("cloud_presets::del: not logged in");
return BAMBU_NETWORK_ERR_DEL_SETTING_FAILED;
}
hdrs.erase("Content-Type");
obn::http::Request req;
req.method = obn::http::Method::DEL;
req.url = settings_base(a) + "/" + obn::http::url_encode(setting_id);
req.headers = hdrs;
auto resp = obn::http::perform(req);
OBN_INFO("cloud_presets::del id=%s http=%ld", setting_id.c_str(), resp.status_code);
if (!resp.error.empty() || resp.status_code < 200 || resp.status_code >= 300) {
return BAMBU_NETWORK_ERR_DEL_SETTING_FAILED;
}
// Server is idempotent: DELETE on a missing id also returns 200.
return BAMBU_NETWORK_SUCCESS;
}
} // namespace obn::cloud_presets