Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/CloudDataHandler/CloudFileReceiver.cpp

Committer:
davidjhoward
Date:
2016-09-08
Revision:
31:4b1587034318
Parent:
27:38205cebc3da
Child:
45:3b9e1923cb15

File content as of revision 31:4b1587034318:

#include "CloudFileReceiver.h"
#include "MTSLog.h"
#include "MbedJSONValue.h"
#include "global.h"

bool CloudFileReceiver( std::string *recv_string, mDot *dot )
{
    MbedJSONValue from_json;
    bool status;
    
    parse( from_json, recv_string->c_str() );

    int sequence = from_json["seq"].get<int>();
    logInfo("Received command: %s", recv_string->c_str());
    // sequence of -1 means string sent in 1 chunk.
    if( sequence == -1 ) {

        FILENAME_STRING filename;

        int mType = from_json["pay"]["mtype"].get<int>();
        logInfo("mtype=%d", mType);

        memset( filename, '\0', sizeof(FILENAME_STRING) );

        switch( mType ) {
            case SETPOINT_CONTROL_MTYPE:
            {
                logInfo("PROCESSING SETPONT CONTROL FILE");
                snprintf( filename, sizeof(FILENAME_STRING), "%s_%s%s",
                          from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str(),
                          from_json["pay"]["spcontrol"]["input"].get<std::string>().c_str(),
                          ".json" );
                logInfo("SETPONT CONTROL FILENAME=%s", filename);
                break;
            }
            case TIMER_CONTROL_MTYPE:
                break;
            case INPUT_CONFIG_MTYPE:
            {
                logInfo("PROCESSING INPUT CONFIG FILE");
                snprintf( filename, sizeof(FILENAME_STRING), "input_%s%s",
                          from_json["pay"]["input"]["id"].get<std::string>().c_str(),
                          ".json" );
                logInfo("INPUT CONFIG FILENAME=%s", filename);

                logInfo("Sending Mail To ModbusMasterMailBox, filename=%s");
                Message_t *mail = ModbusMasterMailBox.alloc();
                mail->action = ACTION_READ_FILE;
                strncpy( mail->controlFile, filename, (sizeof(mail->controlFile)-1));
                ModbusMasterMailBox.put(mail);
                break;
            }
            default:
                logInfo("DEFAULT");
                break;
        }

        return false;

    } else if( sequence == 0 ) {
        char data_buf[1024];
//       std::string file_string(1024, '\0');
//       std::string recv_payload(from_json["pay"].get<std::string>().c_str());
//       file_string.replace(0,recv_payload.length(), recv_payload.c_str());
//       logInfo("(%d)File String Length=%d, %s", __LINE__, file_string.length(), file_string.c_str() );

        memset(data_buf,0,sizeof(data_buf));
        snprintf(data_buf, sizeof(data_buf), "%s", from_json["pay"].get<std::string>().c_str() );
        logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf );

        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
        if( status != true ) {
            logInfo("(%d)save file failed, status=%d", __LINE__, status);
            return false;
        }
        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf));
    } else if( sequence == -2 ) {

        logInfo("(%d)READING BACK scratch.json FILE FOR LAST PACKET", __LINE__);

        char scratch_buf[1024];
        char data_buf[1024];

        // read the file back
        status = dot->readUserFile("scratch.json", (void *)scratch_buf, 1024);
        if( status != true ) {
            logInfo("(%d)read file failed, status=%d", __LINE__, status);
            return false;
        }

        snprintf(data_buf, sizeof(data_buf), "%s%s", scratch_buf, from_json["pay"].get<std::string>().c_str() );
        logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf );

        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
        if( status != true ) {
            logInfo("(%d)save file failed, status=%d", __LINE__, status);
            return false;
        }
        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf));
    } else {

        logInfo("(%d)READING BACK scratch.json FILE", __LINE__);

        char scratch_buf[1024];
        char data_buf[1024];

        // read the file back
        status = dot->readUserFile("scratch.json", (void *)scratch_buf, 1024);
        if( status != true ) {
            logInfo("(%d)read file failed, status=%d", __LINE__, status);
            return false;
        }

        snprintf(data_buf, sizeof(data_buf), "%s%s", scratch_buf, from_json["pay"].get<std::string>().c_str() );
        logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf );

        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
        if( status != true ) {
            logInfo("(%d)save file failed, status=%d", __LINE__, status);
            return false;
        }
        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf));
    }

    return true;
}