feat(streaming): introduce WebSocket Streaming API bridging event bus
ApiServer: add AsyncWebSocket at /ws; accept JSON {event, payload} (string or object) and dispatch via ctx.fire; mirror all local events to clients using NodeContext::onAny.\nNodeContext: add onAny subscriber API.\nNeoPatternService: add api/neopattern/color event to set solid color.\nCluster: centralize cluster/broadcast sending in core; services delegate.\nAPI: add generic /api/node/event and /api/cluster/event endpoints in respective services.\nTests: add ws-color-client, ws-cluster-broadcast-color, http-cluster-broadcast-color.\nDocs: add StreamingAPI.md; update README and test/README.\nFixes: robust WS JSON parsing on ESP8266 and payload handling.
This commit is contained in:
@@ -205,6 +205,44 @@ void NeoPatternService::registerEventHandlers() {
|
||||
LOG_INFO("NeoPattern", "Applied control from CLUSTER_EVENT");
|
||||
}
|
||||
});
|
||||
|
||||
// Solid color event: sets all pixels to the same color
|
||||
ctx.on("api/neopattern/color", [this](void* dataPtr) {
|
||||
String* jsonStr = static_cast<String*>(dataPtr);
|
||||
if (!jsonStr) {
|
||||
LOG_WARN("NeoPattern", "Received api/neopattern/color with null dataPtr");
|
||||
return;
|
||||
}
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, *jsonStr);
|
||||
if (err) {
|
||||
LOG_WARN("NeoPattern", String("Failed to parse color event data: ") + err.c_str());
|
||||
return;
|
||||
}
|
||||
JsonObject obj = doc.as<JsonObject>();
|
||||
// color can be string or number
|
||||
String colorStr;
|
||||
if (obj["color"].is<const char*>() || obj["color"].is<String>()) {
|
||||
colorStr = obj["color"].as<String>();
|
||||
} else if (obj["color"].is<long>() || obj["color"].is<int>()) {
|
||||
colorStr = String(obj["color"].as<long>());
|
||||
} else {
|
||||
LOG_WARN("NeoPattern", "api/neopattern/color missing 'color'");
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional brightness
|
||||
if (obj["brightness"].is<int>() || obj["brightness"].is<long>()) {
|
||||
int b = obj["brightness"].as<int>();
|
||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||
setBrightness(static_cast<uint8_t>(b));
|
||||
}
|
||||
|
||||
uint32_t color = parseColor(colorStr);
|
||||
setPattern(NeoPatternType::NONE);
|
||||
setColor(color);
|
||||
LOG_INFO("NeoPattern", String("Set solid color ") + colorStr);
|
||||
});
|
||||
}
|
||||
|
||||
bool NeoPatternService::applyControlParams(const JsonObject& obj) {
|
||||
|
||||
Reference in New Issue
Block a user