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.

199 lines
5.5 KiB

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