You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 rivejä
3.3 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. //TODO : send sms, return if failed
  44. alerts::add(notified); //only add the successly notified failures
  45. //TODO : network::powerOff(); count "handles" like for i2c ?
  46. }
  47. void updateRtcTime() {
  48. tmElements_t time;
  49. gps::getTime(time);
  50. rtc::setTime(time);
  51. }
  52. bool updateSleepTime() {
  53. uint8_t velocity = gps::getVelocity();
  54. uint16_t result = mapSleepTime(velocity);
  55. bool goingLongSleep = false;
  56. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  57. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  58. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  59. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  60. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  61. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  62. }
  63. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  64. }
  65. else stoppedInARow = 0;
  66. sleepTime = result;
  67. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  68. return goingLongSleep;
  69. }
  70. uint16_t mapSleepTime(uint8_t velocity) {
  71. uint16_t result;
  72. uint16_t currentTime = 0xFFFF;
  73. if (rtc::isAccurate()) {
  74. tmElements_t time;
  75. rtc::getTime(time);
  76. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  77. }
  78. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  79. sleepTimings_t timing;
  80. flash::read(&config::defaultSleepTimings[i], timing);
  81. if (velocity < timing.speed) continue;
  82. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  83. result = timing.seconds;
  84. break;
  85. }
  86. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  87. return result;
  88. }
  89. }