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.

90 regels
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. debug::displayFreeRam();
  21. return false;
  22. /*char buffer[BUFFER_SIZE];
  23. snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"),
  24. entry.metadata.batteryLevel,
  25. entry.metadata.batteryVoltage,
  26. entry.metadata.temperature,
  27. static_cast<uint8_t>(entry.metadata.status),
  28. entry.position);
  29. return hardware::sim808::device.httpPost(
  30. config.network.url,
  31. PSTR("text/csv"),
  32. buffer,
  33. buffer,
  34. BUFFER_SIZE
  35. ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;*/
  36. }
  37. void appendPositions(config_t &config) {
  38. VERBOSE("appendPositions");
  39. uint16_t currentEntryIndex = config.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(config, currentEntry)) break;
  51. config.network.lastSavedEntry = currentEntryIndex;
  52. config::main::set(config);
  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. config_t config;
  65. if (!details::isBackupNeeded(config)) return;
  66. details::appendPositions(config);
  67. }
  68. }
  69. }
  70. }