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.

188 lines
5.3 KiB

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