// Import/export bundles: main's util/profile_bundle.py. A profile bundle carries the profile, its // palette colours, zones, the presets they use and their sequences; preset and sequence bundles carry // one record (a sequence with its presets). A profile bundle also carries the one-shot sequences its // sequences play as clips (step animation_id); older bundles' zone animation_ids fold into sequence_ids. Imports get new ids and belong to the importing profile. #include #include #include #include "api_util.h" #include "engine.h" #include "store.h" #define BUNDLE_VERSION 1 static void add_ref(cJSON *set, const cJSON *v) { if (cJSON_IsArray(v)) { const cJSON *it; cJSON_ArrayForEach(it, v) { add_ref(set, it); } return; } char id[24]; if (api_id(v, id, sizeof(id)) && !cJSON_GetObjectItem(set, id)) { cJSON_AddTrueToObject(set, id); } } static void zone_preset_ids(const cJSON *zone, cJSON *set) { add_ref(set, cJSON_GetObjectItem(zone, "presets")); add_ref(set, cJSON_GetObjectItem(zone, "presets_flat")); add_ref(set, cJSON_GetObjectItem(zone, "default_preset")); } static void sequence_preset_ids(const cJSON *seq, cJSON *set) { const cJSON *lane, *step; cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes")) { cJSON_ArrayForEach(step, lane) { add_ref(set, cJSON_GetObjectItem(step, "preset_id")); } } cJSON_ArrayForEach(step, cJSON_GetObjectItem(seq, "steps")) { add_ref(set, cJSON_GetObjectItem(step, "preset_id")); } } // Animations (sequences played once) that steps of seq fire static void sequence_animation_ids(const cJSON *seq, cJSON *set) { const cJSON *lane, *step; cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes")) { cJSON_ArrayForEach(step, lane) { add_ref(set, cJSON_GetObjectItem(step, "animation_id")); } } } // Point a list field (or each step's animation_id) at the new sequence ids static void remap_ids(cJSON *rec, const char *key, const cJSON *smap) { cJSON *list = cJSON_GetObjectItem(rec, key); if (!cJSON_IsArray(list)) { return; } cJSON *mapped = cJSON_CreateArray(), *it; cJSON_ArrayForEach(it, list) { char k[24]; if (api_id(it, k, sizeof(k))) { const cJSON *ns = cJSON_GetObjectItem(smap, k); cJSON_AddItemToArray(mapped, cJSON_CreateString(ns ? ns->valuestring : k)); } } api_set(rec, key, mapped); } static void remap_animation_steps(cJSON *seq, const cJSON *smap) { cJSON *lane, *step; cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes")) { cJSON_ArrayForEach(step, lane) { char k[24]; const cJSON *ns = api_id(cJSON_GetObjectItem(step, "animation_id"), k, sizeof(k)) ? cJSON_GetObjectItem(smap, k) : NULL; if (ns) { api_set(step, "animation_id", cJSON_CreateString(ns->valuestring)); } } } } static bool owned_by(const cJSON *rec, const char *profile) { char pid[24]; return api_id(cJSON_GetObjectItem(rec, "profile_id"), pid, sizeof(pid)) && !strcmp(pid, profile); } static cJSON *bundle(const char *kind) { cJSON *b = cJSON_CreateObject(); cJSON_AddNumberToObject(b, "version", BUNDLE_VERSION); cJSON_AddStringToObject(b, "kind", kind); return b; } static cJSON *palette_colors(const cJSON *profile) { char id[24]; const cJSON *pal = api_id(cJSON_GetObjectItem(profile, "palette_id"), id, sizeof(id)) ? cJSON_GetObjectItem(store_coll("palette"), id) : NULL; if (cJSON_IsObject(pal)) { pal = cJSON_GetObjectItem(pal, "colors"); } return cJSON_IsArray(pal) ? cJSON_Duplicate(pal, true) : cJSON_CreateArray(); } static cJSON *export_profile(const char *id) { const cJSON *src = cJSON_GetObjectItem(store_coll("profile"), id); if (!src) { return NULL; } cJSON *b = bundle("profile"); cJSON *prof = cJSON_Duplicate(src, true); cJSON_DeleteItemFromObject(prof, "palette"); cJSON_AddItemToObject(b, "profile", prof); cJSON *pal = cJSON_AddObjectToObject(b, "palette"); cJSON_AddItemToObject(pal, "colors", palette_colors(src)); cJSON *zones = cJSON_AddObjectToObject(b, "zones"); cJSON *presets = cJSON_AddObjectToObject(b, "presets"); cJSON *seqs = cJSON_AddObjectToObject(b, "sequences"); cJSON *pset = cJSON_CreateObject(), *sset = cJSON_CreateObject(); const cJSON *zl = cJSON_GetObjectItem(src, "zones"); if (!cJSON_GetArraySize(zl)) { zl = cJSON_GetObjectItem(src, "zone_order"); } const cJSON *zid; cJSON_ArrayForEach(zid, zl) { char key[24]; const cJSON *z = api_id(zid, key, sizeof(key)) ? cJSON_GetObjectItem(store_coll("zone"), key) : NULL; if (!z || cJSON_GetObjectItem(zones, key)) { continue; } cJSON_AddItemToObject(zones, key, cJSON_Duplicate(z, true)); zone_preset_ids(z, pset); add_ref(sset, cJSON_GetObjectItem(z, "sequence_ids")); add_ref(sset, cJSON_GetObjectItem(z, "animation_ids")); } const cJSON *it; cJSON_ArrayForEach(it, sset) { const cJSON *s = cJSON_GetObjectItem(store_coll("sequence"), it->string); if (s && owned_by(s, id)) { cJSON_AddItemToObject(seqs, it->string, cJSON_Duplicate(s, true)); sequence_preset_ids(s, pset); sequence_animation_ids(s, sset); // appended, so this loop reaches them too } } cJSON_ArrayForEach(it, pset) { const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), it->string); if (p && owned_by(p, id)) { cJSON_AddItemToObject(presets, it->string, cJSON_Duplicate(p, true)); } } cJSON_Delete(pset); cJSON_Delete(sset); return b; } // Copy of v with every preset id mapped (nested lists allowed); unknown ids are copied from the store // into the new profile, or become null when there is no such preset static cJSON *map_presets(const cJSON *v, cJSON *map, const char *profile) { if (cJSON_IsArray(v)) { cJSON *out = cJSON_CreateArray(); const cJSON *it; cJSON_ArrayForEach(it, v) { cJSON_AddItemToArray(out, map_presets(it, map, profile)); } return out; } char id[24]; if (!api_id(v, id, sizeof(id))) { return cJSON_CreateNull(); } const cJSON *to = cJSON_GetObjectItem(map, id); if (to) { return cJSON_Duplicate(to, false); } const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), id); if (!p) { return cJSON_CreateNull(); } cJSON *copy = cJSON_Duplicate(p, true); api_set(copy, "profile_id", cJSON_CreateString(profile)); char nid[16]; api_add("preset", copy, nid, sizeof(nid)); cJSON_AddStringToObject(map, id, nid); return cJSON_CreateString(nid); } static void map_steps(cJSON *steps, const cJSON *map) { cJSON *step; cJSON_ArrayForEach(step, steps) { char id[24]; const cJSON *to = api_id(cJSON_GetObjectItem(step, "preset_id"), id, sizeof(id)) ? cJSON_GetObjectItem(map, id) : NULL; if (to) { api_set(step, "preset_id", cJSON_Duplicate(to, false)); } } } // Sequence copy with step preset ids mapped where the map knows them static cJSON *map_sequence(const cJSON *seq, const cJSON *map) { cJSON *out = cJSON_Duplicate(seq, true), *lane; cJSON_ArrayForEach(lane, cJSON_GetObjectItem(out, "lanes")) { map_steps(lane, map); } map_steps(cJSON_GetObjectItem(out, "steps"), map); return out; } // Presets from a bundle into profile; returns old id -> new id static cJSON *import_presets(const cJSON *presets, const char *profile) { cJSON *map = cJSON_CreateObject(); const cJSON *p; cJSON_ArrayForEach(p, presets) { if (!cJSON_IsObject(p)) { continue; } cJSON *copy = cJSON_Duplicate(p, true); api_set(copy, "profile_id", cJSON_CreateString(profile)); char nid[16]; api_add("preset", copy, nid, sizeof(nid)); cJSON_AddStringToObject(map, p->string, nid); } return map; } static const char *import_profile(const cJSON *b, const char *name, char *pid, size_t len, cJSON **out) { const cJSON *ver = cJSON_GetObjectItem(b, "version"), *kind = cJSON_GetObjectItem(b, "kind"); if (!((cJSON_IsNumber(ver) && ver->valuedouble == BUNDLE_VERSION) || (cJSON_IsString(ver) && !strcmp(ver->valuestring, "1")))) { return "Unsupported bundle version"; } if (kind && !cJSON_IsNull(kind) && !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "profile"))) { return "Not a profile bundle"; } const cJSON *src = cJSON_GetObjectItem(b, "profile"); if (!cJSON_IsObject(src)) { return "Bundle missing profile"; } const cJSON *sname = cJSON_GetObjectItem(src, "name"); const char *new_name = name && name[0] ? name : cJSON_IsString(sname) && sname->valuestring[0] ? sname->valuestring : "Imported profile"; const cJSON *type = cJSON_GetObjectItem(src, "type"); cJSON *prof = cJSON_CreateObject(); cJSON_AddStringToObject(prof, "name", new_name); cJSON_AddStringToObject(prof, "type", cJSON_IsString(type) ? type->valuestring : "zones"); api_add("profile", prof, pid, len); char palid[16]; const cJSON *pal = cJSON_GetObjectItem(cJSON_GetObjectItem(b, "palette"), "colors"); api_add("palette", cJSON_IsArray(pal) ? cJSON_Duplicate(pal, true) : cJSON_CreateArray(), palid, sizeof(palid)); cJSON *pmap = import_presets(cJSON_GetObjectItem(b, "presets"), pid); cJSON *smap = cJSON_CreateObject(); const cJSON *s; cJSON_ArrayForEach(s, cJSON_GetObjectItem(b, "sequences")) { if (!cJSON_IsObject(s)) { continue; } cJSON *copy = map_sequence(s, pmap); api_set(copy, "profile_id", cJSON_CreateString(pid)); char nid[16]; api_add("sequence", copy, nid, sizeof(nid)); cJSON_AddStringToObject(smap, s->string, nid); } cJSON *imported; cJSON_ArrayForEach(imported, smap) { remap_animation_steps(cJSON_GetObjectItem(store_coll("sequence"), imported->valuestring), smap); } const cJSON *zones_in = cJSON_GetObjectItem(b, "zones"); const cJSON *order = cJSON_GetObjectItem(src, "zones"); cJSON *keys = NULL; if (!cJSON_IsArray(order)) { keys = cJSON_CreateArray(); const cJSON *z; cJSON_ArrayForEach(z, zones_in) { cJSON_AddItemToArray(keys, cJSON_CreateString(z->string)); } order = keys; } cJSON *zone_ids = cJSON_CreateArray(); const cJSON *oz; cJSON_ArrayForEach(oz, order) { char key[24]; const cJSON *z = api_id(oz, key, sizeof(key)) ? cJSON_GetObjectItem(zones_in, key) : NULL; if (!cJSON_IsObject(z)) { continue; } cJSON *copy = cJSON_Duplicate(z, true); const cJSON *zn = cJSON_GetObjectItem(z, "name"); if (!cJSON_IsString(zn) || !zn->valuestring[0]) { char nm[40]; snprintf(nm, sizeof(nm), "Zone %s", key); api_set(copy, "name", cJSON_CreateString(nm)); } if (!cJSON_IsArray(cJSON_GetObjectItem(copy, "names"))) { api_set(copy, "names", cJSON_CreateArray()); } static const char *const REFS[] = { "presets", "presets_flat" }; for (int i = 0; i < 2; i++) { const cJSON *v = cJSON_GetObjectItem(z, REFS[i]); if (v && !cJSON_IsNull(v)) { api_set(copy, REFS[i], map_presets(v, pmap, pid)); } } char dp[24]; const cJSON *to = api_id(cJSON_GetObjectItem(z, "default_preset"), dp, sizeof(dp)) ? cJSON_GetObjectItem(pmap, dp) : NULL; if (to) { api_set(copy, "default_preset", cJSON_Duplicate(to, false)); } remap_ids(copy, "sequence_ids", smap); remap_ids(copy, "animation_ids", smap); api_zone_fold_oneshots(copy); char nid[16]; api_add("zone", copy, nid, sizeof(nid)); cJSON_AddItemToArray(zone_ids, cJSON_CreateString(nid)); } cJSON_Delete(keys); cJSON_Delete(pmap); cJSON_Delete(smap); cJSON_AddItemToObject(prof, "zones", zone_ids); const cJSON *scenes = cJSON_GetObjectItem(src, "scenes"); cJSON_AddItemToObject(prof, "scenes", cJSON_IsArray(scenes) ? cJSON_Duplicate(scenes, true) : cJSON_CreateArray()); cJSON_AddStringToObject(prof, "palette_id", palid); store_save("palette"); store_save("preset"); store_save("zone"); store_save("sequence"); store_save("profile"); engine_groups_changed(); *out = prof; return NULL; } // The bundle from a body: {"bundle": {...}} or the bundle itself static const cJSON *bundle_of(const cJSON *body) { const cJSON *b = cJSON_GetObjectItem(body, "bundle"); return cJSON_IsObject(b) ? b : body; } static bool truthy(const cJSON *v, bool dflt) { if (!v || cJSON_IsNull(v)) { return dflt; } if (cJSON_IsString(v)) { const char *s = v->valuestring; return !strcasecmp(s, "1") || !strcasecmp(s, "true") || !strcasecmp(s, "yes") || !strcasecmp(s, "on"); } return cJSON_IsTrue(v) || (cJSON_IsNumber(v) && v->valuedouble != 0); } static esp_err_t profiles_import(httpd_req_t *req) { cJSON *body = api_body(req); if (!body) { return ESP_OK; } const cJSON *nm = cJSON_GetObjectItem(body, "name"); char name[64] = ""; if (cJSON_IsString(nm)) { snprintf(name, sizeof(name), "%s", nm->valuestring); } bool apply = truthy(cJSON_GetObjectItem(body, "apply"), true); char pid[16]; cJSON *prof = NULL; const char *err = import_profile(bundle_of(body), name, pid, sizeof(pid), &prof); cJSON_Delete(body); if (err) { return api_error(req, "400 Bad Request", err); } if (apply) { api_set(store_settings(), "current_profile_id", cJSON_CreateString(pid)); store_save("settings"); } cJSON *o = cJSON_CreateObject(); cJSON_AddItemReferenceToObject(o, pid, prof); cJSON_AddStringToObject(o, "id", pid); return api_json(req, "201 Created", o, true); } static esp_err_t presets_import(httpd_req_t *req, const char *profile) { cJSON *body = api_body(req); if (!body) { return ESP_OK; } const cJSON *b = bundle_of(body), *kind = cJSON_GetObjectItem(b, "kind"), *preset = cJSON_GetObjectItem(b, "preset"); const char *err = !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "preset")) ? "Not a preset bundle" : !cJSON_IsObject(preset) ? "Bundle missing preset" : NULL; if (err) { cJSON_Delete(body); return api_error(req, "400 Bad Request", err); } cJSON *rec = api_preset_defaults(profile); api_merge(rec, preset); cJSON_Delete(body); api_set(rec, "profile_id", cJSON_CreateString(profile)); char id[16]; api_add("preset", rec, id, sizeof(id)); store_save("preset"); cJSON *o = cJSON_CreateObject(); cJSON_AddItemReferenceToObject(o, id, rec); return api_json(req, "201 Created", o, true); } static esp_err_t sequences_import(httpd_req_t *req, const char *profile) { cJSON *body = api_body(req); if (!body) { return ESP_OK; } const cJSON *b = bundle_of(body), *kind = cJSON_GetObjectItem(b, "kind"), *seq = cJSON_GetObjectItem(b, "sequence"); const char *err = !(cJSON_IsString(kind) && !strcmp(kind->valuestring, "sequence")) ? "Not a sequence bundle" : !cJSON_IsObject(seq) ? "Bundle missing sequence" : NULL; if (err) { cJSON_Delete(body); return api_error(req, "400 Bad Request", err); } cJSON *map = import_presets(cJSON_GetObjectItem(b, "presets"), profile); // Steps naming presets that were not in the bundle: copy those from this controller cJSON *used = cJSON_CreateObject(), *it; sequence_preset_ids(seq, used); cJSON_ArrayForEach(it, used) { if (!cJSON_GetObjectItem(map, it->string)) { cJSON *ref = cJSON_CreateString(it->string); cJSON_Delete(map_presets(ref, map, profile)); // copies the preset and records the mapping cJSON_Delete(ref); } } cJSON_Delete(used); cJSON *rec = api_sequence_defaults(); cJSON *mapped = map_sequence(seq, map); api_merge(rec, mapped); cJSON_Delete(mapped); cJSON_Delete(map); cJSON_Delete(body); api_set(rec, "profile_id", cJSON_CreateString(profile)); char id[16]; api_add("sequence", rec, id, sizeof(id)); store_save("preset"); store_save("sequence"); cJSON *o = cJSON_CreateObject(); cJSON_AddItemReferenceToObject(o, id, rec); return api_json(req, "201 Created", o, true); } bool bundles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { const bool profiles = !strcmp(seg[0], "profiles"), presets = !strcmp(seg[0], "presets"), sequences = !strcmp(seg[0], "sequences"); if (!profiles && !presets && !sequences) { return false; } const bool is_import = n == 2 && req->method == HTTP_POST && !strcmp(seg[1], "import"); const bool is_export = n == 3 && req->method == HTTP_GET && !strcmp(seg[2], "export"); if (!is_import && !is_export) { return false; } const char *profile = api_current_profile(); if (is_import) { *ret = profiles ? profiles_import(req) : !profile ? api_error(req, "404 Not Found", "No profile available") : presets ? presets_import(req, profile) : sequences_import(req, profile); return true; } cJSON *b = NULL; if (profiles) { b = export_profile(seg[1]); } else { const cJSON *rec = cJSON_GetObjectItem(store_coll(presets ? "preset" : "sequence"), seg[1]); if (rec && profile && owned_by(rec, profile)) { b = bundle(presets ? "preset" : "sequence"); cJSON_AddItemToObject(b, presets ? "preset" : "sequence", cJSON_Duplicate(rec, true)); if (sequences) { cJSON *ps = cJSON_AddObjectToObject(b, "presets"), *used = cJSON_CreateObject(), *it; sequence_preset_ids(rec, used); cJSON_ArrayForEach(it, used) { const cJSON *p = cJSON_GetObjectItem(store_coll("preset"), it->string); if (p && owned_by(p, profile)) { cJSON_AddItemToObject(ps, it->string, cJSON_Duplicate(p, true)); } } cJSON_Delete(used); } } } *ret = b ? api_json(req, "200 OK", b, true) : api_error(req, "404 Not Found", profiles ? "Profile not found" : presets ? "Preset not found" : "Sequence not found"); return true; }