Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
  1. /*
  2. WEBSERVER MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include "espurna.h"
  7. #if WEB_SUPPORT
  8. #include <functional>
  9. #include <list>
  10. #include <vector>
  11. #include <ESPAsyncWebServer.h>
  12. struct AsyncWebPrintConfig {
  13. const char* const mimeType;
  14. const size_t backlogCountMax;
  15. const size_t backlogSizeMax;
  16. const decltype(millis()) backlogTimeout;
  17. };
  18. struct AsyncWebPrint : public Print {
  19. enum class State {
  20. None,
  21. Sending,
  22. Done,
  23. Error
  24. };
  25. using BufferType = std::vector<uint8_t>;
  26. // To be able to safely output data right from the request callback,
  27. // we schedule a 'printer' task that will print into the request response buffer via AsyncChunkedResponse
  28. // Note: implementation must be included in the header
  29. template<typename CallbackType>
  30. static void scheduleFromRequest(const AsyncWebPrintConfig& config, AsyncWebServerRequest*, CallbackType);
  31. template<typename CallbackType>
  32. static void scheduleFromRequest(AsyncWebServerRequest*, CallbackType);
  33. State getState();
  34. void setState(State);
  35. // note: existing implementation only expects this to be available via AsyncWebPrint
  36. #if defined(ARDUINO_ESP8266_RELEASE_2_3_0)
  37. void flush();
  38. #else
  39. void flush() final override;
  40. #endif
  41. size_t write(uint8_t) final override;
  42. size_t write(const uint8_t *buffer, size_t size) final override;
  43. const char* const mimeType;
  44. const size_t backlogCountMax;
  45. const size_t backlogSizeMax;
  46. const decltype(millis()) backlogTimeout;
  47. protected:
  48. std::list<BufferType> _buffers;
  49. AsyncWebServerRequest* const _request;
  50. State _state;
  51. AsyncWebPrint(const AsyncWebPrintConfig&, AsyncWebServerRequest* req);
  52. bool _addBuffer();
  53. bool _exhaustBuffers();
  54. void _prepareRequest();
  55. };
  56. using web_body_callback_f = std::function<bool(AsyncWebServerRequest*, uint8_t* data, size_t len, size_t index, size_t total)>;
  57. using web_request_callback_f = std::function<bool(AsyncWebServerRequest*)>;
  58. AsyncWebServer* webServer();
  59. bool webAuthenticate(AsyncWebServerRequest *request);
  60. void webLog(AsyncWebServerRequest *request);
  61. void webBodyRegister(web_body_callback_f);
  62. void webRequestRegister(web_request_callback_f);
  63. uint16_t webPort();
  64. void webSetup();
  65. #endif // WEB_SUPPORT == 1