#ifndef __MESH_APP__ #define __MESH_APP__ #include #include #include #include #include "config.h" #include "NeoPattern.cpp" #include "NeoPatternDto.h" #include "NeoPattern_api_json.h" #include "NeoPattern_api_modes.cpp" #include "utils_print.h" #include "utils_ws.h" #include #include #include #include #include "PixelPlugin.h" using namespace std; using namespace std::placeholders; const byte DNS_PORT = 53; class IlluCat : public MeshSprocket { public: NeoPattern* pixels; NeoPatternDto defaultState; NeoPatternDto state; AsyncWebServer* server; AsyncWebSocket* ws; DNSServer* dnsServer; NeoPixelConfig pixelConfig; SprocketConfig sprocketConfig; OtaConfig otaConfig; WebServerConfig webConfig; SprocketMessage currentMessage; IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : MeshSprocket(cfg) { //pixelConfig = pixelCfg; 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() { String defaultStr = String(defaultState.value); PIXEL_FNCS[defaultState.mode](pixels, defaultStr.c_str()); } Sprocket* activate(Scheduler* scheduler, Network* network) { net = static_cast(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"); dnsServer = new DNSServer(); // add plugins addPlugin(new OtaTcpPlugin(otaConfig)); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); addPlugin(new PixelPlugin(pixelConfig, pixels)); defaultAnimation(); // configure DNS // plugin? dnsServer->setErrorReplyCode(DNSReplyCode::NoError); dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); 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); return MeshSprocket::activate(scheduler, network); } using MeshSprocket::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 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){ net->mesh.sendBroadcast(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); } } virtual void dispatch( uint32_t from, String &msg ) { currentMessage.fromJsonString(msg); if(currentMessage.valid){ currentMessage.from = from; publish(currentMessage.topic, currentMessage.payload); if(currentMessage.broadcast){ net->mesh.sendBroadcast(msg); } } } void loop(){ MeshSprocket::loop(); dnsServer->processNextRequest(); } }; #endif