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.

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