Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

93 linhas
2.3 KiB

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