Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

99 řádky
2.5 KiB

  1. #pragma once
  2. #include "NetworkPositionsBackup.h"
  3. #include "Debug.h"
  4. #include "Positions.h"
  5. #include "Config.h"
  6. #include "Hardware.h"
  7. #include "Network.h"
  8. #define LOGGER_NAME "Positions::backup::network"
  9. #define BUFFER_SIZE 170
  10. namespace positions {
  11. namespace backup {
  12. namespace net {
  13. namespace details {
  14. bool isBackupNeeded() {
  15. config_t *config = &config::main::value;
  16. return config->network.lastSavedEntry == 0xFFFF ||
  17. positions::count(config->network.lastSavedEntry) > config->network.saveThreshold;
  18. }
  19. bool appendPosition(PositionEntry &entry) {
  20. VERBOSE("appendPosition");
  21. debug::displayFreeRam();
  22. char buffer[BUFFER_SIZE];
  23. snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,",
  24. entry.metadata.batteryLevel,
  25. entry.metadata.batteryVoltage,
  26. static_cast<uint16_t>(entry.metadata.temperature * 100),
  27. static_cast<uint8_t>(entry.metadata.status));
  28. strcat(buffer, entry.position);
  29. debug::displayFreeRam();
  30. return hardware::sim808::device.httpPost(
  31. config::main::value.network.url,
  32. F("text/csv"),
  33. buffer,
  34. buffer,
  35. BUFFER_SIZE
  36. ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  37. }
  38. void appendPositions() {
  39. VERBOSE("appendPositions");
  40. debug::displayFreeRam();
  41. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  42. PositionEntry currentEntry;
  43. SIM808RegistrationStatus networkStatus;
  44. network::powerOn();
  45. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  46. if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable");
  47. else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable");
  48. else {
  49. hardware::i2c::powerOn();
  50. do {
  51. if (!positions::get(currentEntryIndex, currentEntry)) break;
  52. debug::displayFreeRam();
  53. if (!appendPosition(currentEntry)) break;
  54. config::main::value.network.lastSavedEntry = currentEntryIndex;
  55. config::main::save();
  56. } while (positions::moveNext(currentEntryIndex));
  57. debug::displayFreeRam();
  58. hardware::i2c::powerOff();
  59. }
  60. network::powerOff();
  61. debug::displayFreeRam();
  62. }
  63. }
  64. void NetworkPositionsBackup::setup() {
  65. VERBOSE("setup");
  66. }
  67. void NetworkPositionsBackup::backup() {
  68. VERBOSE("backup");
  69. debug::displayFreeRam();
  70. if (!details::isBackupNeeded()) return;
  71. debug::displayFreeRam();
  72. details::appendPositions();
  73. }
  74. }
  75. }
  76. }