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.

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