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

118 řádky
3.3 KiB

  1. #pragma once
  2. #include "Config.h"
  3. #if BACKUP_ENABLE_NETWORK
  4. #include "NetworkPositionsBackup.h"
  5. #include "Debug.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. uint8_t networkUnavailableInARow = 0;
  14. uint8_t networkUnavailablePostpone = 1;
  15. bool NetworkPositionsBackup::isBackupNeeded(bool forPrepare) {
  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 * networkUnavailablePostpone) - (forPrepare ? 1 : 0);
  19. }
  20. bool NetworkPositionsBackup::appendPosition(PositionEntry &entry) {
  21. char buffer[BUFFER_SIZE];
  22. snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,%d,%d,%d,",
  23. debug::freeRam(),
  24. hardware::sim808::device.getSignalQuality().attenuation,
  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. entry.metadata.timeToFix);
  30. strcat(buffer, entry.position);
  31. NOTICE_FORMAT("appendPosition", "Sending : %s", buffer);
  32. uint16_t responseCode = hardware::sim808::device.httpPost(
  33. config::main::value.network.url,
  34. F("text/gpstracker"),
  35. buffer,
  36. buffer,
  37. BUFFER_SIZE
  38. ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  39. NOTICE_FORMAT("appendPosition", "Response : %d", responseCode);
  40. return responseCode;
  41. }
  42. //__attribute__((__optimize__("O2")))
  43. void NetworkPositionsBackup::appendPositions() {
  44. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  45. uint32_t networkTimeout = 0;
  46. PositionEntry currentEntry;
  47. SIM808RegistrationStatus networkStatus;
  48. network::powerOn();
  49. networkTimeout = NETWORK_DEFAULT_TOTAL_TIMEOUT_MS;
  50. if (_prepareTime > 0) networkTimeout -= (rtc::getTime() - _prepareTime) * 1000;
  51. networkStatus = network::waitForRegistered(networkTimeout);
  52. if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) {
  53. networkUnavailableInARow++;
  54. NOTICE_MSG("appendPositions", "network or gprs unavailable");
  55. if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) {
  56. networkUnavailablePostpone++;
  57. }
  58. }
  59. else {
  60. networkUnavailableInARow = 0;
  61. networkUnavailablePostpone = 1;
  62. hardware::i2c::powerOn();
  63. do {
  64. if (!positions::get(currentEntryIndex, currentEntry)) break;
  65. if (!appendPosition(currentEntry)) break;
  66. config::main::value.network.lastSavedEntry = currentEntryIndex;
  67. config::main::save();
  68. } while (positions::moveNext(currentEntryIndex));
  69. hardware::i2c::powerOff();
  70. }
  71. network::powerOff();
  72. }
  73. void NetworkPositionsBackup::setup() {
  74. NOTICE("setup");
  75. }
  76. void NetworkPositionsBackup::prepare() {
  77. NOTICE("prepare");
  78. if (!isBackupNeeded(true)) {
  79. _prepareTime = 0;
  80. return;
  81. }
  82. network::powerOn();
  83. _prepareTime = rtc::getTime();
  84. }
  85. void NetworkPositionsBackup::backup() {
  86. NOTICE("backup");
  87. if (!isBackupNeeded(false)) return;
  88. appendPositions();
  89. }
  90. }
  91. }
  92. }
  93. #endif