Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
49:0456ee2271be
Parent:
45:3b9e1923cb15
Child:
99:55317f374a94
--- a/src/CloudDataHandler/CloudFileReceiver.cpp	Mon Sep 12 22:19:27 2016 +0000
+++ b/src/CloudDataHandler/CloudFileReceiver.cpp	Tue Sep 13 19:25:13 2016 +0000
@@ -3,26 +3,17 @@
 #include "MbedJSONValue.h"
 #include "global.h"
 
-char CloudFileReceiverDataBuf[1024];
-
-bool CloudFileReceiver( std::string *recv_string, mDot *dot )
-{
-    MbedJSONValue from_json;
-    bool status;
-
-    parse( from_json, recv_string->c_str() );
+char CloudFileReceiverWriteBuf[1024];
+char CloudFileReceiverReadBuf[1024];
 
-    int sequence = from_json["seq"].get<int>();
-//    printf("Received command: %s\r\n", recv_string->c_str());
-    // sequence of -1 means string sent in 1 chunk.
-    if( sequence == -1 ) {
+bool StoreReceivedFile( mDot *dot, MbedJSONValue &from_json )
+{
         FILENAME_STRING filename;
 
-        int mType = from_json["pay"]["mtype"].get<int>();
-        logInfo("mtype=%d\r\n", mType);
-
         memset( filename, '\0', sizeof(FILENAME_STRING) );
 
+        int mType = from_json["pay"]["mtype"].get<int>();
+        logInfo("mtype=%d", mType);
         switch( mType ) {
             case SETPOINT_CONTROL_MTYPE: {
                 logInfo("PROCESSING SETPONT CONTROL FILE");
@@ -36,13 +27,14 @@
             case TIMER_CONTROL_MTYPE:
                 break;
             case INPUT_CONFIG_MTYPE: {
+                logInfo("id=%s", from_json["pay"]["input"]["id"].get<std::string>().c_str() );
                 snprintf( filename, sizeof(FILENAME_STRING), "input_%s%s",
                           from_json["pay"]["input"]["id"].get<std::string>().c_str(),
                           ".jsn" );
                 logInfo("INPUT CONFIG FILENAME=%s", filename);
 
-                memset( CloudFileReceiverDataBuf, '\0', 1024 );
-                snprintf(CloudFileReceiverDataBuf, sizeof(CloudFileReceiverDataBuf),
+                memset( CloudFileReceiverWriteBuf, '\0', 1024 );
+                snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf),
                          "{ "
                          "\"id\":\"%s\", "
                          "\"name\":\"%s\", "
@@ -54,7 +46,9 @@
                          "\"rtype\":\"%s\", "
                          "\"type\":\"%s\", "
                          "\"size\":\"%s\", "
-                         "\"order\":\"%s\" } ",
+                         "\"order\":\"%s\", "
+                         "\"fmt\":\"%s\", "
+                         "\"rfreq\":\"%s\" } ",
                          from_json["pay"]["input"]["id"].get<std::string>().c_str(),
                          from_json["pay"]["input"]["name"].get<std::string>().c_str(),
                          from_json["pay"]["input"]["units"].get<std::string>().c_str(),
@@ -65,13 +59,15 @@
                          from_json["pay"]["input"]["rtype"].get<std::string>().c_str(),
                          from_json["pay"]["input"]["type"].get<std::string>().c_str(),
                          from_json["pay"]["input"]["size"].get<std::string>().c_str(),
-                         from_json["pay"]["input"]["order"].get<std::string>().c_str()
+                         from_json["pay"]["input"]["order"].get<std::string>().c_str(),
+                         from_json["pay"]["input"]["fmt"].get<std::string>().c_str(),
+                         from_json["pay"]["input"]["rfreq"].get<std::string>().c_str()
                         );
 
-                logInfo("saving string%s", CloudFileReceiverDataBuf);
-                bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverDataBuf, 1024);
+                logInfo("saving string%s", CloudFileReceiverWriteBuf);
+                bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024);
                 if( status != true ) {
-                    logInfo("(%d)save file failed, status=%d", __LINE__, status);
+                    logError("(%d)save file failed, status=%d", __LINE__, status);
                 }
 
                 logInfo("Sending Mail To ModbusMasterMailBox, filename=%s", filename);
@@ -85,72 +81,85 @@
                 logInfo("DEFAULT");
                 break;
         }
+        return true;
+}
 
-        return false;
+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>();
+
+    // sequence of -1 means string sent in 1 chunk.
+    if( sequence == -1 ) {
+
+        return StoreReceivedFile( dot, from_json );
 
     } 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 );
+        memset(CloudFileReceiverWriteBuf,0,sizeof(CloudFileReceiverWriteBuf));
+        snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", "{\"seq\":-1, \"pay\":",from_json["pay"].get<std::string>().c_str() );
+        logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, CloudFileReceiverWriteBuf );
 
-        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
+        status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024);
         if( status != true ) {
-            logInfo("(%d)save file failed, status=%d", __LINE__, status);
+            logError("(%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));
+        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf));
+
     } else if( sequence == -2 ) {
 
+        MbedJSONValue final_json;
+
         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);
+        status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024);
         if( status != true ) {
-            logInfo("(%d)read file failed, status=%d", __LINE__, status);
+            logError("(%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 );
+        snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str(), "}" );
+        logInfo("(%d)Final String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf );
 
-        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
+        parse( final_json, CloudFileReceiverWriteBuf );
+
+        status = StoreReceivedFile( dot, final_json );
         if( status != true ) {
-            logInfo("(%d)save file failed, status=%d", __LINE__, status);
-            return false;
+            logError("(%d)save file failed, status=%d", __LINE__, status);
         }
-        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf));
+
+        status = dot->deleteUserFile("scratch.json");
+        if( status != true ) {
+            logError("(%d)delete file failed, status=%d", __LINE__, status);
+        }
+
+        logInfo("(%d)DELETED scratch.json FILE, status:%d", __LINE__, status );
+
     } 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);
+        status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024);
         if( status != true ) {
-            logInfo("(%d)read file failed, status=%d", __LINE__, status);
+            logError("(%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 );
+        snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str() );
+        logInfo("(%d)Writing String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf );
 
-        status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024);
+        status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024);
         if( status != true ) {
-            logInfo("(%d)save file failed, status=%d", __LINE__, status);
+            logError("(%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));
+        logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf));
+
     }
 
     return true;