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.

79 lines
1.9 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
  1. /*
  2. Part of the API MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. */
  6. #include "espurna.h"
  7. #include "api.h"
  8. #include "ws.h"
  9. #include "web.h"
  10. // -----------------------------------------------------------------------------
  11. #if WEB_SUPPORT
  12. namespace {
  13. bool _apiWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  14. return (strncmp(key, "api", 3) == 0);
  15. }
  16. void _apiWebSocketOnConnected(JsonObject& root) {
  17. root["apiEnabled"] = apiEnabled();
  18. root["apiKey"] = apiKey();
  19. root["apiRestFul"] = apiRestFul();
  20. root["apiRealTime"] = getSetting("apiRealTime", 1 == API_REAL_TIME_VALUES);
  21. }
  22. }
  23. // -----------------------------------------------------------------------------
  24. // Public API
  25. // -----------------------------------------------------------------------------
  26. bool apiEnabled() {
  27. return getSetting("apiEnabled", 1 == API_ENABLED);
  28. }
  29. bool apiRestFul() {
  30. return getSetting("apiRestFul", 1 == API_RESTFUL);
  31. }
  32. String apiKey() {
  33. return getSetting("apiKey", API_KEY);
  34. }
  35. bool apiAuthenticate(AsyncWebServerRequest *request) {
  36. const auto key = apiKey();
  37. if (!apiEnabled() || !key.length()) {
  38. DEBUG_MSG_P(PSTR("[WEBSERVER] HTTP API is not enabled\n"));
  39. request->send(403);
  40. return false;
  41. }
  42. AsyncWebParameter* keyParam = request->getParam("apikey", (request->method() == HTTP_PUT));
  43. if (!keyParam || !keyParam->value().equals(key)) {
  44. DEBUG_MSG_P(PSTR("[WEBSERVER] Wrong / missing apikey parameter\n"));
  45. request->send(403);
  46. return false;
  47. }
  48. return true;
  49. }
  50. void apiCommonSetup() {
  51. wsRegister()
  52. .onVisible([](JsonObject& root) { root["apiVisible"] = 1; })
  53. .onConnected(_apiWebSocketOnConnected)
  54. .onKeyCheck(_apiWebSocketOnKeyCheck);
  55. }
  56. #endif // WEB_SUPPORT == 1