@@ -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; | |||
@@ -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(); | |||
} |
@@ -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() { | |||
@@ -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<uint8_t>(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); | |||
} | |||
} | |||
} | |||
} |
@@ -1,3 +1,18 @@ | |||
#pragma once | |||
#include "PositionsBackup.h" | |||
#include "PositionsBackup.h" | |||
namespace positions { | |||
namespace backup { | |||
namespace network { | |||
class NetworkPositionsBackup : public PositionsBackup { | |||
private: | |||
public: | |||
void setup(); | |||
void backup(); | |||
}; | |||
} | |||
} | |||
} |
@@ -1,4 +1,13 @@ | |||
#pragma once | |||
#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 | |||
#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]; | |||
}; |
@@ -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; | |||