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.

197 lines
5.4 KiB

3 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
6 years ago
  1. /*
  2. NOFUSS MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "nofuss.h"
  6. #if NOFUSS_SUPPORT
  7. #include "wifi.h"
  8. #include "mdns.h"
  9. #include "terminal.h"
  10. #include "ws.h"
  11. #include <NoFUSSClient.h>
  12. unsigned long _nofussLastCheck = 0;
  13. unsigned long _nofussInterval = 0;
  14. bool _nofussEnabled = false;
  15. // -----------------------------------------------------------------------------
  16. // NOFUSS
  17. // -----------------------------------------------------------------------------
  18. #if WEB_SUPPORT
  19. bool _nofussWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  20. return (strncmp(key, "nofuss", 6) == 0);
  21. }
  22. void _nofussWebSocketOnVisible(JsonObject& root) {
  23. root["nofussVisible"] = 1;
  24. }
  25. void _nofussWebSocketOnConnected(JsonObject& root) {
  26. root["nofussEnabled"] = getSetting("nofussEnabled", 1 == NOFUSS_ENABLED);
  27. root["nofussServer"] = getSetting("nofussServer", NOFUSS_SERVER);
  28. }
  29. #endif
  30. void _nofussConfigure() {
  31. String nofussServer = getSetting("nofussServer", NOFUSS_SERVER);
  32. if (!nofussServer.length()) {
  33. setSetting("nofussEnabled", 0);
  34. _nofussEnabled = false;
  35. } else {
  36. _nofussEnabled = getSetting("nofussEnabled", 1 == NOFUSS_ENABLED);
  37. }
  38. _nofussInterval = getSetting("nofussInterval", NOFUSS_INTERVAL);
  39. _nofussLastCheck = 0;
  40. if (!_nofussEnabled) {
  41. DEBUG_MSG_P(PSTR("[NOFUSS] Disabled\n"));
  42. } else {
  43. NoFUSSClient.setServer(nofussServer);
  44. NoFUSSClient.setDevice(APP_NAME "_" DEVICE);
  45. NoFUSSClient.setVersion(APP_VERSION);
  46. NoFUSSClient.setBuild(String(__UNIX_TIMESTAMP__));
  47. DEBUG_MSG_P(PSTR("[NOFUSS] Server : %s\n"), nofussServer.c_str());
  48. DEBUG_MSG_P(PSTR("[NOFUSS] Device: %s\n"), APP_NAME "_" DEVICE);
  49. DEBUG_MSG_P(PSTR("[NOFUSS] Version: %s\n"), APP_VERSION);
  50. DEBUG_MSG_P(PSTR("[NOFUSS] Build: %s\n"), String(__UNIX_TIMESTAMP__).c_str());
  51. DEBUG_MSG_P(PSTR("[NOFUSS] Enabled\n"));
  52. }
  53. }
  54. // -----------------------------------------------------------------------------
  55. void nofussRun() {
  56. NoFUSSClient.handle();
  57. _nofussLastCheck = millis();
  58. }
  59. void _nofussLoop() {
  60. if (!_nofussEnabled) return;
  61. if (!wifiConnected()) return;
  62. if ((_nofussLastCheck > 0) && ((millis() - _nofussLastCheck) < _nofussInterval)) return;
  63. nofussRun();
  64. }
  65. #if TERMINAL_SUPPORT
  66. void _nofussInitCommands() {
  67. terminalRegisterCommand(F("NOFUSS"), [](const terminal::CommandContext&) {
  68. terminalOK();
  69. nofussRun();
  70. });
  71. }
  72. #endif // TERMINAL_SUPPORT
  73. void nofussSetup() {
  74. _nofussConfigure();
  75. NoFUSSClient.onMessage([](nofuss_t code) {
  76. if (code == NOFUSS_START) {
  77. DEBUG_MSG_P(PSTR("[NoFUSS] Start\n"));
  78. }
  79. if (code == NOFUSS_UPTODATE) {
  80. DEBUG_MSG_P(PSTR("[NoFUSS] Already in the last version\n"));
  81. }
  82. if (code == NOFUSS_NO_RESPONSE_ERROR) {
  83. DEBUG_MSG_P(PSTR("[NoFUSS] Wrong server response: %d %s\n"), NoFUSSClient.getErrorNumber(), (char *) NoFUSSClient.getErrorString().c_str());
  84. }
  85. if (code == NOFUSS_PARSE_ERROR) {
  86. DEBUG_MSG_P(PSTR("[NoFUSS] Error parsing server response\n"));
  87. }
  88. if (code == NOFUSS_UPDATING) {
  89. DEBUG_MSG_P(PSTR("[NoFUSS] Updating\n"));
  90. DEBUG_MSG_P(PSTR(" New version: %s\n"), (char *) NoFUSSClient.getNewVersion().c_str());
  91. DEBUG_MSG_P(PSTR(" Firmware: %s\n"), (char *) NoFUSSClient.getNewFirmware().c_str());
  92. DEBUG_MSG_P(PSTR(" File System: %s\n"), (char *) NoFUSSClient.getNewFileSystem().c_str());
  93. #if WEB_SUPPORT
  94. wsSend_P(PSTR("{\"message\": \"Automatic OTA started.\"}"));
  95. #endif
  96. // Disabling EEPROM rotation to prevent writing to EEPROM after the upgrade
  97. eepromRotate(false);
  98. // Force backup right now, because NoFUSS library will immediatly reset on success
  99. eepromBackup(0);
  100. }
  101. if (code == NOFUSS_FILESYSTEM_UPDATE_ERROR) {
  102. DEBUG_MSG_P(PSTR("[NoFUSS] File System Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  103. }
  104. if (code == NOFUSS_FILESYSTEM_UPDATED) {
  105. DEBUG_MSG_P(PSTR("[NoFUSS] File System Updated\n"));
  106. }
  107. if (code == NOFUSS_FIRMWARE_UPDATE_ERROR) {
  108. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  109. }
  110. if (code == NOFUSS_FIRMWARE_UPDATED) {
  111. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Updated\n"));
  112. }
  113. if (code == NOFUSS_RESET) {
  114. DEBUG_MSG_P(PSTR("[NoFUSS] Resetting board\n"));
  115. #if WEB_SUPPORT
  116. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  117. #endif
  118. // TODO: NoFUSS will reset the board after this callback returns.
  119. // Maybe this should be optional
  120. customResetReason(CustomResetReason::Ota);
  121. nice_delay(100);
  122. }
  123. if (code == NOFUSS_END) {
  124. DEBUG_MSG_P(PSTR("[NoFUSS] End\n"));
  125. eepromRotate(true);
  126. }
  127. });
  128. #if WEB_SUPPORT
  129. wsRegister()
  130. .onVisible(_nofussWebSocketOnVisible)
  131. .onConnected(_nofussWebSocketOnConnected)
  132. .onKeyCheck(_nofussWebSocketOnKeyCheck);
  133. #endif
  134. #if TERMINAL_SUPPORT
  135. _nofussInitCommands();
  136. #endif
  137. // Main callbacks
  138. espurnaRegisterLoop(_nofussLoop);
  139. espurnaRegisterReload(_nofussConfigure);
  140. }
  141. #endif // NOFUSS_SUPPORT