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.

92 regels
2.3 KiB

  1. #pragma once
  2. #include "Config.h"
  3. #if BACKUP_ENABLE_NETWORK
  4. #include "NetworkPositionsBackup.h"
  5. #include "Debug.h"
  6. #include "Positions.h"
  7. #include "Hardware.h"
  8. #include "Network.h"
  9. #define LOGGER_NAME "Positions::backup::network"
  10. #define BUFFER_SIZE 170
  11. namespace positions {
  12. namespace backup {
  13. namespace net {
  14. namespace details {
  15. bool isBackupNeeded() {
  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;
  19. }
  20. bool appendPosition(PositionEntry &entry) {
  21. VERBOSE("appendPosition");
  22. char buffer[BUFFER_SIZE];
  23. snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,",
  24. entry.metadata.batteryLevel,
  25. entry.metadata.batteryVoltage,
  26. static_cast<uint16_t>(entry.metadata.temperature * 100),
  27. static_cast<uint8_t>(entry.metadata.status));
  28. strcat(buffer, entry.position);
  29. return hardware::sim808::device.httpPost(
  30. config::main::value.network.url,
  31. F("text/csv"),
  32. buffer,
  33. buffer,
  34. BUFFER_SIZE
  35. ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  36. }
  37. void appendPositions() {
  38. VERBOSE("appendPositions");
  39. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  40. PositionEntry currentEntry;
  41. SIM808RegistrationStatus networkStatus;
  42. network::powerOn();
  43. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  44. if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable");
  45. else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable");
  46. else {
  47. hardware::i2c::powerOn();
  48. do {
  49. if (!positions::get(currentEntryIndex, currentEntry)) break;
  50. if (!appendPosition(currentEntry)) break;
  51. config::main::value.network.lastSavedEntry = currentEntryIndex;
  52. config::main::save();
  53. } while (positions::moveNext(currentEntryIndex));
  54. hardware::i2c::powerOff();
  55. }
  56. network::powerOff();
  57. }
  58. }
  59. void NetworkPositionsBackup::setup() {
  60. VERBOSE("setup");
  61. }
  62. void NetworkPositionsBackup::backup() {
  63. VERBOSE("backup");
  64. if (!details::isBackupNeeded()) return;
  65. details::appendPositions();
  66. }
  67. }
  68. }
  69. }
  70. #endif