Add CLUSTER_EVENT message type and end-to-end handling across cluster and NeoPattern example.\n\nCluster protocol / core:\n- Add ClusterProtocol::CLUSTER_EVENT_MSG\n- ClusterManager: register predicate/handler for CLUSTER_EVENT\n- Robust parsing: accept data as string or nested JSON; serialize nested data to string before firing\n- Add defensive null-termination for full UDP reads; log unknown message head if no handler matches\n- Logging: source IP, payload length, missing fields, and pre-fire event details\n\nNeoPatternService:\n- Constructor now accepts NodeContext and registers ctx.on(api/neopattern) handler\n- /api/neopattern: add optional boolean 'broadcast' flag\n- If broadcast=true: build event payload and send CLUSTER_EVENT over UDP (subnet-directed broadcast); log target and payload size; also fire locally so sender applies immediately\n- Implement applyControlParams with robust ArduinoJson is<T>() checks (replaces deprecated containsKey()) and flexible string/number parsing for color, brightness, steps, interval\n- Minor fixes: include Globals for ClusterProtocol, include ESP8266WiFi for broadcast IP calc, safer brightness clamping\n\nExample:\n- Update neopattern main to pass NodeContext into NeoPatternService\n\nResult:\n- Nodes receive and process CLUSTER_EVENT (api/neopattern) via ctx.fire/ctx.on\n- Broadcast reliably reaches peers; parsing handles both stringified and nested JSON data\n- Additional logs aid diagnosis of delivery/format issues and remove deprecation warnings
100 lines
2.9 KiB
C++
100 lines
2.9 KiB
C++
#pragma once
|
|
#include "spore/Service.h"
|
|
#include "spore/core/TaskManager.h"
|
|
#include "spore/core/NodeContext.h"
|
|
#include "NeoPattern.h"
|
|
#include "NeoPatternState.h"
|
|
#include "NeoPixelConfig.h"
|
|
#include "PatternRegistry.h"
|
|
#include <map>
|
|
#include <functional>
|
|
|
|
class NeoPatternService : public Service {
|
|
public:
|
|
enum class NeoPatternType {
|
|
NONE = 0,
|
|
RAINBOW_CYCLE = 1,
|
|
THEATER_CHASE = 2,
|
|
COLOR_WIPE = 3,
|
|
SCANNER = 4,
|
|
FADE = 5,
|
|
FIRE = 6
|
|
};
|
|
|
|
enum class NeoDirection {
|
|
FORWARD,
|
|
REVERSE
|
|
};
|
|
|
|
NeoPatternService(NodeContext& ctx, TaskManager& taskMgr, const NeoPixelConfig& config);
|
|
~NeoPatternService();
|
|
|
|
void registerEndpoints(ApiServer& api) override;
|
|
const char* getName() const override { return "NeoPattern"; }
|
|
|
|
// Pattern control methods
|
|
void setPattern(NeoPatternType pattern);
|
|
void setPatternByName(const String& name);
|
|
void setColor(uint32_t color);
|
|
void setColor2(uint32_t color2);
|
|
void setBrightness(uint8_t brightness);
|
|
void setTotalSteps(uint16_t steps);
|
|
void setDirection(NeoDirection direction);
|
|
void setUpdateInterval(unsigned long interval);
|
|
|
|
// State management
|
|
void setState(const NeoPatternState& state);
|
|
NeoPatternState getState() const;
|
|
|
|
private:
|
|
void registerTasks();
|
|
void registerPatterns();
|
|
void update();
|
|
void registerEventHandlers();
|
|
bool applyControlParams(const JsonObject& obj);
|
|
|
|
// Pattern updaters
|
|
void updateRainbowCycle();
|
|
void updateTheaterChase();
|
|
void updateColorWipe();
|
|
void updateScanner();
|
|
void updateFade();
|
|
void updateFire();
|
|
void updateNone();
|
|
|
|
// API handlers
|
|
void handleStatusRequest(AsyncWebServerRequest* request);
|
|
void handlePatternsRequest(AsyncWebServerRequest* request);
|
|
void handleControlRequest(AsyncWebServerRequest* request);
|
|
void handleStateRequest(AsyncWebServerRequest* request);
|
|
|
|
// Utility methods
|
|
std::vector<String> patternNamesVector() const;
|
|
String currentPatternName() const;
|
|
NeoPatternType nameToPattern(const String& name) const;
|
|
void resetStateForPattern(NeoPatternType pattern);
|
|
uint32_t parseColor(const String& colorStr) const;
|
|
|
|
// Pattern validation methods
|
|
bool isValidPattern(const String& name) const;
|
|
bool isValidPattern(NeoPatternType type) const;
|
|
bool patternRequiresColor2(const String& name) const;
|
|
bool patternSupportsDirection(const String& name) const;
|
|
String getPatternDescription(const String& name) const;
|
|
|
|
TaskManager& taskManager;
|
|
NodeContext& ctx;
|
|
NeoPattern* neoPattern;
|
|
NeoPixelConfig config;
|
|
NeoPatternState currentState;
|
|
|
|
// Pattern registry for centralized pattern management
|
|
PatternRegistry patternRegistry;
|
|
|
|
NeoPatternType activePattern;
|
|
NeoDirection direction;
|
|
unsigned long updateIntervalMs;
|
|
unsigned long lastUpdateMs;
|
|
bool initialized;
|
|
};
|