feat: main's import/export, zones, audio API and web UI

Profiles, presets and sequences export and import as main's bundles.
Zones carry group_ids, preset_group_ids, sequence_ids, content_kind and
brightness; profiles seed rainbow as colour_cycle, and deleting a
profile also deletes its sequences. Groups, zones and sequences written
by older versions are migrated at boot.

/api/audio (status, event stream, start/stop, reset, anchor-bar,
devices) maps main's audio detector onto the mic's beat detector. Audio
start/stop picks whether mic beats or the BPM clock time sequences; the
clock's beats also go to the drivers while the mic is off.
/ws now greets from a post-handshake callback (httpd does not call the
handler for the upgrade).

The web UI is main's, plus v2's outputs editor, list search and
hostname/static IP settings; the bridge, Wi-Fi AP and led-tool parts
are gone. API tests follow main's behaviour (119 pass on the board).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-03 20:34:22 +13:00
1 parent 04b56c6ef2
commit bfeeb958df
44 files changed
+8709 -2338

No files matched your search

+92 -15
View File
@@ -12,7 +12,7 @@
/* ---------------------------------------------------------------- presets */
// A new preset before the request's fields are applied
static cJSON *preset_defaults(const char *profile_id)
cJSON *api_preset_defaults(const char *profile_id)
{
cJSON *p = cJSON_CreateObject();
cJSON_AddStringToObject(p, "name", "");
@@ -120,7 +120,7 @@ bool presets_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
*ret = ESP_OK;
return true;
}
cJSON *rec = preset_defaults(profile);
cJSON *rec = api_preset_defaults(profile);
api_merge(rec, body);
cJSON_Delete(body);
api_set(rec, "profile_id", cJSON_CreateString(profile));
@@ -164,7 +164,7 @@ typedef struct {
static const seed_t SEED[] = {
{ "on", "on", "[\"#FFFFFF\"]", 255, 100, { 0 } },
{ "off", "off", "[]", 0, 100, { 0 } },
{ "rainbow", "rainbow", "[]", 255, 100, { 2 } },
{ "rainbow", "colour_cycle", "[]", 255, 100, { 2 } },
{ "Colour Cycle", "colour_cycle", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 100, { 1 } },
{ "transition", "transition", "[\"#FF0000\",\"#00FF00\",\"#0000FF\"]", 255, 500, { 0 } },
{ "flicker", "flicker", "[\"#FFB84D\"]", 255, 80, { 30 } },
@@ -172,7 +172,7 @@ static const seed_t SEED[] = {
{ "twinkle", "twinkle", "[\"#78C8FF\",\"#508CFF\",\"#B478FF\",\"#64DCE8\",\"#A0C8FF\"]", 255, 55, { 72, 140, 2, 6 } },
};
static const seed_t SEED_DJ[] = {
{ "DJ Rainbow", "rainbow", "[]", 220, 60, { 12 } },
{ "DJ Rainbow", "colour_cycle", "[]", 220, 60, { 12 } },
{ "DJ Single Color", "on", "[\"#ff00ff\"]", 220, 100, { 0 } },
{ "DJ Transition", "transition", "[\"#ff0000\",\"#00ff00\",\"#0000ff\"]", 220, 250, { 0 } },
};
@@ -183,7 +183,7 @@ static void seed_zone(const char *profile, const char *zone_name, const char *dr
{
cJSON *flat = cJSON_CreateArray();
for (int i = 0; i < count; i++) {
cJSON *p = preset_defaults(profile);
cJSON *p = api_preset_defaults(profile);
api_set(p, "name", cJSON_CreateString(seed[i].name));
api_set(p, "pattern", cJSON_CreateString(seed[i].pattern));
api_set(p, "colors", cJSON_Parse(seed[i].colors));
@@ -395,8 +395,8 @@ static bool used_elsewhere(const char *skip, const char *key, const char *id)
return false;
}
// DELETE /profiles/<id>: the profile with its presets, and its zones and palette unless another profile
// uses them. Ids are reused (next id = highest + 1), so leftovers would turn up in the next new profile.
// DELETE /profiles/<id>: the profile with its presets and sequences, and its zones and palette unless
// another profile uses them. Ids are reused (next id = highest + 1), so leftovers would turn up in the next new profile.
static esp_err_t delete_profile(httpd_req_t *req, const char *id)
{
char pid[24];
@@ -405,14 +405,20 @@ static esp_err_t delete_profile(httpd_req_t *req, const char *id)
if (!prof) {
return api_error(req, "404 Not Found", "Profile not found");
}
cJSON *presets = store_coll("preset"), *it = presets->child;
while (it) {
cJSON *next = it->next;
char owner[24];
if (api_id(cJSON_GetObjectItem(it, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, pid)) {
cJSON_Delete(cJSON_DetachItemViaPointer(presets, it));
static const char *const OWNED[] = { "preset", "sequence" };
for (size_t i = 0; i < sizeof(OWNED) / sizeof(OWNED[0]); i++) {
cJSON *coll = store_coll(OWNED[i]), *it = coll->child;
while (it) {
cJSON *next = it->next;
char owner[24];
if (api_id(cJSON_GetObjectItem(it, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, pid)) {
if (i == 1) {
seqplay_stop_if(it->string);
}
cJSON_Delete(cJSON_DetachItemViaPointer(coll, it));
}
it = next;
}
it = next;
}
const cJSON *z;
cJSON_ArrayForEach(z, profile_zones(prof))
@@ -433,6 +439,7 @@ static esp_err_t delete_profile(httpd_req_t *req, const char *id)
store_save("settings");
}
store_save("preset");
store_save("sequence");
store_save("zone");
store_save("palette");
store_save("profile");
@@ -553,7 +560,7 @@ static esp_err_t create_zone(httpd_req_t *req)
const cJSON *name = cJSON_GetObjectItem(body, "name");
if (!cJSON_IsString(name) || !name->valuestring[0]) {
cJSON_Delete(body);
return api_error(req, "400 Bad Request", "name is required");
return api_error(req, "400 Bad Request", "Zone name cannot be empty");
}
cJSON *names = cJSON_GetObjectItem(body, "names");
if (!names) {
@@ -565,9 +572,26 @@ static esp_err_t create_zone(httpd_req_t *req)
const cJSON *presets = cJSON_GetObjectItem(body, "presets");
cJSON_AddItemToObject(z, "presets", cJSON_IsArray(presets) ? cJSON_Duplicate(presets, true) : cJSON_CreateArray());
cJSON_AddNullToObject(z, "default_preset");
cJSON_AddArrayToObject(z, "group_ids");
cJSON_AddObjectToObject(z, "preset_group_ids");
cJSON_AddArrayToObject(z, "sequence_ids");
cJSON_AddNumberToObject(z, "brightness", 255);
cJSON_DeleteItemFromObject(body, "ids");
const cJSON *kind = cJSON_GetObjectItem(body, "content_kind");
if (kind && !(cJSON_IsString(kind) && (!strcmp(kind->valuestring, "presets") || !strcmp(kind->valuestring, "sequences")))) {
cJSON_DeleteItemFromObject(body, "content_kind");
}
api_merge(z, body);
cJSON_Delete(body);
cJSON *gids = cJSON_CreateArray(), *g;
cJSON_ArrayForEach(g, cJSON_GetObjectItem(z, "group_ids"))
{
char id[24];
if (api_id(g, id, sizeof(id))) {
cJSON_AddItemToArray(gids, cJSON_CreateString(id));
}
}
api_set(z, "group_ids", gids);
char id[16];
api_add("zone", z, id, sizeof(id));
cJSON *list = profile_zone_list();
@@ -786,3 +810,56 @@ bool zones_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
}
return false;
}
/* -------------------------------------------------------------- migration */
// Preset ids a zone lists (presets_flat, else presets as a flat list or rows)
static bool zone_has_presets(const cJSON *z)
{
const cJSON *flat = cJSON_GetObjectItem(z, "presets_flat");
if (cJSON_IsArray(flat)) {
return cJSON_GetArraySize(flat) > 0;
}
const cJSON *row;
cJSON_ArrayForEach(row, cJSON_GetObjectItem(z, "presets"))
{
if (cJSON_IsString(row) || cJSON_GetArraySize(row) > 0) {
return true;
}
}
return false;
}
// main's Zone.load: group_ids, preset_group_ids, sequence_ids and content_kind on every zone
void api_migrate_zones(void)
{
bool changed = false;
cJSON *z;
cJSON_ArrayForEach(z, store_coll("zone"))
{
if (!cJSON_IsObject(z)) {
continue;
}
if (!cJSON_IsArray(cJSON_GetObjectItem(z, "group_ids"))) {
api_set(z, "group_ids", cJSON_CreateArray());
changed = true;
}
if (!cJSON_IsObject(cJSON_GetObjectItem(z, "preset_group_ids"))) {
api_set(z, "preset_group_ids", cJSON_CreateObject());
changed = true;
}
if (!cJSON_IsArray(cJSON_GetObjectItem(z, "sequence_ids"))) {
api_set(z, "sequence_ids", cJSON_CreateArray());
changed = true;
}
const cJSON *kind = cJSON_GetObjectItem(z, "content_kind");
if (!(cJSON_IsString(kind) && (!strcmp(kind->valuestring, "presets") || !strcmp(kind->valuestring, "sequences")))) {
bool seqs = cJSON_GetArraySize(cJSON_GetObjectItem(z, "sequence_ids")) > 0;
api_set(z, "content_kind", cJSON_CreateString(seqs && !zone_has_presets(z) ? "sequences" : "presets"));
changed = true;
}
}
if (changed) {
store_save("zone");
}
}