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.

130 line
3.8 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. size_t available = SMS_BUFFER_SIZE - strlen(buffer);
  33. if (!triggered) return 0;
  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. available = SMS_BUFFER_SIZE - strlen(buffer);
  39. snprintf_P(buffer + strlen(buffer), available, PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); //TODO : this code will NOT print the string at the right place
  40. }
  41. if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) {
  42. available = SMS_BUFFER_SIZE - strlen(buffer);
  43. snprintf_P(buffer + strlen(buffer), available, PSTR(" - RTC was stopped. Bakup battery failure ?\n"));
  44. }
  45. if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
  46. available = SMS_BUFFER_SIZE - strlen(buffer);
  47. snprintf_P(buffer + strlen(buffer), available, PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast<uint16_t>(metadata.temperature * 100));
  48. }
  49. config_t* config = &config::main::value;
  50. bool sent = network::sendSms(config->contactPhone, buffer);
  51. if (!sent) NOTICE_MSG("notifyFailure", "SMS not sent !");
  52. network::powerOff();
  53. return sent ? triggered : 0;
  54. }
  55. void updateRtcTime() {
  56. tmElements_t time;
  57. gps::getTime(time);
  58. rtc::setTime(time);
  59. }
  60. bool updateSleepTime() {
  61. uint8_t velocity = gps::getVelocity();
  62. uint16_t result = mapSleepTime(velocity);
  63. bool goingLongSleep = false;
  64. if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) {
  65. float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ?
  66. if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0;
  67. else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops
  68. if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) {
  69. result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS;
  70. }
  71. else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true;
  72. }
  73. else stoppedInARow = 0;
  74. sleepTime = result;
  75. NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime);
  76. return goingLongSleep;
  77. }
  78. uint16_t mapSleepTime(uint8_t velocity) {
  79. uint16_t result;
  80. uint16_t currentTime = 0xFFFF;
  81. if (rtc::isAccurate()) {
  82. tmElements_t time;
  83. rtc::getTime(time);
  84. currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute);
  85. }
  86. for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) {
  87. sleepTimings_t timing;
  88. flash::read(&config::defaultSleepTimings[i], timing);
  89. if (velocity < timing.speed) continue;
  90. if (currentTime != 0xFFFF && (currentTime < timing.timeMin || currentTime > timing.timeMax)) continue;
  91. result = timing.seconds;
  92. break;
  93. }
  94. VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result);
  95. return result;
  96. }
  97. }