// Palettes and sequences: the v1 app's controllers/palette.py and sequence.py #include #include #include #include "api_util.h" #include "engine.h" #include "seqplay.h" #include "store.h" /* --------------------------------------------------------------- palettes */ // A palette is stored as a bare list of colours; older records hold {"colors": [...]} static const cJSON *palette_colors(const cJSON *rec) { if (cJSON_IsObject(rec)) { rec = cJSON_GetObjectItem(rec, "colors"); } return cJSON_IsArray(rec) ? rec : NULL; } static cJSON *palette_reply(const char *id, const cJSON *rec) { const cJSON *colors = palette_colors(rec); cJSON *o = cJSON_CreateObject(); cJSON_AddItemToObject(o, "colors", colors ? cJSON_Duplicate(colors, true) : cJSON_CreateArray()); cJSON_AddStringToObject(o, "id", id); return o; } // Colours from {"colors": [...]}; anything else is an empty list static cJSON *colors_from(const cJSON *body) { const cJSON *c = cJSON_GetObjectItem(body, "colors"); return cJSON_IsArray(c) ? cJSON_Duplicate(c, true) : cJSON_CreateArray(); } bool palettes_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { cJSON *all = store_coll("palette"); const int method = req->method; if (n == 1 && method == HTTP_GET) { cJSON *o = cJSON_CreateObject(), *it; cJSON_ArrayForEach(it, all) { const cJSON *colors = palette_colors(it); cJSON_AddItemToObject(o, it->string, colors ? cJSON_Duplicate(colors, true) : cJSON_CreateArray()); } *ret = api_json(req, "200 OK", o, true); return true; } if (n == 1 && method == HTTP_POST) { cJSON *body = api_body_opt(req); if (!body) { *ret = ESP_OK; return true; } cJSON *colors = colors_from(body); cJSON_Delete(body); char id[16]; api_add("palette", colors, id, sizeof(id)); store_save("palette"); *ret = api_json(req, "201 Created", palette_reply(id, colors), true); return true; } if (n != 2 || (method != HTTP_GET && method != HTTP_PUT)) { return false; } cJSON *rec = cJSON_GetObjectItem(all, seg[1]); if (!rec) { *ret = api_error(req, "404 Not Found", "Palette not found"); return true; } if (method == HTTP_PUT) { cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } rec = colors_from(body); cJSON_Delete(body); cJSON_ReplaceItemInObject(all, seg[1], rec); store_save("palette"); } *ret = api_json(req, "200 OK", palette_reply(seg[1], rec), true); return true; } /* -------------------------------------------------------------- sequences */ // main's models/sequence.py: lanes of {preset_id, beats} steps, each lane with its own groups static const char SEQ_DEFAULTS[] = "{\"name\":\"\",\"group_ids\":[],\"lanes\":[[]],\"lanes_group_ids\":[[]]," "\"advance_mode\":\"beats\",\"steps\":[],\"step_duration_ms\":3000," "\"sequence_transition\":500,\"loop\":true}"; cJSON *api_sequence_defaults(void) { return cJSON_Parse(SEQ_DEFAULTS); } static bool has_steps(const cJSON *lanes) { const cJSON *l; cJSON_ArrayForEach(l, lanes) { if (cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0) { return true; } } return false; } // Fill what older records lack (main's Sequence._migrate_after_load). True when doc changed. static bool migrate_seq(cJSON *doc, const char *default_profile) { bool changed = false; if (!cJSON_IsArray(cJSON_GetObjectItem(doc, "steps"))) { cJSON *steps = cJSON_CreateArray(), *p; cJSON_ArrayForEach(p, cJSON_GetObjectItem(doc, "presets")) { char id[24]; if (api_id(p, id, sizeof(id))) { cJSON *st = cJSON_CreateObject(); cJSON_AddStringToObject(st, "preset_id", id); cJSON_AddArrayToObject(st, "group_ids"); cJSON_AddItemToArray(steps, st); } } api_set(doc, "steps", steps); changed = true; } static const char *const DEFAULT_KEYS[] = { "step_duration_ms", "loop", "name", "advance_mode", "sequence_transition", "group_ids" }; cJSON *defs = cJSON_Parse(SEQ_DEFAULTS); for (size_t i = 0; i < sizeof(DEFAULT_KEYS) / sizeof(DEFAULT_KEYS[0]); i++) { const cJSON *v = cJSON_GetObjectItem(doc, DEFAULT_KEYS[i]); bool bad = !v || (!strcmp(DEFAULT_KEYS[i], "group_ids") && !cJSON_IsArray(v)) || (!strcmp(DEFAULT_KEYS[i], "advance_mode") && !(cJSON_IsString(v) && (!strcmp(v->valuestring, "beats") || !strcmp(v->valuestring, "time")))); if (bad) { api_set(doc, DEFAULT_KEYS[i], cJSON_Duplicate(cJSON_GetObjectItem(defs, DEFAULT_KEYS[i]), true)); changed = true; } } cJSON_Delete(defs); if (!cJSON_GetObjectItem(doc, "profile_id") && default_profile) { cJSON_AddStringToObject(doc, "profile_id", default_profile); changed = true; } cJSON *lanes = cJSON_GetObjectItem(doc, "lanes"); if (!cJSON_IsArray(lanes)) { lanes = cJSON_CreateArray(); const cJSON *steps = cJSON_GetObjectItem(doc, "steps"); cJSON_AddItemToArray(lanes, cJSON_GetArraySize(steps) ? cJSON_Duplicate(steps, true) : cJSON_CreateArray()); api_set(doc, "lanes", lanes); changed = true; } int n_lanes = 0; cJSON *lane, *step; cJSON_ArrayForEach(lane, lanes) { if (!cJSON_IsArray(lane)) { continue; } n_lanes++; cJSON_ArrayForEach(step, lane) { if (cJSON_IsObject(step) && !cJSON_GetObjectItem(step, "beats")) { cJSON_AddNumberToObject(step, "beats", 1); changed = true; } } } const cJSON *lg = cJSON_GetObjectItem(doc, "lanes_group_ids"); if (n_lanes && (!cJSON_IsArray(lg) || cJSON_GetArraySize(lg) != n_lanes)) { const cJSON *shared = cJSON_GetObjectItem(doc, "group_ids"); cJSON *out = cJSON_CreateArray(); const cJSON *first_step = cJSON_GetArrayItem(cJSON_GetArrayItem(lanes, 0), 0); const cJSON *step_g = cJSON_GetObjectItem(first_step, "group_ids"); for (int i = 0; i < n_lanes; i++) { bool own = n_lanes == 1 && cJSON_GetArraySize(step_g) > 0; const cJSON *src = own ? step_g : shared; cJSON_AddItemToArray(out, cJSON_IsArray(src) ? cJSON_Duplicate(src, true) : cJSON_CreateArray()); } api_set(doc, "lanes_group_ids", out); changed = true; } return changed; } void api_migrate_sequences(void) { const char *first = store_coll("profile")->child ? store_coll("profile")->child->string : NULL; bool changed = false; cJSON *it; cJSON_ArrayForEach(it, store_coll("sequence")) { if (cJSON_IsObject(it)) { changed |= migrate_seq(it, first); } } if (changed) { store_save("sequence"); } } static bool in_profile(const cJSON *seq) { const char *cur = api_current_profile(); char pid[24]; return cur && api_id(cJSON_GetObjectItem(seq, "profile_id"), pid, sizeof(pid)) && !strcmp(pid, cur); } // main's Sequence.update: a steps list with no usable lanes becomes lane 0 static void apply_update(cJSON *doc, cJSON *data) { const cJSON *steps = cJSON_GetObjectItem(data, "steps"); if (cJSON_GetArraySize(steps) && !has_steps(cJSON_GetObjectItem(data, "lanes"))) { cJSON *lanes = cJSON_CreateArray(); cJSON_AddItemToArray(lanes, cJSON_Duplicate(steps, true)); api_set(data, "lanes", lanes); } // `loop` decides whether a sequence is a one-shot. Older clients sent kind "animation" for one; // without a `loop` of their own that means loop off. kind itself is no longer kept. const cJSON *kind = cJSON_GetObjectItem(data, "kind"); if (cJSON_IsString(kind) && !strcmp(kind->valuestring, "animation") && !cJSON_GetObjectItem(data, "loop")) { api_set(data, "loop", cJSON_CreateFalse()); } cJSON_DeleteItemFromObject(data, "kind"); if (cJSON_GetObjectItem(data, "loop")) { cJSON_DeleteItemFromObject(doc, "kind"); } api_merge(doc, data); } static esp_err_t play_sequence(httpd_req_t *req, const char *id) { const char *profile = api_current_profile(); if (!profile) { return api_error(req, "404 Not Found", "No profile available"); } cJSON *body = api_body_opt(req); if (!body) { return ESP_OK; } char zone[24]; const cJSON *z = cJSON_GetObjectItem(body, "zone_id"); bool have = api_id(z ? z : cJSON_GetObjectItem(body, "zoneId"), zone, sizeof(zone)); cJSON_Delete(body); if (!have) { return api_error(req, "400 Bad Request", "zone_id required"); } const char *msg; // A one-shot (loop off) plays once over whatever runs, then the zone goes back to its last looping // preset or sequence esp_err_t err = seqplay_is_oneshot(id) ? seqplay_fire(id, zone, profile, "now", &msg) : seqplay_start(id, zone, profile, &msg); if (err != ESP_OK) { return api_error(req, "400 Bad Request", msg ? msg : "sequence not found"); } cJSON *o = seqplay_pending(); cJSON_AddTrueToObject(o, "ok"); return api_json(req, "200 OK", o, true); } // POST /sequences//fire {zone_id?, wait?: "now" | "beat" | "downbeat"}: play it once over whatever runs static esp_err_t fire_sequence(httpd_req_t *req, const char *id) { const char *profile = api_current_profile(); if (!profile) { return api_error(req, "404 Not Found", "No profile available"); } cJSON *body = api_body_opt(req); if (!body) { return ESP_OK; } char zone[24] = "", wait[12] = "now"; api_id(cJSON_GetObjectItem(body, "zone_id"), zone, sizeof(zone)); const cJSON *w = cJSON_GetObjectItem(body, "wait"); if (cJSON_IsString(w)) { snprintf(wait, sizeof(wait), "%s", w->valuestring); } cJSON_Delete(body); const char *msg; esp_err_t err = seqplay_fire(id, zone, profile, wait, &msg); if (err != ESP_OK) { return api_error(req, err == ESP_ERR_NOT_FOUND ? "404 Not Found" : "400 Bad Request", msg ? msg : "sequence not found"); } cJSON *o = cJSON_CreateObject(); cJSON_AddTrueToObject(o, "ok"); cJSON_AddStringToObject(o, "wait", wait); return api_json(req, "200 OK", o, true); } bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { cJSON *all = store_coll("sequence"); const int method = req->method; const char *profile = api_current_profile(); if (n == 2 && method == HTTP_GET && !strcmp(seg[1], "status")) { *ret = api_json(req, "200 OK", seqplay_status(), true); return true; } if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "stop")) { seqplay_stop(true); cJSON *o = cJSON_CreateObject(); cJSON_AddTrueToObject(o, "ok"); *ret = api_json(req, "200 OK", o, true); return true; } if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "sync-phase")) { cJSON *body = api_body_opt(req); if (!body) { *ret = ESP_OK; return true; } const cJSON *m = cJSON_GetObjectItem(body, "mode"); if (!cJSON_IsString(m)) { m = cJSON_GetObjectItem(body, "align"); } char mode[16]; snprintf(mode, sizeof(mode), "%s", cJSON_IsString(m) ? m->valuestring : "step"); for (char *c = mode; *c; c++) { *c = tolower((unsigned char)*c); } cJSON_Delete(body); if (!seqplay_sync(mode)) { *ret = api_error(req, "409 Conflict", "No sequence is playing"); return true; } cJSON *o = cJSON_CreateObject(); cJSON_AddTrueToObject(o, "ok"); cJSON_AddStringToObject(o, "mode", mode); *ret = api_json(req, "200 OK", o, true); return true; } if (n == 3 && method == HTTP_POST && !strcmp(seg[1], "overlay") && !strcmp(seg[2], "stop")) { seqplay_overlay_stop(); cJSON *o = cJSON_CreateObject(); cJSON_AddTrueToObject(o, "ok"); *ret = api_json(req, "200 OK", o, true); return true; } if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "fire")) { *ret = fire_sequence(req, seg[1]); return true; } if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "play")) { *ret = play_sequence(req, seg[1]); return true; } if (n == 1 && method == HTTP_GET) { cJSON *o = cJSON_CreateObject(), *it; cJSON_ArrayForEach(it, all) { if (cJSON_IsObject(it) && in_profile(it)) { cJSON_AddItemReferenceToObject(o, it->string, it); } } *ret = api_json(req, "200 OK", o, true); return true; } if (n == 1 && method == HTTP_POST) { if (!profile) { *ret = api_error(req, "404 Not Found", "No profile available"); return true; } cJSON *body = api_body_opt(req); if (!body) { *ret = ESP_OK; return true; } cJSON *doc = cJSON_Parse(SEQ_DEFAULTS); api_set(body, "profile_id", cJSON_CreateString(profile)); apply_update(doc, body); cJSON_Delete(body); char id[16]; api_add("sequence", doc, id, sizeof(id)); store_save("sequence"); cJSON *o = cJSON_CreateObject(); cJSON_AddItemReferenceToObject(o, id, doc); *ret = api_json(req, "201 Created", o, true); return true; } if (n != 2) { return false; } cJSON *rec = cJSON_GetObjectItem(all, seg[1]); if (!rec || !in_profile(rec)) { *ret = api_error(req, "404 Not Found", "Sequence not found"); return true; } switch (method) { case HTTP_GET: *ret = api_json(req, "200 OK", rec, false); return true; case HTTP_PUT: { cJSON *body = api_body(req); if (!body) { *ret = ESP_OK; return true; } api_set(body, "profile_id", cJSON_CreateString(profile)); apply_update(rec, body); cJSON_Delete(body); store_save("sequence"); seqplay_stop_if(seg[1]); *ret = api_json(req, "200 OK", rec, false); return true; } case HTTP_DELETE: seqplay_stop_if(seg[1]); cJSON_DeleteItemFromObject(all, seg[1]); store_save("sequence"); *ret = api_message(req, "200 OK", "Sequence deleted successfully"); return true; default: return false; } }