25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 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. notifyFailures(metadata);
  23. alerts::clear(metadata);
  24. positions::doBackup(forceBackup);
  25. if (acquired) updateRtcTime();
  26. mainunit::deepSleep(sleepTime);
  27. }
  28. void notifyFailures(PositionEntryMetadata &metadata) {
  29. uint8_t triggered = alerts::getTriggered(metadata);
  30. uint8_t notified = 0;
  31. SIM808RegistrationStatus networkStatus;
  32. char buffer[SMS_BUFFER_SIZE] = "Alerts !\n";
  33. if (!triggered) return;
  34. network::powerOn();
  35. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  36. if (!network::isAvailable(networkStatus.stat)) return;
  37. if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) {
  38. sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel);
  39. }
  40. if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
  41. sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2f°C. Backup battery failure ?\n"), metadata.batteryLevel);
  42. }
  43. config_t* config = &config::main::value;
  44. if (!network::sendSms(config->contactPhone, buffer)) {
  45. NOTICE_MSG("notifyFailure", "SMS not sent !");
  46. return;
  47. }
  48. alerts::add(notified); //only add the successly notified failures
  49. //TODO : network::powerOff(); count "handles" like for i2c ?
  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. }