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.

192 lines
5.2 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
6 years ago
6 years ago
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. SYSTEM MODULE
  3. Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM_Rotate.h>
  6. // -----------------------------------------------------------------------------
  7. bool _system_send_heartbeat = false;
  8. unsigned char _heartbeat_mode = HEARTBEAT_MODE;
  9. unsigned long _heartbeat_interval = HEARTBEAT_INTERVAL;
  10. // Calculated load average 0 to 100;
  11. unsigned short int _load_average = 100;
  12. // -----------------------------------------------------------------------------
  13. #if SYSTEM_CHECK_ENABLED
  14. // Call this method on boot with start=true to increase the crash counter
  15. // Call it again once the system is stable to decrease the counter
  16. // If the counter reaches SYSTEM_CHECK_MAX then the system is flagged as unstable
  17. // setting _systemOK = false;
  18. //
  19. // An unstable system will only have serial access, WiFi in AP mode and OTA
  20. bool _systemStable = true;
  21. void systemCheck(bool stable) {
  22. unsigned char value = EEPROMr.read(EEPROM_CRASH_COUNTER);
  23. if (stable) {
  24. value = 0;
  25. DEBUG_MSG_P(PSTR("[MAIN] System OK\n"));
  26. } else {
  27. if (++value > SYSTEM_CHECK_MAX) {
  28. _systemStable = false;
  29. value = 0;
  30. DEBUG_MSG_P(PSTR("[MAIN] System UNSTABLE\n"));
  31. }
  32. }
  33. EEPROMr.write(EEPROM_CRASH_COUNTER, value);
  34. eepromCommit();
  35. }
  36. bool systemCheck() {
  37. return _systemStable;
  38. }
  39. void systemCheckLoop() {
  40. static bool checked = false;
  41. if (!checked && (millis() > SYSTEM_CHECK_TIME)) {
  42. // Check system as stable
  43. systemCheck(true);
  44. checked = true;
  45. }
  46. }
  47. #endif
  48. // -----------------------------------------------------------------------------
  49. void systemSendHeartbeat() {
  50. _system_send_heartbeat = true;
  51. }
  52. bool systemGetHeartbeat() {
  53. return _system_send_heartbeat;
  54. }
  55. unsigned long systemLoadAverage() {
  56. return _load_average;
  57. }
  58. void _systemSetupHeartbeat() {
  59. _heartbeat_mode = getSetting("hbMode", HEARTBEAT_MODE).toInt();
  60. _heartbeat_interval = getSetting("hbInterval", HEARTBEAT_INTERVAL).toInt();
  61. }
  62. #if WEB_SUPPORT
  63. bool _systemWebSocketOnReceive(const char * key, JsonVariant& value) {
  64. return (strncmp(key, "hb", 2) == 0);
  65. }
  66. #endif
  67. void systemLoop() {
  68. // -------------------------------------------------------------------------
  69. // User requested reset
  70. // -------------------------------------------------------------------------
  71. if (checkNeedsReset()) {
  72. reset();
  73. }
  74. // -------------------------------------------------------------------------
  75. // Check system stability
  76. // -------------------------------------------------------------------------
  77. #if SYSTEM_CHECK_ENABLED
  78. systemCheckLoop();
  79. #endif
  80. // -------------------------------------------------------------------------
  81. // Heartbeat
  82. // -------------------------------------------------------------------------
  83. if (_system_send_heartbeat && _heartbeat_mode == HEARTBEAT_ONCE) {
  84. heartbeat();
  85. _system_send_heartbeat = false;
  86. } else if (_heartbeat_mode == HEARTBEAT_REPEAT || _heartbeat_mode == HEARTBEAT_REPEAT_STATUS) {
  87. static unsigned long last_hbeat = 0;
  88. #if NTP_SUPPORT
  89. if ((_system_send_heartbeat && ntpSynced()) || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  90. #else
  91. if (_system_send_heartbeat || (millis() - last_hbeat > _heartbeat_interval * 1000)) {
  92. #endif
  93. last_hbeat = millis();
  94. heartbeat();
  95. _system_send_heartbeat = false;
  96. }
  97. }
  98. // -------------------------------------------------------------------------
  99. // Load Average calculation
  100. // -------------------------------------------------------------------------
  101. static unsigned long last_loadcheck = 0;
  102. static unsigned long load_counter_temp = 0;
  103. load_counter_temp++;
  104. if (millis() - last_loadcheck > LOADAVG_INTERVAL) {
  105. static unsigned long load_counter = 0;
  106. static unsigned long load_counter_max = 1;
  107. load_counter = load_counter_temp;
  108. load_counter_temp = 0;
  109. if (load_counter > load_counter_max) {
  110. load_counter_max = load_counter;
  111. }
  112. _load_average = 100 - (100 * load_counter / load_counter_max);
  113. last_loadcheck = millis();
  114. }
  115. }
  116. void _systemSetupSpecificHardware() {
  117. //The ESPLive has an ADC MUX which needs to be configured.
  118. #if defined(MANCAVEMADE_ESPLIVE)
  119. pinMode(16, OUTPUT);
  120. digitalWrite(16, HIGH); //Defualt CT input (pin B, solder jumper B)
  121. #endif
  122. // These devices use the hardware UART
  123. // to communicate to secondary microcontrollers
  124. #if defined(ITEAD_SONOFF_RFBRIDGE) || (RELAY_PROVIDER == RELAY_PROVIDER_DUAL) || (RELAY_PROVIDER == RELAY_PROVIDER_STM)
  125. Serial.begin(SERIAL_BAUDRATE);
  126. #endif
  127. }
  128. void systemSetup() {
  129. #if SPIFFS_SUPPORT
  130. SPIFFS.begin();
  131. #endif
  132. // Question system stability
  133. #if SYSTEM_CHECK_ENABLED
  134. systemCheck(false);
  135. #endif
  136. #if WEB_SUPPORT
  137. wsOnReceiveRegister(_systemWebSocketOnReceive);
  138. #endif
  139. // Init device-specific hardware
  140. _systemSetupSpecificHardware();
  141. // Register Loop
  142. espurnaRegisterLoop(systemLoop);
  143. // Cache Heartbeat values
  144. _systemSetupHeartbeat();
  145. }