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.

110 lines
2.9 KiB

6 years ago
6 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
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. LightFox module
  3. Copyright (C) 2019 by Andrey F. Kupreychik <foxle@quickfox.ru>
  4. */
  5. #include "lightfox.h"
  6. #ifdef FOXEL_LIGHTFOX_DUAL
  7. // -----------------------------------------------------------------------------
  8. // DEFINITIONS
  9. // -----------------------------------------------------------------------------
  10. #define LIGHTFOX_CODE_START 0xA0
  11. #define LIGHTFOX_CODE_LEARN 0xF1
  12. #define LIGHTFOX_CODE_CLEAR 0xF2
  13. #define LIGHTFOX_CODE_STOP 0xA1
  14. // -----------------------------------------------------------------------------
  15. // PUBLIC
  16. // -----------------------------------------------------------------------------
  17. void lightfoxLearn() {
  18. Serial.write(LIGHTFOX_CODE_START);
  19. Serial.write(LIGHTFOX_CODE_LEARN);
  20. Serial.write(0x00);
  21. Serial.write(LIGHTFOX_CODE_STOP);
  22. Serial.println();
  23. Serial.flush();
  24. DEBUG_MSG_P(PSTR("[LIGHTFOX] Learn comman sent\n"));
  25. }
  26. void lightfoxClear() {
  27. Serial.write(LIGHTFOX_CODE_START);
  28. Serial.write(LIGHTFOX_CODE_CLEAR);
  29. Serial.write(0x00);
  30. Serial.write(LIGHTFOX_CODE_STOP);
  31. Serial.println();
  32. Serial.flush();
  33. DEBUG_MSG_P(PSTR("[LIGHTFOX] Clear comman sent\n"));
  34. }
  35. // -----------------------------------------------------------------------------
  36. // WEB
  37. // -----------------------------------------------------------------------------
  38. #if WEB_SUPPORT
  39. void _lightfoxWebSocketOnConnected(JsonObject& root) {
  40. root["lightfoxVisible"] = 1;
  41. uint8_t buttonsCount = _buttons.size();
  42. root["lightfoxRelayCount"] = relayCount();
  43. JsonArray& rfb = root.createNestedArray("lightfoxButtons");
  44. for (byte id=0; id<buttonsCount; id++) {
  45. JsonObject& node = rfb.createNestedObject();
  46. node["id"] = id;
  47. node["relay"] = getSetting({"btnRelay", id}, 0);
  48. }
  49. }
  50. void _lightfoxWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  51. if (strcmp(action, "lightfoxLearn") == 0) lightfoxLearn();
  52. if (strcmp(action, "lightfoxClear") == 0) lightfoxClear();
  53. }
  54. #endif
  55. // -----------------------------------------------------------------------------
  56. // TERMINAL
  57. // -----------------------------------------------------------------------------
  58. #if TERMINAL_SUPPORT
  59. void _lightfoxInitCommands() {
  60. terminalRegisterCommand(F("LIGHTFOX.LEARN"), [](const terminal::CommandContext&) {
  61. lightfoxLearn();
  62. DEBUG_MSG_P(PSTR("+OK\n"));
  63. });
  64. terminalRegisterCommand(F("LIGHTFOX.CLEAR"), [](const terminal::CommandContext&) {
  65. lightfoxClear();
  66. DEBUG_MSG_P(PSTR("+OK\n"));
  67. });
  68. }
  69. #endif
  70. // -----------------------------------------------------------------------------
  71. // SETUP & LOOP
  72. // -----------------------------------------------------------------------------
  73. void lightfoxSetup() {
  74. #if WEB_SUPPORT
  75. wsRegister()
  76. .onConnected(_lightfoxWebSocketOnConnected)
  77. .onAction(_lightfoxWebSocketOnAction);
  78. #endif
  79. #if TERMINAL_SUPPORT
  80. _lightfoxInitCommands();
  81. #endif
  82. }
  83. #endif