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.

139 linhas
3.8 KiB

  1. #include "Config.h"
  2. #include "Positions.h"
  3. #if defined(BACKUP_ENABLE_SDCARD)
  4. #define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD
  5. #endif
  6. #ifdef BACKUP_ENABLE_SDCARD
  7. #include "SdPositionsBackup.h"
  8. #endif
  9. #include "Debug.h"
  10. #include "Gps.h"
  11. #include "Network.h"
  12. #define LOGGER_NAME "Positions"
  13. #define ENTRY_RESERVED_SIZE 128
  14. #define ENTRIES_ADDR ENTRY_RESERVED_SIZE
  15. namespace positions {
  16. #ifdef BACKUPS_ENABLED
  17. backup::PositionsBackup **_backups;
  18. #endif
  19. namespace details {
  20. uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  21. uint16_t getEntryAddress(uint16_t index) {
  22. if (index > maxEntryIndex) return -1;
  23. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  24. }
  25. }
  26. void setup() {
  27. //TODO : enable/disable based on config
  28. #ifdef BACKUPS_ENABLED
  29. uint8_t backupIdx = 0;
  30. _backups = new backup::PositionsBackup*[BACKUPS_ENABLED];
  31. #ifdef BACKUP_ENABLE_SDCARD
  32. _backups[backupIdx] = new backup::sd::SdPositionsBackup();
  33. _backups[backupIdx++]->setup();
  34. #endif
  35. #endif
  36. }
  37. bool acquire(PositionEntryMetadata &metadata) {
  38. VERBOSE("acquire");
  39. timestamp_t before;
  40. gps::powerOn();
  41. before = rtc::getTime();
  42. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  43. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  44. gps::powerOff();
  45. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  46. uint16_t timeToFix = rtc::getTime() - before;
  47. tmElements_t time;
  48. gps::getTime(time);
  49. rtc::setTime(time);
  50. metadata = {
  51. battery.level,
  52. battery.voltage,
  53. rtc::getTemperature(),
  54. timeToFix,
  55. gpsStatus
  56. };
  57. return true;
  58. }
  59. void appendLast(const PositionEntryMetadata &metadata) {
  60. VERBOSE("appendLast");
  61. uint16_t entryAddress;
  62. PositionEntry entry = { metadata };
  63. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  64. hardware::i2c::powerOn();
  65. Config_t config = config::main::get();
  66. config.lastEntry++;
  67. if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0;
  68. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  69. if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0;
  70. entryAddress = details::getEntryAddress(config.lastEntry);
  71. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  72. 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);
  73. config::main::set(config);
  74. hardware::i2c::powerOff();
  75. }
  76. bool get(uint16_t index, PositionEntry &entry) {
  77. uint16_t entryAddress = details::getEntryAddress(index);
  78. if (entryAddress == -1) return false;
  79. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  80. hardware::i2c::powerOn();
  81. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  82. hardware::i2c::powerOff();
  83. 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);
  84. return true;
  85. }
  86. bool moveNext(uint16_t &index) {
  87. if (index == config::main::get().lastEntry) return false;
  88. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  89. else index++;
  90. return true;
  91. }
  92. uint16_t count(uint16_t fromIndex) {
  93. Config_t config = config::main::get();
  94. if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; }
  95. return config.lastEntry - fromIndex;
  96. }
  97. void doBackup() {
  98. #ifdef BACKUPS_ENABLED
  99. for (int i = 0; i < BACKUPS_ENABLED; i++) {
  100. _backups[i]->backup();
  101. }
  102. #endif
  103. }
  104. }