選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

117 行
3.7 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 LOGGER_NAME "Positions::backup::network"
  10. #define BUFFER_SIZE 170
  11. namespace positions {
  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. char buffer[BUFFER_SIZE];
  23. if(signalAttenuation == 0) signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation;
  24. snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,%s"),
  25. mainunit::freeRam(),
  26. signalAttenuation,
  27. entry.metadata.batteryLevel,
  28. entry.metadata.batteryVoltage,
  29. entry.metadata.temperature,
  30. static_cast<uint8_t>(entry.metadata.status),
  31. entry.metadata.timeToFix,
  32. entry.position);
  33. NOTICE_FORMAT("appendPosition", "Sending : %s", buffer);
  34. uint16_t responseCode = hardware::sim808::device.httpPost(
  35. config::main::value.network.url,
  36. F("text/gpstracker"),
  37. buffer,
  38. buffer,
  39. BUFFER_SIZE
  40. );
  41. NOTICE_FORMAT("appendPosition", "Response : %d", responseCode);
  42. return responseCode == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  43. }
  44. //__attribute__((__optimize__("O2")))
  45. void NetworkPositionsBackup::appendPositions() {
  46. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  47. PositionEntry currentEntry;
  48. SIM808RegistrationStatus networkStatus;
  49. //avoid edge case where if 0, whole set of positions will be sent again
  50. if (!positions::count(config::main::value.network.lastSavedEntry)) return;
  51. network::powerOn();
  52. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  53. if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) {
  54. networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow
  55. NOTICE_MSG("appendPositions", "network or gprs unavailable");
  56. if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) {
  57. networkUnavailablePostpone++;
  58. }
  59. }
  60. else {
  61. networkUnavailableInARow = 0;
  62. networkUnavailablePostpone = 1;
  63. int8_t signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation;
  64. hardware::i2c::powerOn();
  65. do {
  66. if (!positions::get(currentEntryIndex, currentEntry)) break;
  67. if (!appendPosition(currentEntry, signalAttenuation)) break;
  68. config::main::value.network.lastSavedEntry = currentEntryIndex;
  69. config::main::save();
  70. } while (positions::moveNext(currentEntryIndex));
  71. hardware::i2c::powerOff();
  72. }
  73. network::powerOff();
  74. }
  75. void NetworkPositionsBackup::setup() {}
  76. void NetworkPositionsBackup::prepare() {
  77. NOTICE("prepare");
  78. if (!isBackupNeeded(true)) return;
  79. network::powerOn();
  80. }
  81. void NetworkPositionsBackup::backup(bool force) {
  82. NOTICE("backup");
  83. if (force || isBackupNeeded(false)) {
  84. appendPositions();
  85. }
  86. network::powerOff();
  87. }
  88. }
  89. }
  90. }
  91. #endif