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.

183 lines
5.0 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. }
  89. if (code == NOFUSS_FILESYSTEM_UPDATE_ERROR) {
  90. DEBUG_MSG_P(PSTR("[NoFUSS] File System Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  91. }
  92. if (code == NOFUSS_FILESYSTEM_UPDATED) {
  93. DEBUG_MSG_P(PSTR("[NoFUSS] File System Updated\n"));
  94. }
  95. if (code == NOFUSS_FIRMWARE_UPDATE_ERROR) {
  96. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  97. }
  98. if (code == NOFUSS_FIRMWARE_UPDATED) {
  99. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Updated\n"));
  100. }
  101. if (code == NOFUSS_RESET) {
  102. DEBUG_MSG_P(PSTR("[NoFUSS] Resetting board\n"));
  103. #if WEB_SUPPORT
  104. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  105. #endif
  106. nice_delay(100);
  107. }
  108. if (code == NOFUSS_END) {
  109. DEBUG_MSG_P(PSTR("[NoFUSS] End\n"));
  110. eepromRotate(true);
  111. }
  112. });
  113. #if WEB_SUPPORT
  114. wsOnSendRegister(_nofussWebSocketOnSend);
  115. wsOnReceiveRegister(_nofussWebSocketOnReceive);
  116. #endif
  117. #if TERMINAL_SUPPORT
  118. _nofussInitCommands();
  119. #endif
  120. // Main callbacks
  121. espurnaRegisterLoop(nofussLoop);
  122. espurnaRegisterReload(_nofussConfigure);
  123. }
  124. void nofussLoop() {
  125. if (!_nofussEnabled) return;
  126. if (!wifiConnected()) return;
  127. if ((_nofussLastCheck > 0) && ((millis() - _nofussLastCheck) < _nofussInterval)) return;
  128. nofussRun();
  129. }
  130. #endif // NOFUSS_SUPPORT