Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

124 wiersze
3.9 KiB

  1. #pragma once
  2. #include "Config.h"
  3. #if BACKUP_ENABLE_NETWORK
  4. #include "NetworkPositionsBackup.h"
  5. #include "MainUnit.h"
  6. #include "Hardware.h"
  7. #include "Network.h"
  8. #include "Logging.h"
  9. #define BUFFER_SIZE 170
  10. namespace positions {
  11. #define CURRENT_LOGGER "Positions::backup::network"
  12. namespace backup {
  13. namespace net {
  14. uint8_t networkUnavailableInARow = 0;
  15. uint8_t networkUnavailablePostpone = 1;
  16. bool NetworkPositionsBackup::isBackupNeeded(bool forPrepare) {
  17. config_t *config = &config::main::value;
  18. return (config->network.lastSavedEntry == 0xFFFF && config->lastEntry != 0xFFFF) ||
  19. positions::count(config->network.lastSavedEntry) > (config->network.saveThreshold * networkUnavailablePostpone) - (forPrepare ? 1 : 0);
  20. }
  21. bool NetworkPositionsBackup::appendPosition(PositionEntry &entry, int8_t signalAttenuation = 0) {
  22. #define CURRENT_LOGGER_FUNCTION "appendPosition"
  23. char buffer[BUFFER_SIZE];
  24. if(signalAttenuation == 0) signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation;
  25. snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,%s"),
  26. mainunit::freeRam(),
  27. signalAttenuation,
  28. entry.metadata.batteryLevel,
  29. entry.metadata.batteryVoltage,
  30. entry.metadata.temperature,
  31. static_cast<uint8_t>(entry.metadata.status),
  32. entry.metadata.timeToFix,
  33. entry.position);
  34. NOTICE_FORMAT("Sending : %s", buffer);
  35. uint16_t responseCode = hardware::sim808::device.httpPost(
  36. config::main::value.network.url,
  37. F("text/gpstracker"),
  38. buffer,
  39. buffer,
  40. BUFFER_SIZE
  41. );
  42. NOTICE_FORMAT("Response : %d", responseCode);
  43. return responseCode == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  44. }
  45. void NetworkPositionsBackup::appendPositions() {
  46. #define CURRENT_LOGGER_FUNCTION "appendPositions"
  47. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  48. PositionEntry currentEntry;
  49. SIM808_NETWORK_REGISTRATION_STATE networkStatus;
  50. //avoid edge case where if 0, whole set of positions will be sent again
  51. if (!positions::count(config::main::value.network.lastSavedEntry)) return;
  52. network::powerOn();
  53. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  54. if (networkStatus == SIM808_NETWORK_REGISTRATION_STATE::ERROR ||
  55. (!network::isAvailable(networkStatus) || !network::enableGprs())) {
  56. networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow
  57. NOTICE_MSG("network or gprs unavailable");
  58. if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) {
  59. networkUnavailablePostpone++;
  60. }
  61. }
  62. else {
  63. networkUnavailableInARow = 0;
  64. networkUnavailablePostpone = 1;
  65. int8_t signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation;
  66. hardware::i2c::powerOn();
  67. do {
  68. if (!positions::get(currentEntryIndex, currentEntry)) break;
  69. if (!appendPosition(currentEntry, signalAttenuation)) break;
  70. config::main::value.network.lastSavedEntry = currentEntryIndex;
  71. config::main::save();
  72. } while (positions::moveNext(currentEntryIndex));
  73. hardware::i2c::powerOff();
  74. }
  75. network::powerOff();
  76. }
  77. void NetworkPositionsBackup::setup() {}
  78. void NetworkPositionsBackup::prepare(bool force) {
  79. #define CURRENT_LOGGER_FUNCTION "prepare"
  80. if (!(force || isBackupNeeded(true))) return;
  81. NOTICE;
  82. network::powerOn();
  83. }
  84. void NetworkPositionsBackup::backup(bool force) {
  85. #define CURRENT_LOGGER_FUNCTION "backup"
  86. if (!(force || isBackupNeeded(false))) return;
  87. NOTICE;
  88. appendPositions();
  89. network::powerOff();
  90. }
  91. }
  92. }
  93. }
  94. #endif