25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

152 satır
4.2 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. VERBOSE("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. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  53. uint16_t timeToFix = rtc::getTime() - before;
  54. tmElements_t time;
  55. gps::getTime(time);
  56. rtc::setTime(time);
  57. metadata = {
  58. battery.level,
  59. battery.voltage,
  60. rtc::getTemperature(),
  61. timeToFix,
  62. gpsStatus
  63. };
  64. return true;
  65. }
  66. void appendLast(const PositionEntryMetadata &metadata) {
  67. VERBOSE("appendLast");
  68. uint16_t entryAddress;
  69. PositionEntry entry = { metadata };
  70. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  71. hardware::i2c::powerOn();
  72. config_t config = config::main::get();
  73. config.lastEntry++;
  74. if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0;
  75. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  76. if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0;
  77. entryAddress = details::getEntryAddress(config.lastEntry);
  78. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  79. 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);
  80. config::main::set(config);
  81. hardware::i2c::powerOff();
  82. }
  83. bool get(uint16_t index, PositionEntry &entry) {
  84. uint16_t entryAddress = details::getEntryAddress(index);
  85. if (entryAddress == -1) return false;
  86. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  87. hardware::i2c::powerOn();
  88. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  89. hardware::i2c::powerOff();
  90. 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);
  91. return true;
  92. }
  93. bool moveNext(uint16_t &index) {
  94. if (index == config::main::get().lastEntry) return false;
  95. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  96. else index++;
  97. return true;
  98. }
  99. uint16_t count(uint16_t fromIndex) {
  100. config_t config = config::main::get();
  101. if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; }
  102. return config.lastEntry - fromIndex;
  103. }
  104. void doBackup() {
  105. #ifdef BACKUPS_ENABLED
  106. VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED);
  107. Serial.println((unsigned int)_backups[0], HEX);
  108. _backups[0]->backup();
  109. /*for (int i = 0; i < BACKUPS_ENABLED; i++) {
  110. VERBOSE_FORMAT("doBackup", "calling backup %d", i);
  111. delay(1000);
  112. _backups[i]->backup();
  113. }*/
  114. #endif
  115. }
  116. }