Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

124 righe
3.8 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. //__attribute__((__optimize__("O2")))
  46. void NetworkPositionsBackup::appendPositions() {
  47. #define CURRENT_LOGGER_FUNCTION "appendPositions"
  48. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  49. PositionEntry currentEntry;
  50. SIM808_NETWORK_REGISTRATION_STATE networkStatus;
  51. //avoid edge case where if 0, whole set of positions will be sent again
  52. if (!positions::count(config::main::value.network.lastSavedEntry)) return;
  53. network::powerOn();
  54. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  55. if (!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() {
  79. #define CURRENT_LOGGER_FUNCTION "prepare"
  80. NOTICE;
  81. if (!isBackupNeeded(true)) return;
  82. network::powerOn();
  83. }
  84. void NetworkPositionsBackup::backup(bool force) {
  85. #define CURRENT_LOGGER_FUNCTION "backup"
  86. NOTICE;
  87. if (force || isBackupNeeded(false)) {
  88. appendPositions();
  89. }
  90. network::powerOff();
  91. }
  92. }
  93. }
  94. }
  95. #endif