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.

134 lines
3.8 KiB

  1. /*
  2. NOFUSS MODULE
  3. Copyright (C) 2016-2017 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. void nofussRun() {
  14. NoFUSSClient.handle();
  15. _nofussLastCheck = millis();
  16. }
  17. void nofussConfigure() {
  18. String nofussServer = getSetting("nofussServer", NOFUSS_SERVER);
  19. if (nofussServer.length() == 0) {
  20. setSetting("nofussEnabled", 0);
  21. _nofussEnabled = false;
  22. } else {
  23. _nofussEnabled = getSetting("nofussEnabled", NOFUSS_ENABLED).toInt() == 1;
  24. }
  25. _nofussInterval = getSetting("nofussInterval", NOFUSS_INTERVAL).toInt();
  26. _nofussLastCheck = 0;
  27. if (!_nofussEnabled) {
  28. DEBUG_MSG_P(PSTR("[NOFUSS] Disabled\n"));
  29. } else {
  30. char buffer[20];
  31. snprintf_P(buffer, sizeof(buffer), PSTR("%s-%s"), APP_NAME, DEVICE);
  32. NoFUSSClient.setServer(nofussServer);
  33. NoFUSSClient.setDevice(buffer);
  34. NoFUSSClient.setVersion(APP_VERSION);
  35. DEBUG_MSG_P(PSTR("[NOFUSS] Server : %s\n"), nofussServer.c_str());
  36. DEBUG_MSG_P(PSTR("[NOFUSS] Dervice: %s\n"), buffer);
  37. DEBUG_MSG_P(PSTR("[NOFUSS] Version: %s\n"), APP_VERSION);
  38. DEBUG_MSG_P(PSTR("[NOFUSS] Enabled\n"));
  39. }
  40. }
  41. void nofussSetup() {
  42. nofussConfigure();
  43. NoFUSSClient.onMessage([](nofuss_t code) {
  44. if (code == NOFUSS_START) {
  45. DEBUG_MSG_P(PSTR("[NoFUSS] Start\n"));
  46. }
  47. if (code == NOFUSS_UPTODATE) {
  48. DEBUG_MSG_P(PSTR("[NoFUSS] Already in the last version\n"));
  49. }
  50. if (code == NOFUSS_NO_RESPONSE_ERROR) {
  51. DEBUG_MSG_P(PSTR("[NoFUSS] Wrong server response: %d %s\n"), NoFUSSClient.getErrorNumber(), (char *) NoFUSSClient.getErrorString().c_str());
  52. }
  53. if (code == NOFUSS_PARSE_ERROR) {
  54. DEBUG_MSG_P(PSTR("[NoFUSS] Error parsing server response\n"));
  55. }
  56. if (code == NOFUSS_UPDATING) {
  57. DEBUG_MSG_P(PSTR("[NoFUSS] Updating\n"));
  58. DEBUG_MSG_P(PSTR(" New version: %s\n"), (char *) NoFUSSClient.getNewVersion().c_str());
  59. DEBUG_MSG_P(PSTR(" Firmware: %s\n"), (char *) NoFUSSClient.getNewFirmware().c_str());
  60. DEBUG_MSG_P(PSTR(" File System: %s\n"), (char *) NoFUSSClient.getNewFileSystem().c_str());
  61. #if WEB_SUPPORT
  62. wsSend_P(PSTR("{\"message\": \"Remote update started\"}"));
  63. #endif
  64. }
  65. if (code == NOFUSS_FILESYSTEM_UPDATE_ERROR) {
  66. DEBUG_MSG_P(PSTR("[NoFUSS] File System Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  67. }
  68. if (code == NOFUSS_FILESYSTEM_UPDATED) {
  69. DEBUG_MSG_P(PSTR("[NoFUSS] File System Updated\n"));
  70. }
  71. if (code == NOFUSS_FIRMWARE_UPDATE_ERROR) {
  72. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  73. }
  74. if (code == NOFUSS_FIRMWARE_UPDATED) {
  75. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Updated\n"));
  76. }
  77. if (code == NOFUSS_RESET) {
  78. DEBUG_MSG_P(PSTR("[NoFUSS] Resetting board\n"));
  79. #if WEB_SUPPORT
  80. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  81. #endif
  82. delay(100);
  83. }
  84. if (code == NOFUSS_END) {
  85. DEBUG_MSG_P(PSTR("[NoFUSS] End\n"));
  86. }
  87. });
  88. }
  89. void nofussLoop() {
  90. if (!_nofussEnabled) return;
  91. if (!wifiConnected()) return;
  92. if ((_nofussLastCheck > 0) && ((millis() - _nofussLastCheck) < _nofussInterval)) return;
  93. nofussRun();
  94. }
  95. #endif // NOFUSS_SUPPORT