浏览代码

Blind implementation of network positions backup

tags/v1.2.0
Bertrand Lemasle 7 年前
父节点
当前提交
1ce692f9e4
共有 7 个文件被更改,包括 132 次插入13 次删除
  1. +13
    -5
      GpsTracker/Config.cpp
  2. +10
    -3
      GpsTracker/Config.h
  3. +2
    -1
      GpsTracker/Hardware.cpp
  4. +79
    -0
      GpsTracker/NetworkPositionsBackup.cpp
  5. +16
    -1
      GpsTracker/NetworkPositionsBackup.h
  6. +10
    -1
      GpsTracker/NetworkPositionsConfig.h
  7. +2
    -2
      GpsTracker/Positions.cpp

+ 13
- 5
GpsTracker/Config.cpp 查看文件

@@ -7,7 +7,7 @@
namespace config { namespace config {
namespace main { namespace main {


Config_t value;
config_t value;


namespace details { namespace details {


@@ -15,7 +15,7 @@ namespace config {
VERBOSE("read"); VERBOSE("read");
hardware::i2c::powerOn(); hardware::i2c::powerOn();
hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); 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(); hardware::i2c::powerOff();
} }


@@ -28,25 +28,33 @@ namespace config {
} }
} }


Config_t get() {
config_t get() {
if (value.seed == 0) details::read(); if (value.seed == 0) details::read();


VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry);
return value; return value;
} }


void set(const Config_t config) {
void set(const config_t config) {
value = config; value = config;
details::write(); details::write();
} }


void reset() { void reset() {
VERBOSE("reset"); VERBOSE("reset");
Config_t config = {
config_t config = {
CONFIG_SEED, CONFIG_SEED,
VERSION, VERSION,
0xFFFF, 0xFFFF,
0xFFFF, 0xFFFF,
#if BACKUP_ENABLE_NETWORK
{
POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD,
0xFFFF,
POSITIONS_CONFIG_DEFAULT_APN,
POSITIONS_CONFIG_DEFAULT_URL,
},
#endif
}; };


value = config; value = config;


+ 10
- 3
GpsTracker/Config.h 查看文件

@@ -5,6 +5,10 @@
#define BACKUP_ENABLE_SDCARD 0 #define BACKUP_ENABLE_SDCARD 0
#define BACKUP_ENABLE_NETWORK 1 #define BACKUP_ENABLE_NETWORK 1


#if BACKUP_ENABLE_NETWORK
#include "NetworkPositionsConfig.h"
#endif

#define CONFIG_ADDR 0 #define CONFIG_ADDR 0
#define CONFIG_RESERVED_SIZE 128 #define CONFIG_RESERVED_SIZE 128
#define CONFIG_SEED 13 #define CONFIG_SEED 13
@@ -20,11 +24,14 @@ struct sleepTimings_t {
uint16_t seconds; uint16_t seconds;
}; };


struct Config_t {
struct config_t {
uint8_t seed; uint8_t seed;
char version[5]; char version[5];
uint16_t firstEntry; uint16_t firstEntry;
uint16_t lastEntry; uint16_t lastEntry;
#if BACKUP_ENABLE_NETWORK
networkConfig_t network;
#endif
}; };


namespace config { namespace config {
@@ -41,8 +48,8 @@ namespace config {
}; };


namespace main { namespace main {
Config_t get();
void set(const Config_t config);
config_t get();
void set(const config_t config);


void reset(); void reset();
} }

+ 2
- 1
GpsTracker/Hardware.cpp 查看文件

@@ -1,3 +1,4 @@
#include "Config.h"
#include "Hardware.h" #include "Hardware.h"
#include "Pins.h" #include "Pins.h"
#include "Debug.h" #include "Debug.h"
@@ -65,7 +66,7 @@ namespace hardware {
VERBOSE("networkPowerOn"); VERBOSE("networkPowerOn");
powerOn(); powerOn();
device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL);
device.enableGprs("Vodafone"); //TODO : get from config
device.enableGprs(config::main::get().network.apn);
} }


void networkPowerOff() { void networkPowerOff() {


+ 79
- 0
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<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);
}
}
}
}

+ 16
- 1
GpsTracker/NetworkPositionsBackup.h 查看文件

@@ -1,3 +1,18 @@
#pragma once #pragma once


#include "PositionsBackup.h"
#include "PositionsBackup.h"

namespace positions {
namespace backup {
namespace network {

class NetworkPositionsBackup : public PositionsBackup {
private:
public:
void setup();
void backup();
};

}
}
}

+ 10
- 1
GpsTracker/NetworkPositionsConfig.h 查看文件

@@ -1,4 +1,13 @@
#pragma once #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];
};

+ 2
- 2
GpsTracker/Positions.cpp 查看文件

@@ -84,7 +84,7 @@ namespace positions {
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);


hardware::i2c::powerOn(); hardware::i2c::powerOn();
Config_t config = config::main::get();
config_t config = config::main::get();


config.lastEntry++; config.lastEntry++;
if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0;
@@ -124,7 +124,7 @@ namespace positions {
} }


uint16_t count(uint16_t fromIndex) { 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; } if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; }


return config.lastEntry - fromIndex; return config.lastEntry - fromIndex;


正在加载...
取消
保存