Não pode escolher mais do que 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.

157 linhas
4.3 KiB

  1. #include "Config.h"
  2. #include "Debug.h"
  3. #include "Positions.h"
  4. #include "Gps.h"
  5. #if BACKUP_ENABLE_SDCARD || BACKUP_ENABLE_NETWORK
  6. #define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK
  7. #endif
  8. #if BACKUP_ENABLE_SDCARD
  9. #include "SdPositionsBackup.h"
  10. #endif
  11. #if BACKUP_ENABLE_NETWORK
  12. #include "NetworkPositionsBackup.h"
  13. #endif
  14. #define LOGGER_NAME "Positions"
  15. #define ENTRY_RESERVED_SIZE 128
  16. #define ENTRIES_ADDR CONFIG_RESERVED_SIZE
  17. namespace positions {
  18. #ifdef BACKUPS_ENABLED
  19. backup::PositionsBackup **_backups;
  20. #endif
  21. namespace details {
  22. uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  23. uint16_t getEntryAddress(uint16_t index) {
  24. if (index > maxEntryIndex) return -1;
  25. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  26. }
  27. }
  28. void setup() {
  29. #ifdef BACKUPS_ENABLED
  30. uint8_t backupIdx = 0;
  31. _backups = new backup::PositionsBackup*[BACKUPS_ENABLED];
  32. #if BACKUP_ENABLE_SDCARD
  33. _backups[backupIdx] = new backup::sd::SdPositionsBackup();
  34. _backups[backupIdx]->setup();
  35. backupIdx++;
  36. #endif
  37. #if BACKUP_ENABLE_NETWORK
  38. _backups[backupIdx] = new backup::net::NetworkPositionsBackup();
  39. _backups[backupIdx]->setup();
  40. backupIdx++;
  41. #endif
  42. #endif
  43. }
  44. bool acquire(PositionEntryMetadata &metadata) {
  45. NOTICE("acquire");
  46. timestamp_t before;
  47. gps::powerOn();
  48. before = rtc::getTime();
  49. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  50. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  51. gps::powerOff();
  52. NOTICE_FORMAT("acquire", "status : %d", gpsStatus);
  53. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  54. uint16_t timeToFix = rtc::getTime() - before;
  55. tmElements_t time;
  56. gps::getTime(time);
  57. rtc::setTime(time);
  58. metadata = {
  59. battery.level,
  60. battery.voltage,
  61. rtc::getTemperature(),
  62. timeToFix,
  63. gpsStatus
  64. };
  65. return true;
  66. }
  67. void appendLast(const PositionEntryMetadata &metadata) {
  68. VERBOSE("appendLast");
  69. uint16_t entryIndex;
  70. uint16_t entryAddress;
  71. PositionEntry entry = { metadata };
  72. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  73. config_t* config = &config::main::value;
  74. entryIndex = config->lastEntry + 1;
  75. entryAddress = details::getEntryAddress(entryIndex);
  76. hardware::i2c::powerOn();
  77. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  78. NOTICE_FORMAT("appendLast", "Saved @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  79. config->lastEntry++;
  80. if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0;
  81. if (config->lastEntry == config->firstEntry) config->firstEntry++;
  82. if (config->firstEntry > details::maxEntryIndex) config->firstEntry = 0;
  83. config::main::save();
  84. hardware::i2c::powerOff();
  85. }
  86. bool get(uint16_t index, PositionEntry &entry) {
  87. uint16_t entryAddress = details::getEntryAddress(index);
  88. if (entryAddress == -1) return false;
  89. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  90. hardware::i2c::powerOn();
  91. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  92. hardware::i2c::powerOff();
  93. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  94. return true;
  95. }
  96. bool moveNext(uint16_t &index) {
  97. if (index == config::main::value.lastEntry) return false;
  98. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  99. else index++;
  100. return true;
  101. }
  102. uint16_t count(uint16_t fromIndex) {
  103. config_t *config = &config::main::value;
  104. if (config->lastEntry < config->firstEntry) { config->lastEntry += details::maxEntryIndex; }
  105. return config->lastEntry - fromIndex;
  106. }
  107. void doBackup() {
  108. #ifdef BACKUPS_ENABLED
  109. debug::displayFreeRam();
  110. VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED);
  111. //_backups[0]->backup(); //disabled for first real running test
  112. debug::displayFreeRam();
  113. /*for (int i = 0; i < BACKUPS_ENABLED; i++) {
  114. VERBOSE_FORMAT("doBackup", "calling backup %d", i);
  115. delay(1000);
  116. _backups[i]->backup();
  117. }*/
  118. #endif
  119. }
  120. }