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.

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