mirror of
https://gitlab.com/zwirbel/illucat.git
synced 2025-12-15 17:58:20 +01:00
148 lines
5.3 KiB
C++
148 lines
5.3 KiB
C++
#ifndef __MESH_APP__
|
|
#define __MESH_APP__
|
|
|
|
#include <painlessMesh.h>
|
|
#include <base/MeshSprocket.h>
|
|
#include <MeshNet.h>
|
|
#include <DNSServer.h>
|
|
|
|
|
|
#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 <plugins/WebSO.h>
|
|
#include <plugins/OtaTcpPlugin.cpp>
|
|
#include <plugins/WebServerPlugin.cpp>
|
|
#include <plugins/WebConfigPlugin.cpp>
|
|
#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;
|
|
|
|
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);
|
|
//pixels->Fade(0, pixels->Color(255,255,255), 4, pixelConfig.updateInterval, FORWARD);
|
|
}
|
|
virtual void defaultAnimation() {
|
|
String defaultStr = String(defaultState.value);
|
|
PIXEL_FNCS[defaultState.mode](pixels, defaultStr.c_str());
|
|
//pixels->RainbowCycle(150);
|
|
}
|
|
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
|
|
|
net = static_cast<MeshNet*>(network);
|
|
net->mesh.onNewConnection(bind(&IlluCat::onNewConnection,this, _1));
|
|
net->mesh.onChangedConnections(bind(&IlluCat::onConnectionChanged,this));
|
|
|
|
if(SPIFFS.begin()){
|
|
pixelConfig.fromFile("/pixelConfig.json");
|
|
defaultState.fromFile("/pixelState.json");
|
|
state = defaultState;
|
|
}
|
|
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
|
|
server = new AsyncWebServer(80);
|
|
ws = new AsyncWebSocket("/pixel");
|
|
dnsServer = new DNSServer();
|
|
|
|
addPlugin(new OtaTcpPlugin(otaConfig));
|
|
addPlugin(new WebServerPlugin(webConfig, server));
|
|
addPlugin(new WebConfigPlugin(server));
|
|
addPlugin(new PixelPlugin(pixelConfig, pixels));
|
|
|
|
|
|
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
|
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
|
|
|
|
//scanningAnimation();
|
|
defaultAnimation();
|
|
|
|
// setup web stuff
|
|
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
|
|
server->on("/pixel/pattern", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
|
|
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
|
server->addHandler(ws);
|
|
|
|
// FIXME OnDisable is triggered after last scan, aprx. 10 sec
|
|
net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this));
|
|
return MeshSprocket::activate(scheduler, network);
|
|
} using MeshSprocket::activate;
|
|
|
|
void patternWebRequestHandler(AsyncWebServerRequest *request) {
|
|
Serial.println("POST /pixel/pattern");
|
|
if(request->hasParam("state", true)) {
|
|
String inStr = request->getParam("state", true)->value();
|
|
onMessage(0, inStr);
|
|
}
|
|
request->send(200, "text/plain", "OK");
|
|
}
|
|
|
|
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);
|
|
onMessage(0, frame);
|
|
net->mesh.sendBroadcast(frame);
|
|
client->text(String(millis()));
|
|
}
|
|
}
|
|
|
|
virtual void onMessage( uint32_t from, String &msg ) {
|
|
PRINT_MSG(Serial, SPROCKET_TYPE, "msg from %u = %s\n", from, msg.c_str());
|
|
state.fromJsonString(msg);
|
|
PIXEL_FNCS[state.mode](pixels, state.valueStr);
|
|
}
|
|
|
|
virtual void onNewConnection(uint32_t nodeId){
|
|
PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId);
|
|
//defaultAnimation();
|
|
String stateJson = state.toJsonString();
|
|
net->mesh.sendSingle(nodeId, stateJson);
|
|
// TODO publish current state to pixel/color on new node
|
|
}
|
|
virtual void onConnectionChanged(){
|
|
PRINT_MSG(Serial, SPROCKET_TYPE, "connection changed");
|
|
if(!net->mesh.getNodeList().size()){
|
|
defaultAnimation();
|
|
}
|
|
}
|
|
|
|
void loop(){
|
|
MeshSprocket::loop();
|
|
dnsServer->processNextRequest();
|
|
}
|
|
};
|
|
|
|
#endif |