Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

143 lignes
4.0 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 ?\n");
  43. uint8_t triggered = alerts::getTriggered(metadata);
  44. if (!triggered) return NO_ALERTS_NOTIFIED;
  45. network::powerOn();
  46. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  47. if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED;
  48. details::appendToSmsBuffer(buffer, PSTR("Alerts !\n"));
  49. if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) {
  50. details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel);
  51. }
  52. if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) {
  53. details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped. %S"), backupFailureString);
  54. }
  55. if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
  56. details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast<uint16_t>(metadata.temperature * 100), backupFailureString);
  57. }
  58. bool notified = network::sendSms(buffer);
  59. if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !");
  60. network::powerOff();
  61. return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them)
  62. }
  63. void updateRtcTime() {
  64. tmElements_t time;
  65. gps::getTime(time);
  66. rtc::setTime(time);
  67. }
  68. bool updateSleepTime() {
  69. uint8_t velocity = gps::getVelocity();
  70. uint16_t result = mapSleepTime(velocity);
  71. bool goingLongSleep = false;
  72. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  73. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  74. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  75. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  76. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  77. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  78. }
  79. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  80. }
  81. else stoppedInARow = 0;
  82. sleepTime = result;
  83. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  84. return goingLongSleep;
  85. }
  86. uint16_t mapSleepTime(uint8_t velocity) {
  87. uint16_t result;
  88. uint16_t currentTime = 0xFFFF;
  89. if (rtc::isAccurate()) {
  90. tmElements_t time;
  91. rtc::getTime(time);
  92. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  93. }
  94. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  95. sleepTimings_t timing;
  96. flash::read(&config::defaultSleepTimings[i], timing);
  97. if (velocity < timing.speed) continue;
  98. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  99. result = timing.seconds;
  100. break;
  101. }
  102. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  103. return result;
  104. }
  105. }