126 lines
3.5 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 100
  7. using namespace utils;
  8. namespace core {
  9. uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;
  10. uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1;
  11. void main() {
  12. bool forceBackup = false;
  13. bool acquired = false;
  14. PositionEntryMetadata metadata;
  15. positions::prepareBackup();
  16. acquired = positions::acquire(metadata);
  17. if (acquired) {
  18. positions::appendLast(metadata);
  19. forceBackup = updateSleepTime();
  20. gps::preserveCurrentCoordinates();
  21. }
  22. alerts::add(notifyFailures(metadata));
  23. alerts::clear(metadata);
  24. positions::doBackup(forceBackup);
  25. if (acquired) updateRtcTime();
  26. mainunit::deepSleep(sleepTime);
  27. }
  28. uint8_t notifyFailures(PositionEntryMetadata &metadata) {
  29. uint8_t triggered = alerts::getTriggered(metadata);
  30. SIM808RegistrationStatus networkStatus;
  31. char buffer[SMS_BUFFER_SIZE] = "Alerts !\n";
  32. if (!triggered) return 0;
  33. network::powerOn();
  34. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  35. if (!network::isAvailable(networkStatus.stat)) return;
  36. if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) {
  37. sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel);
  38. }
  39. if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) {
  40. sprintf_P(buffer + strlen(buffer), PSTR(" - RTC was stopped. Bakup battery failure ?\n"));
  41. }
  42. if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
  43. sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast<uint16_t>(metadata.temperature * 100));
  44. }
  45. config_t* config = &config::main::value;
  46. bool sent = network::sendSms(config->contactPhone, buffer);
  47. if (!sent) NOTICE_MSG("notifyFailure", "SMS not sent !");
  48. network::powerOff();
  49. return sent ? triggered : 0;
  50. }
  51. void updateRtcTime() {
  52. tmElements_t time;
  53. gps::getTime(time);
  54. rtc::setTime(time);
  55. }
  56. bool updateSleepTime() {
  57. uint8_t velocity = gps::getVelocity();
  58. uint16_t result = mapSleepTime(velocity);
  59. bool goingLongSleep = false;
  60. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  61. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  62. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  63. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  64. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  65. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  66. }
  67. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  68. }
  69. else stoppedInARow = 0;
  70. sleepTime = result;
  71. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  72. return goingLongSleep;
  73. }
  74. uint16_t mapSleepTime(uint8_t velocity) {
  75. uint16_t result;
  76. uint16_t currentTime = 0xFFFF;
  77. if (rtc::isAccurate()) {
  78. tmElements_t time;
  79. rtc::getTime(time);
  80. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  81. }
  82. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  83. sleepTimings_t timing;
  84. flash::read(&config::defaultSleepTimings[i], timing);
  85. if (velocity < timing.speed) continue;
  86. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  87. result = timing.seconds;
  88. break;
  89. }
  90. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  91. return result;
  92. }
  93. }