您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

117 行
3.3 KiB

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