Files
sprocket-device-illucat/src/IlluCat.h

166 lines
6.1 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;
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;
}
// TDOO remove
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));
// TODO plugin?
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
Serial.println("SoftAP IP: " + WiFi.softAPIP().toString());
defaultAnimation();
// TODO plugin
// setup web stuff
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
server->on("/pixel/state", 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
// FIXME chck if this is also triggered when new connection arrives
//net->mesh.stationScan.task.setOnDisable(bind(&IlluCat::defaultAnimation,this));
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) {
Serial.println("POST /pixel/state");
currentMessage.topic = getRequestParameterOrDefault(request, "topic", "");
currentMessage.payload = getRequestParameterOrDefault(request, "payload", "");
String msg = currentMessage.toJsonString();
net->mesh.sendBroadcast(msg);
publish(currentMessage.topic, currentMessage.payload);
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);
net->mesh.sendBroadcast(frame);
dispatch(0, frame);
client->text(String(millis()));
}
}
virtual void dispatch( uint32_t from, String &msg ) {
//Serial.println(msg);
currentMessage.fromJsonString(msg);
if(currentMessage.valid){
currentMessage.from = from;
publish(currentMessage.topic, currentMessage.payload);
}
}
virtual void onNewConnection(uint32_t nodeId){
PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId);
// publish current state to new node
// TODO broadcast enable/disable flag
//String stateJson = currentMessage.toJsonString();
//net->mesh.sendSingle(nodeId, stateJson);
}
//virtual void onConnectionChanged(){
// PRINT_MSG(Serial, SPROCKET_TYPE, "connection changed");
// //if(!net->mesh.getNodeList().size()){
// // defaultAnimation();
// //}
//}
void loop(){
MeshSprocket::loop();
dnsServer->processNextRequest();
}
};
#endif