Profiles seed presets, a default zone and a palette, and clone with their own copies; deleting a profile removes what it owned. Zones get the v1 list shape, set-current, current, clone and content-fragment. Devices are keyed by MAC with v1 validation and identify. Patterns: definitions, OTA file and manifest, upload, driver, and send over ESP-NOW (the led-driver pattern sources are embedded in the firmware). Pixel outputs get v1 validation and a DDP/Art-Net/sACN test pattern. Sequences, palettes and settings follow v1 replies and validation, and /ws forwards browser messages to the drivers. The resource handlers move out of api.c into res_*.c, patterns.c and outputs.c. cJSON now allocates from PSRAM: the store had grown enough to starve internal RAM and abort the mic at boot. /status reports free_internal. The Wi-Fi AP and led-tool routes are left out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
253 lines
8.8 KiB
C
253 lines
8.8 KiB
C
// Palettes and sequences: the v1 app's controllers/palette.py and sequence.py
|
|
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "api_util.h"
|
|
#include "engine.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 */
|
|
|
|
#define DURATION_MIN_S 0.5
|
|
#define DURATION_MAX_S 600.0
|
|
#define DURATION_DEFAULT_S 5.0
|
|
|
|
static bool truthy(const cJSON *v, bool dflt)
|
|
{
|
|
if (!v || cJSON_IsNull(v)) {
|
|
return dflt;
|
|
}
|
|
if (cJSON_IsString(v)) {
|
|
return v->valuestring[0] != 0;
|
|
}
|
|
return cJSON_IsTrue(v) || (cJSON_IsNumber(v) && v->valuedouble != 0);
|
|
}
|
|
|
|
// Bring a sequence record into shape: {name, zone_id, loop, repeat_count, steps: [{preset_id,
|
|
// duration_s}], active}. Returns an error message, or NULL when doc is valid.
|
|
static const char *normalize_seq(cJSON *doc)
|
|
{
|
|
char buf[64];
|
|
const cJSON *name = cJSON_GetObjectItem(doc, "name");
|
|
const char *s = cJSON_IsString(name) ? name->valuestring : "";
|
|
while (isspace((unsigned char)*s)) {
|
|
s++;
|
|
}
|
|
snprintf(buf, sizeof(buf), "%s", s);
|
|
for (int i = strlen(buf) - 1; i >= 0 && isspace((unsigned char)buf[i]); i--) {
|
|
buf[i] = 0;
|
|
}
|
|
api_set(doc, "name", cJSON_CreateString(buf));
|
|
|
|
api_set(doc, "zone_id", api_id(cJSON_GetObjectItem(doc, "zone_id"), buf, sizeof(buf)) ? cJSON_CreateString(buf)
|
|
: cJSON_CreateNull());
|
|
api_set(doc, "loop", cJSON_CreateBool(truthy(cJSON_GetObjectItem(doc, "loop"), true)));
|
|
|
|
const cJSON *rc = cJSON_GetObjectItem(doc, "repeat_count");
|
|
int repeat = 0;
|
|
if (cJSON_IsNumber(rc)) {
|
|
repeat = (int)rc->valuedouble;
|
|
} else if (rc && !cJSON_IsNull(rc) && !cJSON_IsFalse(rc)) {
|
|
return "repeat_count must be an integer";
|
|
}
|
|
api_set(doc, "repeat_count", cJSON_CreateNumber(repeat < 0 ? 0 : repeat));
|
|
|
|
const cJSON *steps = cJSON_GetObjectItem(doc, "steps");
|
|
if (steps && !cJSON_IsNull(steps) && !cJSON_IsArray(steps)) {
|
|
return "steps must be a list";
|
|
}
|
|
cJSON *out = cJSON_CreateArray();
|
|
const cJSON *st;
|
|
cJSON_ArrayForEach(st, steps)
|
|
{
|
|
const cJSON *pid = cJSON_IsObject(st) ? cJSON_GetObjectItem(st, "preset_id") : st;
|
|
if (!api_id(pid, buf, sizeof(buf))) {
|
|
cJSON_Delete(out);
|
|
return "each step needs a preset_id";
|
|
}
|
|
const cJSON *dv = cJSON_IsObject(st) ? cJSON_GetObjectItem(st, "duration_s") : NULL;
|
|
double d = cJSON_IsNumber(dv) ? dv->valuedouble : DURATION_DEFAULT_S;
|
|
d = d < DURATION_MIN_S ? DURATION_MIN_S : (d > DURATION_MAX_S ? DURATION_MAX_S : d);
|
|
cJSON *step = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(step, "preset_id", buf);
|
|
cJSON_AddNumberToObject(step, "duration_s", d);
|
|
cJSON_AddItemToArray(out, step);
|
|
}
|
|
api_set(doc, "steps", out);
|
|
api_set(doc, "active", cJSON_CreateBool(truthy(cJSON_GetObjectItem(doc, "active"), false)));
|
|
return NULL;
|
|
}
|
|
|
|
static esp_err_t play_sequence(httpd_req_t *req, const char *id)
|
|
{
|
|
const cJSON *rec = cJSON_GetObjectItem(store_coll("sequence"), id);
|
|
if (!rec) {
|
|
return api_error(req, "404 Not Found", "Sequence not found");
|
|
}
|
|
char zone[24];
|
|
if (!api_id(cJSON_GetObjectItem(rec, "zone_id"), zone, sizeof(zone))) {
|
|
return api_error(req, "400 Bad Request", "Sequence has no zone_id");
|
|
}
|
|
if (!cJSON_GetArraySize(cJSON_GetObjectItem(rec, "steps"))) {
|
|
return api_error(req, "400 Bad Request", "Sequence has no steps");
|
|
}
|
|
if (!cJSON_GetObjectItem(store_coll("zone"), zone)) {
|
|
return api_error(req, "404 Not Found", "Zone not found");
|
|
}
|
|
esp_err_t err = engine_seq_play(id);
|
|
if (err == ESP_ERR_NO_MEM) {
|
|
return api_error(req, "503 Service Unavailable", "too many sequences running");
|
|
}
|
|
if (err != ESP_OK) {
|
|
return api_result(req, err, "");
|
|
}
|
|
cJSON *all = engine_seq_status();
|
|
cJSON *state = cJSON_DetachItemFromObject(all, id);
|
|
cJSON_Delete(all);
|
|
return api_json(req, "200 OK", state ? state : cJSON_CreateObject(), 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;
|
|
if (n == 2 && method == HTTP_GET && !strcmp(seg[1], "status")) {
|
|
*ret = api_json(req, "200 OK", engine_seq_status(), true);
|
|
return true;
|
|
}
|
|
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "play")) {
|
|
*ret = play_sequence(req, seg[1]);
|
|
return true;
|
|
}
|
|
if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "stop")) {
|
|
bool stopped = engine_seq_stop(seg[1]) == ESP_OK;
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(o, "stopped", stopped);
|
|
*ret = api_json(req, "200 OK", o, true);
|
|
return true;
|
|
}
|
|
if ((n == 1 && method == HTTP_POST) || (n == 2 && method == HTTP_PUT)) {
|
|
cJSON *old = n == 2 ? cJSON_GetObjectItem(all, seg[1]) : NULL;
|
|
if (n == 2 && !old) {
|
|
*ret = api_error(req, "404 Not Found", "Sequence not found");
|
|
return true;
|
|
}
|
|
cJSON *body = api_body(req);
|
|
if (!body) {
|
|
*ret = ESP_OK;
|
|
return true;
|
|
}
|
|
cJSON *doc = old ? cJSON_Duplicate(old, true) : cJSON_CreateObject();
|
|
api_merge(doc, body);
|
|
cJSON_Delete(body);
|
|
const char *bad = normalize_seq(doc);
|
|
if (bad) {
|
|
cJSON_Delete(doc);
|
|
*ret = api_error(req, "400 Bad Request", bad);
|
|
return true;
|
|
}
|
|
char id[16];
|
|
if (old) {
|
|
snprintf(id, sizeof(id), "%s", seg[1]);
|
|
api_set(doc, "id", cJSON_CreateString(id));
|
|
cJSON_ReplaceItemInObject(all, id, doc);
|
|
} else {
|
|
api_add("sequence", doc, id, sizeof(id));
|
|
}
|
|
store_save("sequence");
|
|
*ret = api_json(req, old ? "200 OK" : "201 Created", doc, false);
|
|
return true;
|
|
}
|
|
if (n == 2 && method == HTTP_DELETE) {
|
|
if (!cJSON_GetObjectItem(all, seg[1])) {
|
|
*ret = api_error(req, "404 Not Found", "Sequence not found");
|
|
return true;
|
|
}
|
|
engine_seq_stop(seg[1]);
|
|
cJSON_DeleteItemFromObject(all, seg[1]);
|
|
store_save("sequence");
|
|
*ret = api_message(req, "200 OK", "Sequence deleted successfully");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|