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.

171 lines
4.6 KiB

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