Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

Revision:
0:61364762ee0e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ICE-Application/src/DataHandler/LogHandler.cpp	Tue Jan 24 19:05:33 2017 +0000
@@ -0,0 +1,206 @@
+#include "global.h"
+#include <stdio.h>
+#include <sstream>
+#include <iostream>
+#include <time.h>
+#include "LogHandler.h"
+#include "LogLocalApi.h"
+#include "LoggerApi.h"
+#ifdef MDOT_ICE
+#include "BLEDataHandler.h"
+#include "MTSLog.h"
+#include "CloudFileReceiver.h"
+#include "CloudDataHandler.h"
+#endif
+
+#ifndef MDOT_ICE
+#include "DataHandler.h"
+#endif
+#include "utilities.h"
+#include "version.h"
+
+Mail<LoggerQueue_t, 16> LoggerQueue;
+
+size_t LoggerXmitLength = LOG_BYTES_PER_ENTRY;
+
+int last_min=8;
+
+
+static void constructHeartBeat(std::string &msg);
+
+//
+// function:            LogHandler
+// description: 
+//
+// @param[in]           joined
+// @param[out]          none
+// @return              
+//
+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;
+
+#ifdef MDOT_ICE
+    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 ) {
+        struct tm       *ts;
+        time_t curr_sec;
+
+        curr_sec = time(0);
+        ts = localtime(&curr_sec);
+//        printf("curr_sec=%ld, min=%d (last=%d) min-mod-5=%d\r\n", curr_sec, ts->tm_min, last_min, (ts->tm_min%5) );
+        if( ((ts->tm_min%1) == 0) && (ts->tm_min != last_min) ) {
+            last_min = ts->tm_min;
+            // no event log to send, send the heart beat message.
+            logInfo("\r%s:%d: sending heart beat, hr=%d min=%d\r\n", __func__, __LINE__, ts->tm_hour, ts->tm_min);
+            constructHeartBeat(tmp_buffer);
+            logInfo("\r%s (%d bytes)\n", tmp_buffer.c_str(), tmp_buffer.size());
+        } else {
+//            printf("%s:%d: Nothing to send to the cloud\r\n", __func__, __LINE__);
+            if( CloudDataHandler_RcvFile != true ) {
+                return false;
+            }
+            printf("%s:%d: currently receiveing a file\r\n", __func__, __LINE__);
+            // send so we get another packet from gateway.
+            tmp_buffer = "{\"mtype\":\"20\"}";
+        }
+    }
+
+    if( joined == true ) {
+        std::vector<uint8_t> data(tmp_buffer.begin(), tmp_buffer.end());
+        ret = GLOBAL_mdot->send(data);
+        if ( ret == mDot::MDOT_OK ) {
+            //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__);
+    }
+#endif
+    return false;
+}
+
+// 
+// function:            CDH_ReplyToHandler
+// description:
+//
+// @param[in]
+// @param[out]
+// @return
+// 
+void CDH_ReplyToHandler( std::string &id, int status, float value )
+{
+    std::ostringstream reply_oss;
+
+    LoggerQueue_t *LoggerEvent = LoggerQueue.alloc();
+    memset( LoggerEvent->log_entry, 0, sizeof(LoggerEvent->log_entry) );
+
+    reply_oss << "{  \"mtype\":" << BT_MODBUS_COMMAND_REPLY_MTYPE << ", \"mbreply\":{ \"id\":\"" << id.c_str() << "\"," "\"status\":\"" << status << "\"," "\"value\":\"" << value << "\"} }";
+    std::string string_reply = reply_oss.str();
+    strncpy( LoggerEvent->log_entry, string_reply.c_str(), (sizeof(LoggerEvent->log_entry)-1));
+    printf("%s:%d: Reply is: %s\r\n",__func__,__LINE__, LoggerEvent->log_entry);
+    LoggerEvent->position = 0;
+    LoggerQueue.put(LoggerEvent);
+}
+
+//
+// function:        ReplyToHandler
+// description:     
+// 
+// @param[in]       
+// @param[out]
+// @return
+//
+void ReplyToHandler( ThreadName_t replyTo, std::string &id, int ret, float value )
+{
+#ifdef MDOT_ICE
+    if( replyTo == BLE_HANDLER ) {
+        BLE_ReplyToHandler( id, ret, value );
+    } else if( replyTo == CLOUD_DATA_HANDLER ) {
+        CDH_ReplyToHandler( id, ret, value );
+    } else {
+        logError("Unknown Thread for Command Reply");
+    }
+#else
+#undef TODO_ICE
+    // do something
+#endif 
+}
+
+//
+// function:            constructHeartBeat()
+// description:         construct the periodic heartbeat message
+//
+// @param[out]          constructed message
+// @return              none
+//
+static void constructHeartBeat(std::string &msg)
+{
+    //std::ostringstream heartbeat_msg(200);
+    std::ostringstream heartbeat_msg;
+    heartbeat_msg.precision(2);
+
+    std::string heap_data = Util_getHeapData();
+    // let's strip off everything after 'bytes'
+    std::string::size_type i = heap_data.find("bytes");
+    if (i != std::string::npos)
+        heap_data.erase(i+strlen("bytes"), heap_data.length());
+
+
+    heartbeat_msg << "{ \"mtype\":" << HEARTBEAT_MSG_MTYPE << 
+        ", \"hb\": {"
+        "\"fwver\": \"" << MAJOR_VERSION_NUMBER << "." << MINOR_VERSION_NUMBER << "." << PATCH_VERSION_NUMBER << "\","
+        "\"heap\":\"" << heap_data << "\","
+        "\"AL\":\""   << (((double)GLOBAL_analyticsLogger_thread->max_stack()/(double)GLOBAL_analyticsLogger_thread->stack_size())*100.) << "%\","
+        "\"DH\":\""   << (((double)GLOBAL_dataHandler_thread->max_stack()/(double)GLOBAL_dataHandler_thread->stack_size())*100.) << "%\","
+        "\"CFG\":\""  << (((double)GLOBAL_configHandler_thread->max_stack()/(double)GLOBAL_configHandler_thread->stack_size())*100.) << "%\","
+        "\"CT\":\""   << (((double)GLOBAL_controlTask_thread->max_stack()/(double)GLOBAL_controlTask_thread->stack_size())*100.) << "%\","
+        "\"MM\":\""   << (((double)GLOBAL_modbusMaster_thread->max_stack()/(double)GLOBAL_modbusMaster_thread->stack_size())*100.) << "%\","
+        "\"OUT\":\""  << (((double)GLOBAL_outputTask_thread->max_stack()/(double)GLOBAL_outputTask_thread->stack_size())*100.) << "%\","
+        "\"boot\":\"" << Util_getLastBootTime() << "\" "
+        "} }";
+    msg = heartbeat_msg.str();
+}