uncouple generic web stuff into api plugin

This commit is contained in:
2018-10-28 03:21:26 +01:00
parent a8d5cae284
commit 7fa3683ca6
8 changed files with 237 additions and 85 deletions

View File

@@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>ESP Kit</title> <title>IlluCat</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="/favicon-32x32.png"> <link rel="icon" type="image/png" href="/favicon-32x32.png">

View File

@@ -10784,7 +10784,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
let endpoint = '/pixel'; let endpoint = '/ws';
// gradients // gradients
// https://uigradients.com // https://uigradients.com

View File

@@ -11,11 +11,12 @@
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0]) #define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
struct NeoPixelConfig : public JsonStruct { struct NeoPixelConfig : public JsonStruct {
int pin; // FIXME constants!
int length; int pin = 4;
int brightness; int length = 8;
int updateInterval; int brightness = 100;
int defaultColor; int updateInterval = 100;
int defaultColor = 100; // FIXME remove unused
void mapJsonObject(JsonObject& root) { void mapJsonObject(JsonObject& root) {
root["pin"] = pin; root["pin"] = pin;
root["length"] = length; root["length"] = length;
@@ -24,11 +25,11 @@ struct NeoPixelConfig : public JsonStruct {
root["defaultColor"] = defaultColor; root["defaultColor"] = defaultColor;
} }
void fromJsonObject(JsonObject& json) { void fromJsonObject(JsonObject& json) {
pin = getIntAttrFromJson(json, "pin"); pin = getIntAttrFromJson(json, "pin", pin);
length = getIntAttrFromJson(json, "length"); length = getIntAttrFromJson(json, "length", length);
brightness = getIntAttrFromJson(json, "brightness"); brightness = getIntAttrFromJson(json, "brightness", brightness);
updateInterval = getIntAttrFromJson(json, "updateInterval"); updateInterval = getIntAttrFromJson(json, "updateInterval", updateInterval);
defaultColor = getIntAttrFromJson(json, "defaultColor"); defaultColor = getIntAttrFromJson(json, "defaultColor", defaultColor);
} }
}; };

View File

@@ -1,13 +1,19 @@
#ifndef __WSUTILS_H___ #ifndef __WebUtils_H___
#define __WSUTILS_H___ #define __WebUtils_H___
#include <Arduino.h> #include <Arduino.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <AsyncWebSocket.h> #include <AsyncWebSocket.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
class WsUtils { class WebUtils {
public: public:
static String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){
if(request->hasParam(param, isPost)) {
return request->getParam(param, isPost)->value();
}
return defaultValue;
}
static String parseFrame(AwsEventType type, void * arg, uint8_t *data, size_t len) { static String parseFrame(AwsEventType type, void * arg, uint8_t *data, size_t len) {
String msg = ""; String msg = "";
if(type == WS_EVT_DATA){ if(type == WS_EVT_DATA){

126
src/IlluCat.cpp_ Normal file
View 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();
} */

View File

@@ -11,11 +11,12 @@
#include "NeoPattern_api_json.h" #include "NeoPattern_api_json.h"
#include "NeoPattern_api_modes.cpp" #include "NeoPattern_api_modes.cpp"
#include "utils_print.h" #include "utils_print.h"
#include "utils_ws.h" #include "utils_web.h"
#include <plugins/WebSO.h> #include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp> #include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp> #include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp> #include <plugins/WebConfigPlugin.cpp>
#include "WebApi.cpp"
#include "PixelPlugin.h" #include "PixelPlugin.h"
using namespace std; using namespace std;
@@ -28,8 +29,6 @@ class IlluCat : public Sprocket {
NeoPatternDto defaultState; NeoPatternDto defaultState;
NeoPatternDto state; NeoPatternDto state;
AsyncWebServer* server; AsyncWebServer* server;
AsyncWebSocket* ws;
//AsyncWebSocket* wsStream;
NeoPixelConfig pixelConfig; NeoPixelConfig pixelConfig;
SprocketConfig sprocketConfig; SprocketConfig sprocketConfig;
@@ -42,24 +41,14 @@ class IlluCat : public Sprocket {
sprocketConfig = cfg; sprocketConfig = cfg;
otaConfig = otaCfg; otaConfig = otaCfg;
webConfig = webCfg; 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) { Sprocket* activate(Scheduler* scheduler, Network* network) {
// load config files from SPIFFS // load config files from SPIFFS
if(SPIFFS.begin()){ if(SPIFFS.begin()){
pixelConfig.fromFile("/pixelConfig.json"); pixelConfig.fromFile("/pixelConfig.json");
// FIXME actualy store and load state to use as initial animation
defaultState.fromFile("/pixelState.json"); defaultState.fromFile("/pixelState.json");
state = defaultState; state = defaultState;
} }
@@ -67,72 +56,32 @@ class IlluCat : public Sprocket {
// initialize services // initialize services
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
server = new AsyncWebServer(80); server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/pixel");
//wsStream = new AsyncWebSocket("/stream");
defaultAnimation(); defaultAnimation();
// add plugins // add plugins
// TODO how can any type of API be linked to another one?
// TODO add HTTP OTA instead of TCP // TODO add HTTP OTA instead of TCP
//addPlugin(new OtaTcpPlugin(otaConfig)); //addPlugin(new OtaTcpPlugin(otaConfig));
addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebServerPlugin(webConfig, server));
addPlugin(new WebConfigPlugin(server)); addPlugin(new WebConfigPlugin(server));
addPlugin(new WebApi(server, 1));
// TODO pixel streaming
addPlugin(new PixelPlugin(pixelConfig, pixels)); 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 // setup web stuff
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); 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); return Sprocket::activate(scheduler, network);
} using Sprocket::activate; } using Sprocket::activate;
// TODO move to utils virtual void scanningAnimation() {
String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval);
if(request->hasParam(param, isPost)) { }
return request->getParam(param, isPost)->value(); virtual void defaultAnimation() {
} pixels->RainbowCycle(pixelConfig.updateInterval);
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);
}
} }
// TODO move to Sprocket
virtual void dispatch( uint32_t from, String &msg ) { virtual void dispatch( uint32_t from, String &msg ) {
currentMessage.fromJsonString(msg); currentMessage.fromJsonString(msg);
if(currentMessage.valid){ if(currentMessage.valid){
@@ -141,10 +90,6 @@ class IlluCat : public Sprocket {
} }
} }
void loop(){
Sprocket::loop();
yield();
}
}; };
#endif #endif

74
src/WebApi.cpp Normal file
View 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

View File

@@ -11,7 +11,7 @@
#include "NeoPattern_api_json.h" #include "NeoPattern_api_json.h"
#include "NeoPattern_api_modes.cpp" #include "NeoPattern_api_modes.cpp"
#include "utils_print.h" #include "utils_print.h"
#include "utils_ws.h" #include "utils_web.h"
#include <plugins/WebSO.h> #include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp> #include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.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) { void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if(type == WS_EVT_DATA){ 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->ActivePattern = NONE;
pixels->handleStream(data, len); 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) { virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if(type == WS_EVT_DATA){ 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); dispatch(0, frame);
network.broadcast(frame); network.broadcast(frame);
} }