Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/src/AnalyticsLogger/AnalyticsLogger.cpp

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 *
 * File:                AnalyticsLogger.cpp
 * Desciption:          source for the ICE Analytics Logger
 *
 *****************************************************************************/
#include "global.h"
#include <stdio.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <time.h>
#include "AnalyticsLogger.h"
#include "LoggerApi.h"
//#include "rtc.h"

static void checkStackUsage(void);

/*****************************************************************************
 * Function:             AnalyticsLogger
 * Description:          entry point for the Analytics Logger
 *
 * @param                (IN) args (user-defined arguments)
 * @return               none
 *****************************************************************************/
void AnalyticsLogger(void const *args)
{
    struct tm       *ts;
    time_t curr_sec;
    int last_min=8;
    bool log_sent=false;
    
    printf("\rAnalyticsLogger has started...\n");

    while ( true ) {

        std::ostringstream log_event;

        curr_sec = time(0);
        ts = localtime(&curr_sec);
//        printf("curr_sec=%ld, min=%d (last=%d)\r\n", curr_sec, ts->tm_min, last_min );
        if( ((ts->tm_min%5) == 0) && (ts->tm_min != last_min) ) {
            last_min = ts->tm_min;

            int map_count = ModbusRegisterMap.size();
            int collected_count = 0;

            log_event << "\"lr\":[";
            std::map<std::string, ModbusRegister>::iterator iter;
            for (iter = ModbusRegisterMap.begin(); iter != ModbusRegisterMap.end(); ++iter) {

                collected_count = collected_count + 1;

                log_event << "{\"t\":"<< "\"" << iter->first.c_str() << "\"," << "\"v\":"<< "\"" << RegisterValueMap[iter->first].float_value<< "\"},";
                log_sent = false;
                if( log_event.str().size() >= 150 ) {
                    std::string str = log_event.str();
                    str.erase( str.size() - 1 );
                    if( collected_count == map_count ) {
                        str.append("],\"seq\":\"0\"");
                    } else {
                        str.append("],\"seq\":\"1\"");
                    }
//                    printf("%s:%d: Logging %s : len=%d\r\n", __func__, __LINE__, str.c_str(), str.length() );
                    LiveDataLoggerApi( str.c_str() );
                    log_event.str("");
                    log_event.clear();
                    log_event << "\"lr\":[";
                    log_sent = true;
                }
            }
            if( log_sent == false ) {
                std::string str = log_event.str();
                str.erase( str.size() - 1 );
                str.append("],\"seq\":\"0\"");
                if( str.length() > 20 ) {
//                    printf("%s:%d: Logging %s : len=%d\r\n", __func__, __LINE__, str.c_str(), str.length() );
                    LiveDataLoggerApi( str.c_str() );
                }
            }
        }

        Thread::wait(5000);
        checkStackUsage();
    }
}

//
// For optimizing/debugging purposes only!
//
static void checkStackUsage(void)
{
    const float threshold = .85;

    std::vector<std::pair<std::string, Thread*> > taskList;
    taskList.push_back(make_pair((std::string)"AnalyticsLogger",  GLOBAL_analyticsLogger_thread));
    taskList.push_back(make_pair((std::string)"DataHandler",      GLOBAL_dataHandler_thread));
    taskList.push_back(make_pair((std::string)"ConfigHandler",    GLOBAL_configHandler_thread));
    taskList.push_back(make_pair((std::string)"ControlTask",      GLOBAL_controlTask_thread));
    taskList.push_back(make_pair((std::string)"ModbusMaster",     GLOBAL_modbusMaster_thread));
    taskList.push_back(make_pair((std::string)"OutputTask",       GLOBAL_outputTask_thread));
    for ( std::vector<std::pair<std::string, Thread*> >::iterator pos = taskList.begin(); pos != taskList.end(); ++ pos) {
        // do something
        if ( (float) pos->second->max_stack() / (float) pos->second->stack_size() >= threshold ) {
            printf("\rWARNING: %s has used %.2f%% of its stack!\n", pos->first.c_str(),
                   (double)((float)pos->second->max_stack() / (float)pos->second->stack_size()) * 100.0);
        }
    }
}