mirror of
https://gitlab.com/zwirbel/illucat.git
synced 2025-12-16 10:04:30 +01:00
uncouple generic web stuff into api plugin
This commit is contained in:
126
src/IlluCat.cpp_
Normal file
126
src/IlluCat.cpp_
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "IlluCat.h"
|
||||
|
||||
IlluCat::IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg)
|
||||
{
|
||||
Serial.println("illucat ctr");
|
||||
sprocketConfig = cfg;
|
||||
otaConfig = otaCfg;
|
||||
webConfig = webCfg;
|
||||
pixelConfig.pin = 4;
|
||||
pixelConfig.length = 8;
|
||||
pixelConfig.brightness = 32;
|
||||
pixelConfig.updateInterval = 100;
|
||||
pixelConfig.defaultColor = 100;
|
||||
catScheduler = new Scheduler();
|
||||
}
|
||||
|
||||
void IlluCat::scanningAnimation()
|
||||
{
|
||||
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
|
||||
}
|
||||
void IlluCat::defaultAnimation()
|
||||
{
|
||||
pixels->RainbowCycle(pixelConfig.updateInterval);
|
||||
}
|
||||
|
||||
/* Sprocket *IlluCat::activate(Scheduler *scheduler, Network *network)
|
||||
{
|
||||
|
||||
// load config files from SPIFFS
|
||||
if (SPIFFS.begin())
|
||||
{
|
||||
pixelConfig.fromFile("/pixelConfig.json");
|
||||
defaultState.fromFile("/pixelState.json");
|
||||
state = defaultState;
|
||||
}
|
||||
|
||||
// initialize services
|
||||
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
||||
server = new AsyncWebServer(80);
|
||||
ws = new AsyncWebSocket("/pixel");
|
||||
//wsStream = new AsyncWebSocket("/stream");
|
||||
defaultAnimation();
|
||||
|
||||
// add plugins
|
||||
// TODO add HTTP OTA instead of TCP
|
||||
//addPlugin(new OtaTcpPlugin(otaConfig));
|
||||
addPlugin(new WebServerPlugin(webConfig, server));
|
||||
addPlugin(new WebConfigPlugin(server));
|
||||
addPlugin(new PixelPlugin(pixelConfig, pixels));
|
||||
|
||||
// FIXME move to networking
|
||||
//String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString();
|
||||
//PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str());
|
||||
|
||||
// TODO move to plugin
|
||||
// setup web stuff
|
||||
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
|
||||
server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
|
||||
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
server->addHandler(ws);
|
||||
//wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6));
|
||||
//server->addHandler(wsStream);
|
||||
|
||||
return Sprocket::activate(scheduler, network);
|
||||
} using Sprocket::activate; */
|
||||
|
||||
// TODO move to utils
|
||||
/* String IlluCat::getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost)
|
||||
{
|
||||
if (request->hasParam(param, isPost))
|
||||
{
|
||||
return request->getParam(param, isPost)->value();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
void IlluCat::onStream(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
if (type == WS_EVT_DATA)
|
||||
{
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str());
|
||||
pixels->ActivePattern = NONE;
|
||||
pixels->handleStream(data, len);
|
||||
}
|
||||
}
|
||||
|
||||
void IlluCat::patternWebRequestHandler(AsyncWebServerRequest *request)
|
||||
{
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api");
|
||||
currentMessage.topic = getRequestParameterOrDefault(request, "topic", "");
|
||||
currentMessage.payload = getRequestParameterOrDefault(request, "payload", "");
|
||||
currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||
String msg = currentMessage.toJsonString();
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
if (currentMessage.broadcast)
|
||||
{
|
||||
network.broadcast(msg);
|
||||
}
|
||||
request->send(200, "text/plain", msg);
|
||||
}
|
||||
|
||||
void IlluCat::onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
|
||||
{
|
||||
if (type == WS_EVT_DATA)
|
||||
{
|
||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
dispatch(0, frame);
|
||||
network.broadcast(frame);
|
||||
}
|
||||
}
|
||||
|
||||
void IlluCat::dispatch(uint32_t from, String &msg)
|
||||
{
|
||||
currentMessage.fromJsonString(msg);
|
||||
if (currentMessage.valid)
|
||||
{
|
||||
currentMessage.from = from;
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
}
|
||||
}
|
||||
|
||||
void IlluCat::loop()
|
||||
{
|
||||
Sprocket::loop();
|
||||
yield();
|
||||
} */
|
||||
@@ -11,11 +11,12 @@
|
||||
#include "NeoPattern_api_json.h"
|
||||
#include "NeoPattern_api_modes.cpp"
|
||||
#include "utils_print.h"
|
||||
#include "utils_ws.h"
|
||||
#include "utils_web.h"
|
||||
#include <plugins/WebSO.h>
|
||||
#include <plugins/OtaTcpPlugin.cpp>
|
||||
#include <plugins/WebServerPlugin.cpp>
|
||||
#include <plugins/WebConfigPlugin.cpp>
|
||||
#include "WebApi.cpp"
|
||||
#include "PixelPlugin.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -28,8 +29,6 @@ class IlluCat : public Sprocket {
|
||||
NeoPatternDto defaultState;
|
||||
NeoPatternDto state;
|
||||
AsyncWebServer* server;
|
||||
AsyncWebSocket* ws;
|
||||
//AsyncWebSocket* wsStream;
|
||||
|
||||
NeoPixelConfig pixelConfig;
|
||||
SprocketConfig sprocketConfig;
|
||||
@@ -42,24 +41,14 @@ class IlluCat : public Sprocket {
|
||||
sprocketConfig = cfg;
|
||||
otaConfig = otaCfg;
|
||||
webConfig = webCfg;
|
||||
pixelConfig.pin = 4;
|
||||
pixelConfig.length = 8;
|
||||
pixelConfig.brightness = 32;
|
||||
pixelConfig.updateInterval = 100;
|
||||
pixelConfig.defaultColor = 100;
|
||||
}
|
||||
|
||||
virtual void scanningAnimation() {
|
||||
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
|
||||
}
|
||||
virtual void defaultAnimation() {
|
||||
pixels->RainbowCycle(pixelConfig.updateInterval);
|
||||
}
|
||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||
|
||||
// load config files from SPIFFS
|
||||
if(SPIFFS.begin()){
|
||||
pixelConfig.fromFile("/pixelConfig.json");
|
||||
// FIXME actualy store and load state to use as initial animation
|
||||
defaultState.fromFile("/pixelState.json");
|
||||
state = defaultState;
|
||||
}
|
||||
@@ -67,72 +56,32 @@ class IlluCat : public Sprocket {
|
||||
// initialize services
|
||||
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
||||
server = new AsyncWebServer(80);
|
||||
ws = new AsyncWebSocket("/pixel");
|
||||
//wsStream = new AsyncWebSocket("/stream");
|
||||
defaultAnimation();
|
||||
|
||||
// add plugins
|
||||
// TODO how can any type of API be linked to another one?
|
||||
// TODO add HTTP OTA instead of TCP
|
||||
//addPlugin(new OtaTcpPlugin(otaConfig));
|
||||
addPlugin(new WebServerPlugin(webConfig, server));
|
||||
addPlugin(new WebConfigPlugin(server));
|
||||
addPlugin(new WebApi(server, 1));
|
||||
// TODO pixel streaming
|
||||
addPlugin(new PixelPlugin(pixelConfig, pixels));
|
||||
|
||||
|
||||
// FIXME move to networking
|
||||
//String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString();
|
||||
//PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str());
|
||||
|
||||
// TODO move to plugin
|
||||
// setup web stuff
|
||||
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
|
||||
server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
|
||||
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
server->addHandler(ws);
|
||||
//wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6));
|
||||
//server->addHandler(wsStream);
|
||||
|
||||
return Sprocket::activate(scheduler, network);
|
||||
} using Sprocket::activate;
|
||||
|
||||
// TODO move to utils
|
||||
String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){
|
||||
if(request->hasParam(param, isPost)) {
|
||||
return request->getParam(param, isPost)->value();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
|
||||
void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
if(type == WS_EVT_DATA){
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, WsUtils::parseFrameAsString(type, arg, data, len, 0).c_str());
|
||||
pixels->ActivePattern = NONE;
|
||||
pixels->handleStream(data, len);
|
||||
}
|
||||
}
|
||||
|
||||
void patternWebRequestHandler(AsyncWebServerRequest *request) {
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api");
|
||||
currentMessage.topic = getRequestParameterOrDefault(request, "topic", "");
|
||||
currentMessage.payload = getRequestParameterOrDefault(request, "payload", "");
|
||||
currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||
String msg = currentMessage.toJsonString();
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
if(currentMessage.broadcast){
|
||||
network.broadcast(msg);
|
||||
}
|
||||
request->send(200, "text/plain", msg);
|
||||
}
|
||||
|
||||
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
if(type == WS_EVT_DATA){
|
||||
String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
dispatch(0, frame);
|
||||
network.broadcast(frame);
|
||||
}
|
||||
virtual void scanningAnimation() {
|
||||
pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
|
||||
}
|
||||
virtual void defaultAnimation() {
|
||||
pixels->RainbowCycle(pixelConfig.updateInterval);
|
||||
}
|
||||
|
||||
// TODO move to Sprocket
|
||||
virtual void dispatch( uint32_t from, String &msg ) {
|
||||
currentMessage.fromJsonString(msg);
|
||||
if(currentMessage.valid){
|
||||
@@ -141,10 +90,6 @@ class IlluCat : public Sprocket {
|
||||
}
|
||||
}
|
||||
|
||||
void loop(){
|
||||
Sprocket::loop();
|
||||
yield();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
74
src/WebApi.cpp
Normal file
74
src/WebApi.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef __WEBAPI_PLUGIN__
|
||||
#define __WEBAPI_PLUGIN__
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <Sprocket.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "utils_print.h"
|
||||
#include "utils_web.h"
|
||||
#include <plugins/WebServerPlugin.cpp>
|
||||
#include <plugins/WebConfigPlugin.cpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
// TODO headerfile
|
||||
|
||||
class WebApi : public Plugin {
|
||||
private:
|
||||
Network* network;
|
||||
public:
|
||||
AsyncWebServer* server;
|
||||
AsyncWebSocket* ws;
|
||||
SprocketMessage currentMessage;
|
||||
|
||||
int broadcast;
|
||||
|
||||
WebApi(AsyncWebServer* _server, int _broadcast = 0){
|
||||
server = _server;
|
||||
broadcast = _broadcast;
|
||||
}
|
||||
|
||||
void activate(Scheduler* _scheduler, Network* _network) {
|
||||
network = _network;
|
||||
ws = new AsyncWebSocket("/ws"); // FIXME constant /ws
|
||||
ws->onEvent(bind(&WebApi::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||
server->addHandler(ws);
|
||||
server->on("/api", HTTP_POST, bind(&WebApi::postRequestHandler, this, _1));
|
||||
}
|
||||
|
||||
void postRequestHandler(AsyncWebServerRequest *request) {
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, "POST WebApi");
|
||||
currentMessage.topic = WebUtils::getRequestParameterOrDefault(request, "topic", "");
|
||||
currentMessage.payload = WebUtils::getRequestParameterOrDefault(request, "payload", "");
|
||||
currentMessage.broadcast = atoi(WebUtils::getRequestParameterOrDefault(request, "broadcast", "0").c_str());
|
||||
String msg = currentMessage.toJsonString();
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
if(currentMessage.broadcast){
|
||||
network->broadcast(msg);
|
||||
}
|
||||
request->send(200, "text/plain", msg);
|
||||
}
|
||||
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
// FIXME to limitted
|
||||
if(type == WS_EVT_DATA){
|
||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
dispatch(0, frame);
|
||||
}
|
||||
}
|
||||
|
||||
void dispatch( uint32_t from, String &msg ) {
|
||||
currentMessage.fromJsonString(msg);
|
||||
if(currentMessage.valid){
|
||||
currentMessage.from = from;
|
||||
publish(currentMessage.topic, currentMessage.payload);
|
||||
if(broadcast){
|
||||
network->broadcast(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "NeoPattern_api_json.h"
|
||||
#include "NeoPattern_api_modes.cpp"
|
||||
#include "utils_print.h"
|
||||
#include "utils_ws.h"
|
||||
#include "utils_web.h"
|
||||
#include <plugins/WebSO.h>
|
||||
#include <plugins/OtaTcpPlugin.cpp>
|
||||
#include <plugins/WebServerPlugin.cpp>
|
||||
@@ -74,7 +74,7 @@ class WebCat : public IlluCat {
|
||||
|
||||
void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
if(type == WS_EVT_DATA){
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, WsUtils::parseFrameAsString(type, arg, data, len, 0).c_str());
|
||||
PRINT_MSG(Serial, SPROCKET_TYPE, WebUtils::parseFrameAsString(type, arg, data, len, 0).c_str());
|
||||
pixels->ActivePattern = NONE;
|
||||
pixels->handleStream(data, len);
|
||||
}
|
||||
@@ -95,7 +95,7 @@ class WebCat : public IlluCat {
|
||||
|
||||
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||
if(type == WS_EVT_DATA){
|
||||
String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
String frame = WebUtils::parseFrameAsString(type, arg, data, len, 0);
|
||||
dispatch(0, frame);
|
||||
network.broadcast(frame);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user