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.

189 lines
5.0 KiB

6 years ago
6 years ago
  1. /*
  2. NOFUSS MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: nof
  5. */
  6. #if NOFUSS_SUPPORT
  7. #include "NoFUSSClient.h"
  8. unsigned long _nofussLastCheck = 0;
  9. unsigned long _nofussInterval = 0;
  10. bool _nofussEnabled = false;
  11. // -----------------------------------------------------------------------------
  12. // NOFUSS
  13. // -----------------------------------------------------------------------------
  14. #if WEB_SUPPORT
  15. void _nofussWebSocketOnSend(JsonObject& root) {
  16. root["nofVisible"] = 1;
  17. root["nofEnabled"] = getSetting("nofEnabled", NOFUSS_ENABLED).toInt() == 1;
  18. root["nofServer"] = getSetting("nofServer", NOFUSS_SERVER);
  19. }
  20. #endif // WEB_SUPPORT
  21. void _nofussConfigure() {
  22. String nofussServer = getSetting("nofServer", NOFUSS_SERVER);
  23. #if MDNS_CLIENT_SUPPORT
  24. nofussServer = mdnsResolve(nofussServer);
  25. #endif
  26. if (nofussServer.length() == 0) {
  27. setSetting("nofEnabled", 0);
  28. _nofussEnabled = false;
  29. } else {
  30. _nofussEnabled = getSetting("nofEnabled", NOFUSS_ENABLED).toInt() == 1;
  31. }
  32. _nofussInterval = getSetting("nofInterval", NOFUSS_INTERVAL).toInt();
  33. _nofussLastCheck = 0;
  34. if (!_nofussEnabled) {
  35. DEBUG_MSG_P(PSTR("[NOFUSS] Disabled\n"));
  36. } else {
  37. char buffer[20];
  38. snprintf_P(buffer, sizeof(buffer), PSTR("%s-%s"), APP_NAME, getDevice().c_str());
  39. NoFUSSClient.setServer(nofussServer);
  40. NoFUSSClient.setDevice(buffer);
  41. NoFUSSClient.setVersion(APP_VERSION);
  42. DEBUG_MSG_P(PSTR("[NOFUSS] Server : %s\n"), nofussServer.c_str());
  43. DEBUG_MSG_P(PSTR("[NOFUSS] Dervice: %s\n"), buffer);
  44. DEBUG_MSG_P(PSTR("[NOFUSS] Version: %s\n"), APP_VERSION);
  45. DEBUG_MSG_P(PSTR("[NOFUSS] Enabled\n"));
  46. }
  47. }
  48. bool _nofussKeyCheck(const char * key) {
  49. return (strncmp(key, "nof", 6) == 0);
  50. }
  51. void _nofussBackwards() {
  52. moveSettings("nofussServer", "nofServer"); // 1.14.0 2018-06-26
  53. moveSettings("nofussEnabled", "nofEnabled"); // 1.14.0 2018-06-26
  54. moveSettings("nofussInterval", "nofInterval"); // 1.14.0 2018-06-26
  55. }
  56. #if TERMINAL_SUPPORT
  57. void _nofussInitCommands() {
  58. settingsRegisterCommand(F("NOFUSS"), [](Embedis* e) {
  59. DEBUG_MSG_P(PSTR("+OK\n"));
  60. nofussRun();
  61. });
  62. }
  63. #endif // TERMINAL_SUPPORT
  64. // -----------------------------------------------------------------------------
  65. void nofussRun() {
  66. NoFUSSClient.handle();
  67. _nofussLastCheck = millis();
  68. }
  69. void nofussSetup() {
  70. _nofussBackwards();
  71. _nofussConfigure();
  72. NoFUSSClient.onMessage([](nofuss_t code) {
  73. if (code == NOFUSS_START) {
  74. DEBUG_MSG_P(PSTR("[NoFUSS] Start\n"));
  75. }
  76. if (code == NOFUSS_UPTODATE) {
  77. DEBUG_MSG_P(PSTR("[NoFUSS] Already in the last version\n"));
  78. }
  79. if (code == NOFUSS_NO_RESPONSE_ERROR) {
  80. DEBUG_MSG_P(PSTR("[NoFUSS] Wrong server response: %d %s\n"), NoFUSSClient.getErrorNumber(), (char *) NoFUSSClient.getErrorString().c_str());
  81. }
  82. if (code == NOFUSS_PARSE_ERROR) {
  83. DEBUG_MSG_P(PSTR("[NoFUSS] Error parsing server response\n"));
  84. }
  85. if (code == NOFUSS_UPDATING) {
  86. DEBUG_MSG_P(PSTR("[NoFUSS] Updating\n"));
  87. DEBUG_MSG_P(PSTR(" New version: %s\n"), (char *) NoFUSSClient.getNewVersion().c_str());
  88. DEBUG_MSG_P(PSTR(" Firmware: %s\n"), (char *) NoFUSSClient.getNewFirmware().c_str());
  89. DEBUG_MSG_P(PSTR(" File System: %s\n"), (char *) NoFUSSClient.getNewFileSystem().c_str());
  90. #if WEB_SUPPORT
  91. wsSend_P(PSTR("{\"message\": 1}"));
  92. #endif
  93. }
  94. if (code == NOFUSS_FILESYSTEM_UPDATE_ERROR) {
  95. DEBUG_MSG_P(PSTR("[NoFUSS] File System Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  96. }
  97. if (code == NOFUSS_FILESYSTEM_UPDATED) {
  98. DEBUG_MSG_P(PSTR("[NoFUSS] File System Updated\n"));
  99. }
  100. if (code == NOFUSS_FIRMWARE_UPDATE_ERROR) {
  101. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Update Error: %s\n"), (char *) NoFUSSClient.getErrorString().c_str());
  102. }
  103. if (code == NOFUSS_FIRMWARE_UPDATED) {
  104. DEBUG_MSG_P(PSTR("[NoFUSS] Firmware Updated\n"));
  105. }
  106. if (code == NOFUSS_RESET) {
  107. DEBUG_MSG_P(PSTR("[NoFUSS] Resetting board\n"));
  108. #if WEB_SUPPORT
  109. wsSend_P(PSTR("{\"action\": \"reload\"}"));
  110. #endif
  111. nice_delay(100);
  112. }
  113. if (code == NOFUSS_END) {
  114. DEBUG_MSG_P(PSTR("[NoFUSS] End\n"));
  115. }
  116. });
  117. #if WEB_SUPPORT
  118. wsOnSendRegister(_nofussWebSocketOnSend);
  119. wsOnAfterParseRegister(_nofussConfigure);
  120. #endif
  121. #if TERMINAL_SUPPORT
  122. _nofussInitCommands();
  123. #endif
  124. // Register
  125. espurnaRegisterLoop(nofussLoop);
  126. settingsRegisterKeyCheck(_nofussKeyCheck);
  127. }
  128. void nofussLoop() {
  129. if (!_nofussEnabled) return;
  130. if (!wifiConnected()) return;
  131. if ((_nofussLastCheck > 0) && ((millis() - _nofussLastCheck) < _nofussInterval)) return;
  132. nofussRun();
  133. }
  134. #endif // NOFUSS_SUPPORT