Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/CloudDataHandler/LogHandler.cpp

Committer:
davidjhoward
Date:
2016-09-23
Revision:
123:ce602c91a9c3
Parent:
116:7337ed514891
Child:
124:48119db89152

File content as of revision 123:ce602c91a9c3:

#include "LogHandler.h"
#include "LogLocalApi.h"
#include "CloudFileReceiver.h"
#include "MTSLog.h"
#include "MbedJSONValue.h"
#include "global.h"
#include <sstream>
#include <iostream>

Mail<LoggerQueue_t, 16> LoggerQueue;

size_t LoggerXmitLength = LOG_BYTES_PER_ENTRY;

bool LogHandler( bool joined )
{
    int32_t ret;
    char buffer[LOG_BYTES_PER_ENTRY];
    std::string tmp_buffer;
    bool log_in_eeprom=false;
    bool log_to_send=false;

    osEvent evt = LoggerQueue.get(50);
    if( (evt.status == osEventMail) ) {
        // pull the log event from the queue, even if we are not joined
        LoggerQueue_t *LoggerEvent = (LoggerQueue_t*)evt.value.p;
        logInfo("Log Msg Received: log entry: %s", LoggerEvent->log_entry);
        strncpy( buffer, LoggerEvent->log_entry, (LoggerXmitLength-1) );
        tmp_buffer.assign(buffer);
        log_to_send = true;
//        printf("%s:%d: Found Log Event on LoggerQueue to send to cloud\r\n", __func__, __LINE__);
        LoggerQueue.free(LoggerEvent);
    }

    if( (evt.status == osEventMail) && (joined == false) ) {
        // if we pulled a log from the queue put it in the EEPROM
        LogLocalApi( tmp_buffer.c_str() );
//        printf("%s:%d: Not Connected, Putting Logger event In EEPROM\r\n", __func__, __LINE__);
        return false;
    }

    if( (evt.status != osEventMail) && joined == true) {
        // nothing on the queue, see if there is anything in the EEPROM to send.
        log_in_eeprom = LogLocalApi_PopEntry( buffer );
        if( log_in_eeprom == true ) {
            tmp_buffer.assign(buffer);
            log_to_send = true;
 //           printf("%s:%d: Found Log Event in EEPROM to send to cloud\r\n", __func__, __LINE__);
        }
        else
        {
//            printf("%s:%d: Nothing in EEPROM\r\n", __func__, __LINE__);
        }
    }

    if( log_to_send == false ) {
        // no event log to send, send the heart beat message.
//        printf("%s:%d: No Log Event to send, attempt heart beat\r\n", __func__, __LINE__);
        tmp_buffer = "{\"mtype\":\"20\"}";
    }

    if( joined == true ) {
        std::vector<uint8_t> data(tmp_buffer.begin(), tmp_buffer.end());
        if ((ret = GLOBAL_mdot->send(data)) == mDot::MDOT_OK) {
//            printf("%s:%d: Successful send to cloud\r\n", __func__, __LINE__);
            return true;
        }
//        printf("failed to send, ret=%d, %s\r\n", ret, mDot::getReturnCodeString(ret).c_str());
    }

    if( log_to_send == true ) {
        // We had a log event ready to send but didn't send it.
        // Store it in the EEPROM for the next attempt.
//        printf("%s:%d: Could not send Log Event to cloud, store in EEPROM\r\n", __func__, __LINE__);
        LogLocalApi( tmp_buffer.c_str() );
    }
    else
    {
//        printf("%s:%d: Could not send to cloud, nothing to store in EEPROM\r\n", __func__, __LINE__);
    }
    return false;
}