You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 rivejä
3.4 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_P(buffer, BUFFER_SIZE, PSTR("%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. PositionEntry currentEntry;
  46. SIM808RegistrationStatus networkStatus;
  47. //avoid edge case where if 0, whole set of positions will be sent again
  48. if (!positions::count(config::main::value.network.lastSavedEntry)) return;
  49. network::powerOn();
  50. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  51. if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) {
  52. networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow
  53. NOTICE_MSG("appendPositions", "network or gprs unavailable");
  54. if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) {
  55. networkUnavailablePostpone++;
  56. }
  57. }
  58. else {
  59. networkUnavailableInARow = 0;
  60. networkUnavailablePostpone = 1;
  61. hardware::i2c::powerOn();
  62. do {
  63. if (!positions::get(currentEntryIndex, currentEntry)) break;
  64. if (!appendPosition(currentEntry)) break;
  65. config::main::value.network.lastSavedEntry = currentEntryIndex;
  66. config::main::save();
  67. } while (positions::moveNext(currentEntryIndex));
  68. hardware::i2c::powerOff();
  69. }
  70. network::powerOff();
  71. }
  72. void NetworkPositionsBackup::setup() {}
  73. void NetworkPositionsBackup::prepare() {
  74. NOTICE("prepare");
  75. if (!isBackupNeeded(true)) return;
  76. network::powerOn();
  77. }
  78. void NetworkPositionsBackup::backup(bool force) {
  79. NOTICE("backup");
  80. if (force || isBackupNeeded(false)) {
  81. appendPositions();
  82. }
  83. network::powerOff();
  84. }
  85. }
  86. }
  87. }
  88. #endif