From 8d51ceb975610e7051e963527240368ab1d3ed47 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 18:32:57 +1200 Subject: [PATCH] Added alerts notification and clearance in the main core function --- GpsTracker/Core.cpp | 32 +++++++++++++++++++++++++++++++- GpsTracker/Core.h | 2 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index b144619..3c0ee03 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -1,8 +1,10 @@ #include "Core.h" #include "Config.h" #include "Flash.h" +#include "Alerts.h" #define LOGGER_NAME "Core" +#define SMS_BUFFER_SIZE 100 using namespace utils; @@ -21,17 +23,45 @@ namespace core { if (acquired) { positions::appendLast(metadata); + forceBackup = updateSleepTime(); - gps::preserveCurrentCoordinates(); } + notifyFailures(metadata); + alerts::clear(metadata); positions::doBackup(forceBackup); if (acquired) updateRtcTime(); mainunit::deepSleep(sleepTime); } + void notifyFailures(PositionEntryMetadata &metadata) { + uint8_t triggered = alerts::getTriggered(metadata); + uint8_t notified = 0; + SIM808RegistrationStatus networkStatus; + char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; + + if (!triggered) return; + + network::powerOn(); + networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); + + if (!network::isAvailable(networkStatus.stat)) return; + + if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); + } + + if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2f°C. Backup battery failure ?\n"), metadata.batteryLevel); + } + + //TODO : send sms, return if failed + alerts::add(notified); //only add the successly notified failures + //TODO : network::powerOff(); count "handles" like for i2c ? + } + void updateRtcTime() { tmElements_t time; gps::getTime(time); diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index c21c2ab..3e8a5f5 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -17,4 +17,6 @@ namespace core { void updateRtcTime(); bool updateSleepTime(); uint16_t mapSleepTime(uint8_t velocity); + + void notifyFailures(PositionEntryMetadata &metadata); } \ No newline at end of file