Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

148 linhas
4.1 KiB

  1. #include "Core.h"
  2. #include "Config.h"
  3. #include "Flash.h"
  4. #include "Alerts.h"
  5. #define LOGGER_NAME "Core"
  6. #define SMS_BUFFER_SIZE 140
  7. #define NO_ALERTS_NOTIFIED 0
  8. using namespace utils;
  9. namespace core {
  10. uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;
  11. uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1;
  12. namespace details {
  13. void appendToSmsBuffer(char * buffer, const char * fmt, ...) {
  14. va_list args;
  15. va_start(args, fmt);
  16. size_t bufferLeft = SMS_BUFFER_SIZE - strlen(buffer);
  17. char * p = buffer + strlen(buffer);
  18. vsnprintf_P(p, bufferLeft, fmt, args);
  19. va_end(args);
  20. }
  21. }
  22. void main() {
  23. bool forceBackup = false;
  24. bool acquired = false;
  25. PositionEntryMetadata metadata;
  26. positions::prepareBackup();
  27. acquired = positions::acquire(metadata);
  28. if (acquired) {
  29. positions::appendLast(metadata);
  30. forceBackup = updateSleepTime();
  31. gps::preserveCurrentCoordinates();
  32. }
  33. alerts::add(notifyFailures(metadata));
  34. alerts::clear(metadata);
  35. positions::doBackup(forceBackup);
  36. if (acquired) updateRtcTime();
  37. mainunit::deepSleep(sleepTime);
  38. }
  39. uint8_t notifyFailures(PositionEntryMetadata &metadata) {
  40. SIM808RegistrationStatus networkStatus;
  41. char buffer[SMS_BUFFER_SIZE];
  42. const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?");
  43. uint8_t triggered = alerts::getTriggered(metadata);
  44. if (!triggered) return NO_ALERTS_NOTIFIED;
  45. NOTICE_FORMAT("notifyFailures", "triggered : %B", triggered);
  46. network::powerOn();
  47. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  48. if (!network::isAvailable(networkStatus.stat)) {
  49. network::powerOff();
  50. return NO_ALERTS_NOTIFIED;
  51. }
  52. strncpy_P(buffer, PSTR("Alerts !"), SMS_BUFFER_SIZE);
  53. if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) {
  54. details::appendToSmsBuffer(buffer, PSTR("\n- Battery at %d%%."), metadata.batteryLevel);
  55. }
  56. if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
  57. details::appendToSmsBuffer(buffer, PSTR("\n- Temperature is %dC.%S"), static_cast<uint16_t>(metadata.temperature * 100), backupFailureString);
  58. }
  59. if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) {
  60. details::appendToSmsBuffer(buffer, PSTR("\n- RTC was stopped.%S"), backupFailureString);
  61. }
  62. bool notified = network::sendSms(buffer);
  63. if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !");
  64. network::powerOff();
  65. return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them)
  66. }
  67. void updateRtcTime() {
  68. tmElements_t time;
  69. gps::getTime(time);
  70. rtc::setTime(time);
  71. }
  72. bool updateSleepTime() {
  73. uint8_t velocity = gps::getVelocity();
  74. uint16_t result = mapSleepTime(velocity);
  75. bool goingLongSleep = false;
  76. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  77. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  78. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  79. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  80. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  81. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  82. }
  83. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  84. }
  85. else stoppedInARow = 0;
  86. sleepTime = result;
  87. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  88. return goingLongSleep;
  89. }
  90. uint16_t mapSleepTime(uint8_t velocity) {
  91. uint16_t result;
  92. uint16_t currentTime = 0xFFFF;
  93. if (rtc::isAccurate()) {
  94. tmElements_t time;
  95. rtc::getTime(time);
  96. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  97. }
  98. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  99. sleepTimings_t timing;
  100. flash::read(&config::defaultSleepTimings[i], timing);
  101. if (velocity < timing.speed) continue;
  102. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  103. result = timing.seconds;
  104. break;
  105. }
  106. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  107. return result;
  108. }
  109. }