diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 7ff59e4..c6c569d 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -7,7 +7,7 @@ namespace config { namespace main { - Config_t value; + config_t value; namespace details { @@ -15,7 +15,7 @@ namespace config { VERBOSE("read"); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); - if (CONFIG_SEED != value.seed) reset(); + if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); } @@ -28,25 +28,33 @@ namespace config { } } - Config_t get() { + config_t get() { if (value.seed == 0) details::read(); VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); return value; } - void set(const Config_t config) { + void set(const config_t config) { value = config; details::write(); } void reset() { VERBOSE("reset"); - Config_t config = { + config_t config = { CONFIG_SEED, VERSION, 0xFFFF, 0xFFFF, +#if BACKUP_ENABLE_NETWORK + { + POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, + 0xFFFF, + POSITIONS_CONFIG_DEFAULT_APN, + POSITIONS_CONFIG_DEFAULT_URL, + }, +#endif }; value = config; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index af74d10..28c298a 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -5,6 +5,10 @@ #define BACKUP_ENABLE_SDCARD 0 #define BACKUP_ENABLE_NETWORK 1 +#if BACKUP_ENABLE_NETWORK +#include "NetworkPositionsConfig.h" +#endif + #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 @@ -20,11 +24,14 @@ struct sleepTimings_t { uint16_t seconds; }; -struct Config_t { +struct config_t { uint8_t seed; char version[5]; uint16_t firstEntry; uint16_t lastEntry; +#if BACKUP_ENABLE_NETWORK + networkConfig_t network; +#endif }; namespace config { @@ -41,8 +48,8 @@ namespace config { }; namespace main { - Config_t get(); - void set(const Config_t config); + config_t get(); + void set(const config_t config); void reset(); } diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 316c69b..d262bc3 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -1,3 +1,4 @@ +#include "Config.h" #include "Hardware.h" #include "Pins.h" #include "Debug.h" @@ -65,7 +66,7 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs("Vodafone"); //TODO : get from config + device.enableGprs(config::main::get().network.apn); } void networkPowerOff() { diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp new file mode 100644 index 0000000..f0ba000 --- /dev/null +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -0,0 +1,79 @@ +#pragma once + +#include "NetworkPositionsBackup.h" +#include "Debug.h" +#include "Positions.h" +#include "Config.h" +#include "Hardware.h" + +#define LOGGER_NAME "Positions::backup::network" +#define BUFFER_SIZE 160 +#define EXPECTED_HTTP_RESPONSE_CODE 201 + +namespace positions { + namespace backup { + namespace network { + + namespace details { + + bool isBackupNeeded(config_t &config) { + config = config::main::get(); + + return config.network.lastSavedEntry == 0xFFFF || + positions::count(config.network.lastSavedEntry) > config.network.saveThreshold; + } + + bool appendPosition(config_t &config, PositionEntry &entry) { + char buffer[BUFFER_SIZE]; + snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"), + entry.metadata.batteryLevel, + entry.metadata.batteryVoltage, + entry.metadata.temperature, + static_cast(entry.metadata.status), + entry.position); + + return hardware::sim808::device.httpPost( + config.network.url, + PSTR("text/csv"), + buffer, + buffer, + BUFFER_SIZE + ) == EXPECTED_HTTP_RESPONSE_CODE; + } + + void appendPositions(config_t &config) { + VERBOSE("appendPositions"); + + uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; + PositionEntry currentEntry; + + hardware::i2c::powerOn(); + hardware::sim808::networkPowerOn(); + do { + if (!positions::get(currentEntryIndex, currentEntry)) break; + if (!appendPosition(config, currentEntry)) break; + + config.network.lastSavedEntry = currentEntryIndex; + config::main::set(config); + + } while (positions::moveNext(currentEntryIndex)); + hardware::sim808::networkPowerOff(); + hardware::i2c::powerOff(); + } + + } + + void NetworkPositionsBackup::setup() { + VERBOSE("backup"); + } + + void NetworkPositionsBackup::backup() { + VERBOSE("backup"); + + config_t config; + if (!details::isBackupNeeded(config)) return; + details::appendPositions(config); + } + } + } +} diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h index 8c75944..0fe578f 100644 --- a/GpsTracker/NetworkPositionsBackup.h +++ b/GpsTracker/NetworkPositionsBackup.h @@ -1,3 +1,18 @@ #pragma once -#include "PositionsBackup.h" \ No newline at end of file +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + namespace network { + + class NetworkPositionsBackup : public PositionsBackup { + private: + public: + void setup(); + void backup(); + }; + + } + } +} \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 760d147..24a661c 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -1,4 +1,13 @@ #pragma once -#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 \ No newline at end of file +#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 +#define POSITIONS_CONFIG_DEFAULT_APN "Vodafone" +#define POSITIONS_CONFIG_DEFAULT_URL "http://default.url" + +struct networkConfig_t { + uint8_t saveThreshold; + uint16_t lastSavedEntry; + char apn[20]; + char url[50]; +}; \ No newline at end of file diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 493b543..412273c 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -84,7 +84,7 @@ namespace positions { strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); hardware::i2c::powerOn(); - Config_t config = config::main::get(); + config_t config = config::main::get(); config.lastEntry++; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; @@ -124,7 +124,7 @@ namespace positions { } uint16_t count(uint16_t fromIndex) { - Config_t config = config::main::get(); + config_t config = config::main::get(); if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; } return config.lastEntry - fromIndex;