#pragma once #include "Config.h" #if BACKUP_ENABLE_NETWORK #include "NetworkPositionsBackup.h" #include "Debug.h" #include "Positions.h" #include "Hardware.h" #include "Network.h" #define LOGGER_NAME "Positions::backup::network" #define BUFFER_SIZE 170 namespace positions { namespace backup { namespace net { namespace details { bool isBackupNeeded() { config_t *config = &config::main::value; return (config->network.lastSavedEntry == 0xFFFF && config ->lastEntry != 0xFFFF) || positions::count(config->network.lastSavedEntry) > config->network.saveThreshold; } bool appendPosition(PositionEntry &entry) { VERBOSE("appendPosition"); char buffer[BUFFER_SIZE]; snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,", entry.metadata.batteryLevel, entry.metadata.batteryVoltage, static_cast(entry.metadata.temperature * 100), static_cast(entry.metadata.status)); strcat(buffer, entry.position); return hardware::sim808::device.httpPost( config::main::value.network.url, F("text/csv"), buffer, buffer, BUFFER_SIZE ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } void appendPositions() { VERBOSE("appendPositions"); uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); else { hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; if (!appendPosition(currentEntry)) break; config::main::value.network.lastSavedEntry = currentEntryIndex; config::main::save(); } while (positions::moveNext(currentEntryIndex)); hardware::i2c::powerOff(); } network::powerOff(); } } void NetworkPositionsBackup::setup() { VERBOSE("setup"); } void NetworkPositionsBackup::backup() { VERBOSE("backup"); if (!details::isBackupNeeded()) return; details::appendPositions(); } } } } #endif