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.

180 lines
4.8 KiB

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