Você não pode selecionar mais de 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.

128 linhas
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. if (!network::sendSms(config->contactPhone, buffer)) {
  47. NOTICE_MSG("notifyFailure", "SMS not sent !");
  48. return 0;
  49. }
  50. network::powerOff();
  51. return triggered;
  52. }
  53. void updateRtcTime() {
  54. tmElements_t time;
  55. gps::getTime(time);
  56. rtc::setTime(time);
  57. }
  58. bool updateSleepTime() {
  59. uint8_t velocity = gps::getVelocity();
  60. uint16_t result = mapSleepTime(velocity);
  61. bool goingLongSleep = false;
  62. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  63. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  64. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  65. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  66. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  67. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  68. }
  69. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  70. }
  71. else stoppedInARow = 0;
  72. sleepTime = result;
  73. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  74. return goingLongSleep;
  75. }
  76. uint16_t mapSleepTime(uint8_t velocity) {
  77. uint16_t result;
  78. uint16_t currentTime = 0xFFFF;
  79. if (rtc::isAccurate()) {
  80. tmElements_t time;
  81. rtc::getTime(time);
  82. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  83. }
  84. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  85. sleepTimings_t timing;
  86. flash::read(&config::defaultSleepTimings[i], timing);
  87. if (velocity < timing.speed) continue;
  88. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  89. result = timing.seconds;
  90. break;
  91. }
  92. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  93. return result;
  94. }
  95. }