Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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