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.

93 rivejä
2.5 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. char buffer[BUFFER_SIZE];
  22. snprintf(buffer, BUFFER_SIZE, "%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. strcat(buffer, entry.position);
  30. NOTICE_FORMAT("appendPosition", "Sending : %s", buffer);
  31. uint16_t responseCode = hardware::sim808::device.httpPost(
  32. config::main::value.network.url,
  33. F("text/csv"),
  34. buffer,
  35. buffer,
  36. BUFFER_SIZE
  37. ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;
  38. NOTICE_FORMAT("appendPosition", "Response : %d", responseCode);
  39. return responseCode;
  40. }
  41. void appendPositions() {
  42. uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1;
  43. PositionEntry currentEntry;
  44. SIM808RegistrationStatus networkStatus;
  45. network::powerOn();
  46. networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);
  47. if (!network::isAvailable(networkStatus.stat)) NOTICE_MSG("appendPositions", "network unavailable");
  48. else if (!network::enableGprs()) NOTICE_MSG("appendPositions", "gprs unavailable");
  49. else {
  50. hardware::i2c::powerOn();
  51. do {
  52. if (!positions::get(currentEntryIndex, currentEntry)) break;
  53. if (!appendPosition(currentEntry)) break;
  54. config::main::value.network.lastSavedEntry = currentEntryIndex;
  55. config::main::save();
  56. } while (positions::moveNext(currentEntryIndex));
  57. hardware::i2c::powerOff();
  58. }
  59. network::powerOff();
  60. }
  61. }
  62. void NetworkPositionsBackup::setup() {
  63. NOTICE("setup");
  64. }
  65. void NetworkPositionsBackup::backup() {
  66. NOTICE("backup");
  67. if (!details::isBackupNeeded()) return;
  68. details::appendPositions();
  69. }
  70. }
  71. }
  72. }
  73. #endif