EmbAJAX
Simplistic framework for creating and handling displays and controls on a web page served by an embeddable device (Arduino or other microcontroller with Arduino support).
EmbAJAXOutputDriverESPAsync.h
1 
21 #ifndef EMBAJAXOUTPUTDRIVERESPASYNC_H
22 #define EMBAJAXOUTPUTDRIVERESPASYNC_H
23 
24 #if defined (EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION)
25 #error Duplicate definition of output driver. Fix your include-directives.
26 #endif
27 #define EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION
28 
29 // For EmbAJAXPage. Important to include after defining EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION
30 #include "EmbAJAX.h"
31 
32 #include <ESPAsyncWebServer.h>
33 
34 #define EmbAJAXOutputDriverWebServerClass AsyncWebServer
35 
41 public:
44  EmbAJAXOutputDriverESPAsync(EmbAJAXOutputDriverWebServerClass *server) {
46  _server = server;
47  _request = 0;
48  }
49  void printHeader(bool html) override {
50  _response = _request->beginResponseStream(html ? "text/html" : "text/json");
51  }
52  void printContent(const char *content) override {
53  _response->print(content);
54  }
55  const char* getArg(const char* name, char* buf, int buflen) override {
56  _request->arg(name).toCharArray (buf, buflen);
57  return buf;
58  }
59  void installPage(EmbAJAXPageBase *page, const char *path, void (*change_callback)()=0) override {
60  _server->on(path, [=](AsyncWebServerRequest* request) {
61  _request = request;
62  _response = 0;
63  if (_request->method() == HTTP_POST) { // AJAX request
64  page->handleRequest(change_callback);
65  } else { // Page load
66  page->printPage();
67  }
68  _request->send(_response);
69  _request = 0;
70  });
71  }
72  void loopHook() override {};
73 private:
74  EmbAJAXOutputDriverWebServerClass *_server;
75  AsyncWebServerRequest *_request;
76  AsyncResponseStream *_response;
77 };
78 
80 
81 #endif
static void setDriver(EmbAJAXOutputDriverBase *driver)
Definition: EmbAJAX.h:77
Abstract base class for output drivers/server implementations.
Definition: EmbAJAX.h:147
Output driver implementation. This implementation works with ESPAsyncWebServer (https://github....
Definition: EmbAJAXOutputDriverESPAsync.h:40
EmbAJAXOutputDriverESPAsync(AsyncWebServer *server)
Definition: EmbAJAXOutputDriverESPAsync.h:44
void loopHook() override
Definition: EmbAJAXOutputDriverESPAsync.h:72
void installPage(EmbAJAXPageBase *page, const char *path, void(*change_callback)()=0) override
Definition: EmbAJAXOutputDriverESPAsync.h:59
Absrract internal helper class.
Definition: EmbAJAX.h:709