Files
led-controller/main/res_profile.c
T
jimmyandClaude Opus 5.5 04b56c6ef2 feat: device groups, combined brightness and beat-driven sequences from main
Groups (registry of driver MACs, optionally scoped to a profile) become
the drivers' group membership, alongside z<zone id> for zones that still
name drivers. Zones play to their group_ids. Group and device routes for
driver config, brightness (global x groups x device x zone) and identify.
The push path takes main's message shape: select as a list, groups as a
filter (one GROUP_CMD per group, or JSON when a preset has a background),
short keys so JSON fits a frame.

Sequences follow main: lanes of {preset_id, beats} steps played to each
lane's groups, advanced by the mic's beats or a clock at
audio_simulated_bpm; a switch waits for the next beat or downbeat; sync-
phase, stop (clears the zone's drivers) and status. This replaces the
time-based per-zone sequencer and scene playback, as in main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 18:09:42 +13:00

789 lines
28 KiB
C

// Profiles, zones and presets: the v1 app's controllers/profile.py, zone.py and preset.py
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "api_util.h"
#include "engine.h"
#include "seqplay.h"
#include "store.h"
#include "web.h"
/* ---------------------------------------------------------------- presets */
// A new preset before the request's fields are applied
static cJSON *preset_defaults(const char *profile_id)
{
cJSON *p = cJSON_CreateObject();
cJSON_AddStringToObject(p, "name", "");
cJSON_AddStringToObject(p, "pattern", "");
cJSON_AddArrayToObject(p, "colors");
cJSON_AddNumberToObject(p, "brightness", 0);
cJSON_AddNumberToObject(p, "delay", 0);
for (int i = 1; i <= 8; i++) {
char k[4];
snprintf(k, sizeof(k), "n%d", i);
cJSON_AddNumberToObject(p, k, 0);
}
cJSON_AddStringToObject(p, "profile_id", profile_id);
return p;
}
static esp_err_t send_keyed(httpd_req_t *req, const char *status, const char *id, cJSON *rec)
{
cJSON *o = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(o, id, rec);
return api_json(req, status, o, true);
}
static esp_err_t push_presets(httpd_req_t *req)
{
cJSON *body = api_body(req);
if (!body) {
return ESP_OK;
}
const cJSON *seq = cJSON_GetObjectItem(body, "sequence");
const cJSON *payload = cJSON_GetObjectItem(body, "payload");
cJSON *one = NULL;
if (!cJSON_IsArray(seq) && payload) {
one = cJSON_CreateArray();
cJSON_AddItemReferenceToArray(one, (cJSON *)payload);
seq = one;
}
esp_err_t err = ESP_ERR_INVALID_ARG;
if (cJSON_IsArray(seq) && cJSON_GetArraySize(seq)) {
err = engine_push(seq, cJSON_GetObjectItem(body, "targets"));
}
cJSON_Delete(one);
cJSON_Delete(body);
return err == ESP_ERR_INVALID_ARG ? api_error(req, "400 Bad Request", "sequence or payload required")
: api_result(req, err, "Delivered");
}
static esp_err_t send_presets(httpd_req_t *req)
{
cJSON *body = api_body(req);
if (!body) {
return ESP_OK;
}
const cJSON *ids = cJSON_GetObjectItem(body, "preset_ids");
if (!cJSON_IsArray(ids)) {
ids = cJSON_GetObjectItem(body, "ids");
}
if (!cJSON_IsArray(ids) || !cJSON_GetArraySize(ids)) {
cJSON_Delete(body);
return api_error(req, "400 Bad Request", "preset_ids must be a non-empty list");
}
const cJSON *save = cJSON_GetObjectItem(body, "save");
char def[24];
api_id(cJSON_GetObjectItem(body, "default"), def, sizeof(def));
const cJSON *targets = cJSON_GetObjectItem(body, "targets");
cJSON *single = NULL;
const cJSON *dest = cJSON_GetObjectItem(body, "destination_mac");
if (!cJSON_IsArray(targets) && cJSON_IsString(dest)) {
single = cJSON_CreateArray();
cJSON_AddItemToArray(single, cJSON_CreateString(dest->valuestring));
targets = single;
}
int sent = 0;
esp_err_t err = engine_send_presets(ids, !save || cJSON_IsTrue(save), def[0] ? def : NULL, targets, &sent);
cJSON_Delete(single);
cJSON_Delete(body);
if (err != ESP_OK) {
return api_result(req, err, "");
}
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "Presets sent");
cJSON_AddNumberToObject(o, "presets_sent", sent);
return api_json(req, "200 OK", o, true);
}
bool presets_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
{
const int method = req->method;
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "push")) {
*ret = push_presets(req);
return true;
}
if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "send")) {
*ret = send_presets(req);
return true;
}
const char *profile = api_current_profile();
if (n == 1 && method == HTTP_POST) {
if (!profile) {
*ret = api_error(req, "404 Not Found", "No profile available");
return true;
}
cJSON *body = api_body(req);
if (!body) {
*ret = ESP_OK;
return true;
}
cJSON *rec = preset_defaults(profile);
api_merge(rec, body);
cJSON_Delete(body);
api_set(rec, "profile_id", cJSON_CreateString(profile));
char id[16];
api_add("preset", rec, id, sizeof(id));
store_save("preset");
*ret = send_keyed(req, "201 Created", id, rec);
return true;
}
if (n == 2 && method == HTTP_PUT) {
cJSON *rec = cJSON_GetObjectItem(store_coll("preset"), seg[1]);
if (!rec || !api_preset_in_profile(rec)) {
*ret = api_error(req, "404 Not Found", "Preset not found");
return true;
}
cJSON *body = api_body(req);
if (!body) {
*ret = ESP_OK;
return true;
}
api_merge(rec, body);
cJSON_Delete(body);
if (profile) {
api_set(rec, "profile_id", cJSON_CreateString(profile));
}
store_save("preset");
*ret = api_json(req, "200 OK", rec, false);
return true;
}
return false;
}
/* --------------------------------------------------------------- profiles */
typedef struct {
const char *name, *pattern, *colors;
int brightness, delay;
int n[4]; // n1..n4
} seed_t;
static const seed_t SEED[] = {
{ "on", "on", "[\"#FFFFFF\"]", 255, 100, { 0 } },
{ "off", "off", "[]", 0, 100, { 0 } },
{ "rainbow", "rainbow", "[]", 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 } },
{ "flame", "flame", "[]", 255, 50, { 35, 2600 } },
{ "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 Single Color", "on", "[\"#ff00ff\"]", 220, 100, { 0 } },
{ "DJ Transition", "transition", "[\"#ff0000\",\"#00ff00\",\"#0000ff\"]", 220, 250, { 0 } },
};
// Create the seed presets and a zone that lists them; returns the zone id in zid
static void seed_zone(const char *profile, const char *zone_name, const char *driver_name, bool with_auto,
const seed_t *seed, int count, char *zid, size_t zlen)
{
cJSON *flat = cJSON_CreateArray();
for (int i = 0; i < count; i++) {
cJSON *p = 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));
api_set(p, "brightness", cJSON_CreateNumber(seed[i].brightness));
api_set(p, "delay", cJSON_CreateNumber(seed[i].delay));
for (int k = 0; k < 4; k++) {
char key[4];
snprintf(key, sizeof(key), "n%d", k + 1);
api_set(p, key, cJSON_CreateNumber(seed[i].n[k]));
}
if (with_auto) {
cJSON_AddTrueToObject(p, "auto");
}
char pid[16];
api_add("preset", p, pid, sizeof(pid));
cJSON_AddItemToArray(flat, cJSON_CreateString(pid));
}
cJSON *z = cJSON_CreateObject();
cJSON_AddStringToObject(z, "name", zone_name);
cJSON *names = cJSON_AddArrayToObject(z, "names");
cJSON_AddItemToArray(names, cJSON_CreateString(driver_name));
cJSON *rows = cJSON_AddArrayToObject(z, "presets");
cJSON_AddItemToArray(rows, cJSON_Duplicate(flat, true));
cJSON_AddItemToObject(z, "presets_flat", flat);
cJSON *first = cJSON_GetArrayItem(flat, 0);
cJSON_AddItemToObject(z, "default_preset", first ? cJSON_Duplicate(first, false) : cJSON_CreateNull());
api_add("zone", z, zid, zlen);
}
static bool truthy(const cJSON *v)
{
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);
}
// POST /profiles: the profile, an empty palette, the seed presets and a "default" zone (plus a "dj"
// zone with seed_dj_zone)
static esp_err_t create_profile(httpd_req_t *req)
{
cJSON *body = api_body(req);
if (!body) {
return ESP_OK;
}
bool dj = truthy(cJSON_GetObjectItem(body, "seed_dj_zone"));
cJSON_DeleteItemFromObject(body, "seed_dj_zone");
char palid[16], pid[16], zid[16], djid[16];
api_add("palette", cJSON_CreateArray(), palid, sizeof(palid));
cJSON *prof = cJSON_CreateObject();
cJSON_AddStringToObject(prof, "name", "");
cJSON_AddStringToObject(prof, "type", "zones");
cJSON_AddArrayToObject(prof, "zones");
cJSON_AddArrayToObject(prof, "scenes");
api_merge(prof, body);
cJSON_Delete(body);
api_set(prof, "palette_id", cJSON_CreateString(palid));
api_add("profile", prof, pid, sizeof(pid));
cJSON *zones = cJSON_CreateArray();
seed_zone(pid, "default", "1", true, SEED, sizeof(SEED) / sizeof(SEED[0]), zid, sizeof(zid));
cJSON_AddItemToArray(zones, cJSON_CreateString(zid));
if (dj) {
seed_zone(pid, "dj", "dj", false, SEED_DJ, sizeof(SEED_DJ) / sizeof(SEED_DJ[0]), djid, sizeof(djid));
cJSON_AddItemToArray(zones, cJSON_CreateString(djid));
}
api_set(prof, "zones", zones);
store_save("palette");
store_save("preset");
store_save("zone");
store_save("profile");
engine_groups_changed();
return send_keyed(req, "201 Created", pid, prof);
}
static const cJSON *profile_zones(const cJSON *prof)
{
const cJSON *z = cJSON_GetObjectItem(prof, "zones");
if (!cJSON_IsArray(z)) {
z = cJSON_GetObjectItem(prof, "zone_order");
}
return cJSON_IsArray(z) ? z : NULL;
}
// Copy of a preset reference list (ids, possibly nested in rows) with ids mapped through map
static cJSON *remap(const cJSON *v, const cJSON *map)
{
if (cJSON_IsArray(v)) {
cJSON *out = cJSON_CreateArray();
const cJSON *it;
cJSON_ArrayForEach(it, v)
{
cJSON_AddItemToArray(out, remap(it, map));
}
return out;
}
char id[24];
const cJSON *to = api_id(v, id, sizeof(id)) ? cJSON_GetObjectItem(map, id) : NULL;
return to ? cJSON_Duplicate(to, false) : cJSON_Duplicate(v, true);
}
// POST /profiles/<id>/clone: the profile with its own copies of its palette, zones and presets
static esp_err_t clone_profile(httpd_req_t *req, const char *src_id)
{
cJSON *src = cJSON_GetObjectItem(store_coll("profile"), src_id);
if (!src) {
return api_error(req, "404 Not Found", "Profile not found");
}
cJSON *body = api_body_opt(req);
if (!body) {
return ESP_OK;
}
const cJSON *name = cJSON_GetObjectItem(body, "name");
cJSON *prof = cJSON_Duplicate(src, true);
if (cJSON_IsString(name) && name->valuestring[0]) {
api_set(prof, "name", cJSON_CreateString(name->valuestring));
}
cJSON_Delete(body);
char pid[16], id[16];
api_add("profile", prof, pid, sizeof(pid));
char palsrc[24];
const cJSON *pal = api_id(cJSON_GetObjectItem(src, "palette_id"), palsrc, sizeof(palsrc))
? cJSON_GetObjectItem(store_coll("palette"), palsrc)
: NULL;
if (cJSON_IsObject(pal)) { // legacy {"colors": [...]}
pal = cJSON_GetObjectItem(pal, "colors");
}
api_add("palette", cJSON_IsArray(pal) ? cJSON_Duplicate(pal, true) : cJSON_CreateArray(), id, sizeof(id));
api_set(prof, "palette_id", cJSON_CreateString(id));
// Every preset of the source profile gets a copy owned by the clone
cJSON *map = cJSON_CreateObject();
cJSON *presets = store_coll("preset");
cJSON *ids = cJSON_CreateArray(), *it;
cJSON_ArrayForEach(it, presets)
{
char owner[24];
if (api_id(cJSON_GetObjectItem(it, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, src_id)) {
cJSON_AddItemToArray(ids, cJSON_CreateString(it->string));
}
}
cJSON_ArrayForEach(it, ids)
{
cJSON *copy = cJSON_Duplicate(cJSON_GetObjectItem(presets, it->valuestring), true);
api_set(copy, "profile_id", cJSON_CreateString(pid));
api_add("preset", copy, id, sizeof(id));
cJSON_AddStringToObject(map, it->valuestring, id);
}
cJSON_Delete(ids);
cJSON *zones = cJSON_CreateArray();
const cJSON *zid;
cJSON_ArrayForEach(zid, profile_zones(src))
{
char key[24];
cJSON *z = api_id(zid, key, sizeof(key)) ? cJSON_GetObjectItem(store_coll("zone"), key) : NULL;
if (!z) {
continue;
}
cJSON *copy = cJSON_Duplicate(z, true);
static const char *const REFS[] = { "presets", "presets_flat", "default_preset" };
for (size_t i = 0; i < sizeof(REFS) / sizeof(REFS[0]); i++) {
const cJSON *v = cJSON_GetObjectItem(z, REFS[i]);
if (v && !cJSON_IsNull(v)) {
api_set(copy, REFS[i], remap(v, map));
}
}
api_add("zone", copy, id, sizeof(id));
cJSON_AddItemToArray(zones, cJSON_CreateString(id));
}
cJSON_Delete(map);
api_set(prof, "zones", zones);
cJSON_DeleteItemFromObject(prof, "zone_order");
store_save("palette");
store_save("preset");
store_save("zone");
store_save("profile");
engine_groups_changed();
return send_keyed(req, "201 Created", pid, prof);
}
// Is id listed by any profile other than skip under key (a list, or a single value)?
static bool used_elsewhere(const char *skip, const char *key, const char *id)
{
const cJSON *p;
cJSON_ArrayForEach(p, store_coll("profile"))
{
if (!strcmp(p->string, skip)) {
continue;
}
const cJSON *v = cJSON_GetObjectItem(p, key), *it;
char buf[24];
if (!cJSON_IsArray(v)) {
if (api_id(v, buf, sizeof(buf)) && !strcmp(buf, id)) {
return true;
}
continue;
}
cJSON_ArrayForEach(it, v)
{
if (api_id(it, buf, sizeof(buf)) && !strcmp(buf, id)) {
return true;
}
}
}
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.
static esp_err_t delete_profile(httpd_req_t *req, const char *id)
{
char pid[24];
snprintf(pid, sizeof(pid), "%s", id);
cJSON *prof = cJSON_GetObjectItem(store_coll("profile"), pid);
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));
}
it = next;
}
const cJSON *z;
cJSON_ArrayForEach(z, profile_zones(prof))
{
char zid[24];
if (api_id(z, zid, sizeof(zid)) && !used_elsewhere(pid, "zones", zid) && !used_elsewhere(pid, "zone_order", zid)) {
cJSON_DeleteItemFromObject(store_coll("zone"), zid);
}
}
char pal[24];
if (api_id(cJSON_GetObjectItem(prof, "palette_id"), pal, sizeof(pal)) && !used_elsewhere(pid, "palette_id", pal)) {
cJSON_DeleteItemFromObject(store_coll("palette"), pal);
}
cJSON_DeleteItemFromObject(store_coll("profile"), pid);
const cJSON *cur = cJSON_GetObjectItem(store_settings(), "current_profile_id");
if (cJSON_IsString(cur) && !strcmp(cur->valuestring, pid)) {
cJSON_DeleteItemFromObject(store_settings(), "current_profile_id");
store_save("settings");
}
store_save("preset");
store_save("zone");
store_save("palette");
store_save("profile");
engine_groups_changed();
return api_message(req, "200 OK", "Profile deleted successfully");
}
bool profiles_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
{
const int method = req->method;
if (n == 2 && method == HTTP_DELETE && strcmp(seg[1], "current")) {
*ret = delete_profile(req, seg[1]);
return true;
}
if (n == 1 && method == HTTP_POST) {
*ret = create_profile(req);
return true;
}
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "clone")) {
*ret = clone_profile(req, seg[1]);
return true;
}
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "apply")) {
if (!cJSON_GetObjectItem(store_coll("profile"), seg[1])) {
*ret = api_error(req, "404 Not Found", "Profile not found");
return true;
}
api_set(store_settings(), "current_profile_id", cJSON_CreateString(seg[1]));
store_save("settings");
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "Profile applied");
cJSON_AddStringToObject(o, "id", seg[1]);
*ret = api_json(req, "200 OK", o, true);
return true;
}
return false;
}
/* ------------------------------------------------------------------ zones */
#define ZONE_COOKIE "current_zone"
static cJSON *current_profile_rec(void)
{
const char *cur = api_current_profile();
return cur ? cJSON_GetObjectItem(store_coll("profile"), cur) : NULL;
}
// The current profile's zone list, created when the profile has none
static cJSON *profile_zone_list(void)
{
cJSON *prof = current_profile_rec();
if (!prof) {
return NULL;
}
cJSON *z = (cJSON *)profile_zones(prof);
if (!z) {
z = cJSON_AddArrayToObject(prof, "zones");
}
return z;
}
static void list_remove(cJSON *list, const char *id)
{
for (int i = cJSON_GetArraySize(list) - 1; i >= 0; i--) {
char key[24];
if (api_id(cJSON_GetArrayItem(list, i), key, sizeof(key)) && !strcmp(key, id)) {
cJSON_DeleteItemFromArray(list, i);
}
}
}
// Zone picked with set-current (cookie), else the current profile's first zone; NULL when none
static const char *current_zone(httpd_req_t *req, char *buf, size_t len)
{
size_t n = len;
if (httpd_req_get_cookie_val(req, ZONE_COOKIE, buf, &n) == ESP_OK && buf[0] &&
cJSON_GetObjectItem(store_coll("zone"), buf)) {
return buf;
}
cJSON *prof = current_profile_rec();
const cJSON *first = prof ? cJSON_GetArrayItem(profile_zones(prof), 0) : NULL;
return api_id(first, buf, len) ? buf : NULL;
}
static void set_zone_cookie(httpd_req_t *req, const char *zone_id)
{
static char hdr[96]; // must outlive the response send
if (zone_id) {
snprintf(hdr, sizeof(hdr), ZONE_COOKIE "=%s; Path=/; Max-Age=31536000; SameSite=Lax", zone_id);
} else {
snprintf(hdr, sizeof(hdr), ZONE_COOKIE "=; Path=/; Max-Age=0; SameSite=Lax");
}
httpd_resp_set_hdr(req, "Set-Cookie", hdr);
}
static esp_err_t list_zones(httpd_req_t *req)
{
char cur[24];
const char *zid = current_zone(req, cur, sizeof(cur));
const char *pid = api_current_profile();
cJSON *prof = current_profile_rec();
const cJSON *order = prof ? profile_zones(prof) : NULL;
cJSON *o = cJSON_CreateObject();
cJSON_AddItemReferenceToObject(o, "zones", store_coll("zone"));
cJSON_AddItemToObject(o, "zone_order", order ? cJSON_Duplicate(order, true) : cJSON_CreateArray());
cJSON_AddItemToObject(o, "current_zone_id", zid ? cJSON_CreateString(zid) : cJSON_CreateNull());
cJSON_AddItemToObject(o, "profile_id", pid ? cJSON_CreateString(pid) : cJSON_CreateNull());
return api_json(req, "200 OK", o, true);
}
static esp_err_t create_zone(httpd_req_t *req)
{
cJSON *body = api_body(req);
if (!body) {
return ESP_OK;
}
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");
}
cJSON *names = cJSON_GetObjectItem(body, "names");
if (!names) {
names = cJSON_GetObjectItem(body, "ids");
}
cJSON *z = cJSON_CreateObject();
cJSON_AddStringToObject(z, "name", name->valuestring);
cJSON_AddItemToObject(z, "names", cJSON_IsArray(names) ? cJSON_Duplicate(names, true) : cJSON_CreateArray());
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_DeleteItemFromObject(body, "ids");
api_merge(z, body);
cJSON_Delete(body);
char id[16];
api_add("zone", z, id, sizeof(id));
cJSON *list = profile_zone_list();
if (list) {
cJSON_AddItemToArray(list, cJSON_CreateString(id));
store_save("profile");
}
store_save("zone");
engine_groups_changed();
return send_keyed(req, "201 Created", id, z);
}
static esp_err_t clone_zone(httpd_req_t *req, const char *src_id)
{
cJSON *src = cJSON_GetObjectItem(store_coll("zone"), src_id);
if (!src) {
return api_error(req, "404 Not Found", "Zone not found");
}
cJSON *body = api_body_opt(req);
if (!body) {
return ESP_OK;
}
cJSON *z = cJSON_Duplicate(src, true);
const cJSON *name = cJSON_GetObjectItem(body, "name"), *src_name = cJSON_GetObjectItem(src, "name");
char copy_name[96];
if (cJSON_IsString(name) && name->valuestring[0]) {
snprintf(copy_name, sizeof(copy_name), "%s", name->valuestring);
} else if (cJSON_IsString(src_name) && src_name->valuestring[0]) {
snprintf(copy_name, sizeof(copy_name), "%s Copy", src_name->valuestring);
} else {
snprintf(copy_name, sizeof(copy_name), "Zone %s Copy", src_id);
}
cJSON_Delete(body);
api_set(z, "name", cJSON_CreateString(copy_name));
char id[16];
api_add("zone", z, id, sizeof(id));
cJSON *list = profile_zone_list();
if (list) {
cJSON_AddItemToArray(list, cJSON_CreateString(id));
store_save("profile");
}
store_save("zone");
engine_groups_changed();
return send_keyed(req, "201 Created", id, z);
}
static esp_err_t delete_zone(httpd_req_t *req, const char *id)
{
char cur[24];
const char *curz = current_zone(req, cur, sizeof(cur));
if (!strcmp(id, "current")) {
if (!curz) {
return api_error(req, "404 Not Found", "No current zone set");
}
id = curz;
}
if (!cJSON_GetObjectItem(store_coll("zone"), id)) {
return api_error(req, "404 Not Found", "Zone not found");
}
char key[24];
snprintf(key, sizeof(key), "%s", id);
cJSON *prof;
cJSON_ArrayForEach(prof, store_coll("profile"))
{
list_remove((cJSON *)profile_zones(prof), key);
}
if (curz && !strcmp(curz, key)) {
set_zone_cookie(req, NULL);
}
cJSON_DeleteItemFromObject(store_coll("zone"), key);
store_save("profile");
store_save("zone");
engine_groups_changed();
return api_message(req, "200 OK", "Zone deleted successfully");
}
// htmx swaps this in as a zone's tab content; a plain request gets the whole page
static esp_err_t content_fragment(httpd_req_t *req, const char *id)
{
char cur[24];
if (!strcmp(id, "current")) {
id = current_zone(req, cur, sizeof(cur));
if (!id) {
return api_error(req, "404 Not Found", "No current zone set");
}
}
if (!cJSON_GetObjectItem(store_coll("zone"), id)) {
httpd_resp_set_status(req, "404 Not Found");
httpd_resp_set_type(req, "text/html");
return httpd_resp_sendstr(req, "<div>Zone not found</div>");
}
set_zone_cookie(req, id);
if (httpd_req_get_hdr_value_len(req, "HX-Request") == 0) {
return web_send_page(req, "index.html");
}
char html[320];
snprintf(html, sizeof(html),
"<div class=\"presets-section\" data-zone-id=\"%s\"><h3>Presets</h3>"
"<div class=\"profiles-actions\" style=\"margin-bottom: 1rem;\"></div>"
"<div id=\"presets-list-zone\" class=\"presets-list\"></div></div>",
id);
httpd_resp_set_type(req, "text/html");
return httpd_resp_sendstr(req, html);
}
bool zones_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
{
const int method = req->method;
if (n == 1) {
if (method == HTTP_GET) {
*ret = list_zones(req);
return true;
}
if (method == HTTP_POST) {
*ret = create_zone(req);
return true;
}
return false;
}
if (n == 2 && method == HTTP_GET && !strcmp(seg[1], "current")) {
char cur[24];
const char *zid = current_zone(req, cur, sizeof(cur));
cJSON *o = cJSON_CreateObject();
if (!zid) {
cJSON_AddStringToObject(o, "error", "No current zone set");
cJSON_AddNullToObject(o, "zone");
cJSON_AddNullToObject(o, "zone_id");
*ret = api_json(req, "404 Not Found", o, true);
return true;
}
cJSON_AddItemReferenceToObject(o, "zone", cJSON_GetObjectItem(store_coll("zone"), zid));
cJSON_AddStringToObject(o, "zone_id", zid);
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (n == 2 && method == HTTP_DELETE) {
*ret = delete_zone(req, seg[1]);
return true;
}
if (n != 3) {
return false;
}
if (!strcmp(seg[1], "engine") && !strcmp(seg[2], "status")) {
*ret = api_json(req, "200 OK", seqplay_status(), true);
return true;
}
if (method == HTTP_GET && !strcmp(seg[2], "content-fragment")) {
*ret = content_fragment(req, seg[1]);
return true;
}
if (method != HTTP_POST) {
return false;
}
if (!strcmp(seg[2], "set-current")) {
if (!cJSON_GetObjectItem(store_coll("zone"), seg[1])) {
*ret = api_error(req, "404 Not Found", "Zone not found");
return true;
}
set_zone_cookie(req, seg[1]);
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "Current zone set");
cJSON_AddStringToObject(o, "zone_id", seg[1]);
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (!strcmp(seg[2], "clone")) {
*ret = clone_zone(req, seg[1]);
return true;
}
if (!strcmp(seg[2], "play")) {
cJSON *body = api_body_opt(req);
if (!body) {
*ret = ESP_OK;
return true;
}
char key[24];
api_id(cJSON_GetObjectItem(body, "preset_id"), key, sizeof(key));
// The UI sends drivers:false when it has pushed to the drivers itself
bool skip = cJSON_IsFalse(cJSON_GetObjectItem(body, "drivers"));
cJSON_Delete(body);
if (!key[0]) {
*ret = api_error(req, "400 Bad Request", "preset_id required");
return true;
}
if (!cJSON_GetObjectItem(store_coll("zone"), seg[1])) {
*ret = api_error(req, "404 Not Found", "Zone not found");
return true;
}
if (!cJSON_GetObjectItem(store_coll("preset"), key)) {
*ret = api_error(req, "404 Not Found", "Preset not found");
return true;
}
esp_err_t err = skip ? ESP_OK : engine_play(seg[1], key);
if (err != ESP_OK) {
*ret = api_result(req, err, "");
return true;
}
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "Playing");
cJSON_AddStringToObject(o, "zone_id", seg[1]);
cJSON_AddStringToObject(o, "preset_id", key);
*ret = api_json(req, "200 OK", o, true);
return true;
}
if (!strcmp(seg[2], "stop")) {
esp_err_t err = engine_stop(seg[1]);
if (err != ESP_OK && err != ESP_ERR_NOT_FOUND) {
*ret = api_result(req, err, "");
return true;
}
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "Stopped");
cJSON_AddBoolToObject(o, "stopped", err == ESP_OK);
*ret = api_json(req, "200 OK", o, true);
return true;
}
return false;
}