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.

140 linhas
3.8 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. #endif
  36. #endif
  37. }
  38. bool acquire(PositionEntryMetadata &metadata) {
  39. VERBOSE("acquire");
  40. timestamp_t before;
  41. gps::powerOn();
  42. before = rtc::getTime();
  43. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  44. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  45. gps::powerOff();
  46. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  47. uint16_t timeToFix = rtc::getTime() - before;
  48. tmElements_t time;
  49. gps::getTime(time);
  50. rtc::setTime(time);
  51. metadata = {
  52. battery.level,
  53. battery.voltage,
  54. rtc::getTemperature(),
  55. timeToFix,
  56. gpsStatus
  57. };
  58. return true;
  59. }
  60. void appendLast(const PositionEntryMetadata &metadata) {
  61. VERBOSE("appendLast");
  62. uint16_t entryAddress;
  63. PositionEntry entry = { metadata };
  64. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  65. hardware::i2c::powerOn();
  66. Config_t config = config::main::get();
  67. config.lastEntry++;
  68. if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0;
  69. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  70. if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0;
  71. entryAddress = details::getEntryAddress(config.lastEntry);
  72. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  73. VERBOSE_FORMAT("appendLast", "Written to 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);
  74. config::main::set(config);
  75. hardware::i2c::powerOff();
  76. }
  77. bool get(uint16_t index, PositionEntry &entry) {
  78. uint16_t entryAddress = details::getEntryAddress(index);
  79. if (entryAddress == -1) return false;
  80. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  81. hardware::i2c::powerOn();
  82. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  83. hardware::i2c::powerOff();
  84. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  85. return true;
  86. }
  87. bool moveNext(uint16_t &index) {
  88. if (index == config::main::get().lastEntry) return false;
  89. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  90. else index++;
  91. return true;
  92. }
  93. uint16_t count(uint16_t fromIndex) {
  94. Config_t config = config::main::get();
  95. if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; }
  96. return config.lastEntry - fromIndex;
  97. }
  98. void doBackup() {
  99. #ifdef BACKUPS_ENABLED
  100. for (int i = 0; i < BACKUPS_ENABLED; i++) {
  101. _backups[i]->backup();
  102. }
  103. #endif
  104. }
  105. }