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ů.

101 řádky
2.6 KiB

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