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.

151 lines
4.3 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. // -----------------------------------------------------------------------------
  46. void nofussRun() {
  47. NoFUSSClient.handle();
  48. _nofussLastCheck = millis();
  49. }
  50. void nofussSetup() {
  51. _nofussConfigure();
  52. NoFUSSClient.onMessage([](nofuss_t code) {
  53. if (code == NOFUSS_START) {
  54. DEBUG_MSG_P(PSTR("[NoFUSS] Start\n"));
  55. }
  56. if (code == NOFUSS_UPTODATE) {
  57. DEBUG_MSG_P(PSTR("[NoFUSS] Already in the last version\n"));
  58. }
  59. if (code == NOFUSS_NO_RESPONSE_ERROR) {
  60. DEBUG_MSG_P(PSTR("[NoFUSS] Wrong server response: %d %s\n"), NoFUSSClient.getErrorNumber(), (char *) NoFUSSClient.getErrorString().c_str());
  61. }
  62. if (code == NOFUSS_PARSE_ERROR) {
  63. DEBUG_MSG_P(PSTR("[NoFUSS] Error parsing server response\n"));
  64. }
  65. if (code == NOFUSS_UPDATING) {
  66. DEBUG_MSG_P(PSTR("[NoFUSS] Updating\n"));
  67. DEBUG_MSG_P(PSTR(" New version: %s\n"), (char *) NoFUSSClient.getNewVersion().c_str());
  68. DEBUG_MSG_P(PSTR(" Firmware: %s\n"), (char *) NoFUSSClient.getNewFirmware().c_str());
  69. DEBUG_MSG_P(PSTR(" File System: %s\n"), (char *) NoFUSSClient.getNewFileSystem().c_str());
  70. #if WEB_SUPPORT
  71. wsSend_P(PSTR("{\"message\": 1}"));
  72. #endif
  73. }
  74. if (code == NOFUSS_FILESYSTEM_UPDATE_ERROR) {
  75. DEBUG_MSG_P(PSTR("[NoFUSS] File System Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  76. }
  77. if (code == NOFUSS_FILESYSTEM_UPDATED) {
  78. DEBUG_MSG_P(PSTR("[NoFUSS] File System Updated\n"));
  79. }
  80. if (code == NOFUSS_FIRMWARE_UPDATE_ERROR) {
  81. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  82. }
  83. if (code == NOFUSS_FIRMWARE_UPDATED) {
  84. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Updated\n"));
  85. }
  86. if (code == NOFUSS_RESET) {
  87. DEBUG_MSG_P(PSTR("[NoFUSS] Resetting board\n"));
  88. #if WEB_SUPPORT
  89. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  90. #endif
  91. delay(100);
  92. }
  93. if (code == NOFUSS_END) {
  94. DEBUG_MSG_P(PSTR("[NoFUSS] End\n"));
  95. }
  96. });
  97. #if WEB_SUPPORT
  98. wsOnSendRegister(_nofussWebSocketOnSend);
  99. wsOnAfterParseRegister(_nofussConfigure);
  100. #endif
  101. }
  102. void nofussLoop() {
  103. if (!_nofussEnabled) return;
  104. if (!wifiConnected()) return;
  105. if ((_nofussLastCheck > 0) && ((millis() - _nofussLastCheck) < _nofussInterval)) return;
  106. nofussRun();
  107. }
  108. #endif // NOFUSS_SUPPORT