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.

168 lines
4.5 KiB

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