Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/CloudDataHandler/CloudFileReceiver.cpp@27:38205cebc3da, 2016-09-08 (annotated)
- Committer:
- davidjhoward
- Date:
- Thu Sep 08 18:39:29 2016 +0000
- Revision:
- 27:38205cebc3da
- Child:
- 31:4b1587034318
more cloud data handler stuff
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| davidjhoward | 27:38205cebc3da | 1 | #include "CloudFileReceiver.h" |
| davidjhoward | 27:38205cebc3da | 2 | #include "MTSLog.h" |
| davidjhoward | 27:38205cebc3da | 3 | #include "MbedJSONValue.h" |
| davidjhoward | 27:38205cebc3da | 4 | |
| davidjhoward | 27:38205cebc3da | 5 | bool CloudFileReceiver( std::string *recv_string, mDot *dot ) |
| davidjhoward | 27:38205cebc3da | 6 | { |
| davidjhoward | 27:38205cebc3da | 7 | MbedJSONValue from_json; |
| davidjhoward | 27:38205cebc3da | 8 | bool status; |
| davidjhoward | 27:38205cebc3da | 9 | |
| davidjhoward | 27:38205cebc3da | 10 | parse( from_json, recv_string->c_str() ); |
| davidjhoward | 27:38205cebc3da | 11 | |
| davidjhoward | 27:38205cebc3da | 12 | int sequence = from_json["seq"].get<int>(); |
| davidjhoward | 27:38205cebc3da | 13 | logInfo("Received command: %s", recv_string->c_str()); |
| davidjhoward | 27:38205cebc3da | 14 | // logInfo("ID=%s",from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str()); |
| davidjhoward | 27:38205cebc3da | 15 | if( sequence == -1 ) { |
| davidjhoward | 27:38205cebc3da | 16 | |
| davidjhoward | 27:38205cebc3da | 17 | FILENAME_STRING filename; |
| davidjhoward | 27:38205cebc3da | 18 | |
| davidjhoward | 27:38205cebc3da | 19 | int mType = from_json["pay"]["mtype"].get<int>(); |
| davidjhoward | 27:38205cebc3da | 20 | logInfo("mtype=%d", mType); |
| davidjhoward | 27:38205cebc3da | 21 | |
| davidjhoward | 27:38205cebc3da | 22 | memset( filename, '\0', sizeof(FILENAME_STRING) ); |
| davidjhoward | 27:38205cebc3da | 23 | |
| davidjhoward | 27:38205cebc3da | 24 | switch( mType ) { |
| davidjhoward | 27:38205cebc3da | 25 | case SETPOINT_CONTROL_MTYPE: |
| davidjhoward | 27:38205cebc3da | 26 | logInfo("PROCESSING SETPONT CONTROL FILE"); |
| davidjhoward | 27:38205cebc3da | 27 | snprintf( filename, sizeof(TASKNAME_STRING), "%s_%s%s", |
| davidjhoward | 27:38205cebc3da | 28 | from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str(), |
| davidjhoward | 27:38205cebc3da | 29 | from_json["pay"]["spcontrol"]["input"].get<std::string>().c_str(), |
| davidjhoward | 27:38205cebc3da | 30 | ".json" ); |
| davidjhoward | 27:38205cebc3da | 31 | logInfo("SETPONT CONTROL FILENAME=%s", filename); |
| davidjhoward | 27:38205cebc3da | 32 | break; |
| davidjhoward | 27:38205cebc3da | 33 | case TIMER_CONTROL_MTYPE: |
| davidjhoward | 27:38205cebc3da | 34 | break; |
| davidjhoward | 27:38205cebc3da | 35 | default: |
| davidjhoward | 27:38205cebc3da | 36 | logInfo("DEFAULT"); |
| davidjhoward | 27:38205cebc3da | 37 | break; |
| davidjhoward | 27:38205cebc3da | 38 | } |
| davidjhoward | 27:38205cebc3da | 39 | |
| davidjhoward | 27:38205cebc3da | 40 | return false; |
| davidjhoward | 27:38205cebc3da | 41 | |
| davidjhoward | 27:38205cebc3da | 42 | } else if( sequence == 0 ) { |
| davidjhoward | 27:38205cebc3da | 43 | char data_buf[1024]; |
| davidjhoward | 27:38205cebc3da | 44 | // std::string file_string(1024, '\0'); |
| davidjhoward | 27:38205cebc3da | 45 | // std::string recv_payload(from_json["pay"].get<std::string>().c_str()); |
| davidjhoward | 27:38205cebc3da | 46 | // file_string.replace(0,recv_payload.length(), recv_payload.c_str()); |
| davidjhoward | 27:38205cebc3da | 47 | // logInfo("(%d)File String Length=%d, %s", __LINE__, file_string.length(), file_string.c_str() ); |
| davidjhoward | 27:38205cebc3da | 48 | |
| davidjhoward | 27:38205cebc3da | 49 | memset(data_buf,0,sizeof(data_buf)); |
| davidjhoward | 27:38205cebc3da | 50 | snprintf(data_buf, sizeof(data_buf), "%s", from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 27:38205cebc3da | 51 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf ); |
| davidjhoward | 27:38205cebc3da | 52 | |
| davidjhoward | 27:38205cebc3da | 53 | status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024); |
| davidjhoward | 27:38205cebc3da | 54 | if( status != true ) { |
| davidjhoward | 27:38205cebc3da | 55 | logInfo("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 56 | return false; |
| davidjhoward | 27:38205cebc3da | 57 | } |
| davidjhoward | 27:38205cebc3da | 58 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf)); |
| davidjhoward | 27:38205cebc3da | 59 | } else if( sequence == -2 ) { |
| davidjhoward | 27:38205cebc3da | 60 | |
| davidjhoward | 27:38205cebc3da | 61 | logInfo("(%d)READING BACK scratch.json FILE FOR LAST PACKET", __LINE__); |
| davidjhoward | 27:38205cebc3da | 62 | |
| davidjhoward | 27:38205cebc3da | 63 | char scratch_buf[1024]; |
| davidjhoward | 27:38205cebc3da | 64 | char data_buf[1024]; |
| davidjhoward | 27:38205cebc3da | 65 | |
| davidjhoward | 27:38205cebc3da | 66 | // read the file back |
| davidjhoward | 27:38205cebc3da | 67 | status = dot->readUserFile("scratch.json", (void *)scratch_buf, 1024); |
| davidjhoward | 27:38205cebc3da | 68 | if( status != true ) { |
| davidjhoward | 27:38205cebc3da | 69 | logInfo("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 70 | return false; |
| davidjhoward | 27:38205cebc3da | 71 | } |
| davidjhoward | 27:38205cebc3da | 72 | |
| davidjhoward | 27:38205cebc3da | 73 | snprintf(data_buf, sizeof(data_buf), "%s%s", scratch_buf, from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 27:38205cebc3da | 74 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf ); |
| davidjhoward | 27:38205cebc3da | 75 | |
| davidjhoward | 27:38205cebc3da | 76 | status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024); |
| davidjhoward | 27:38205cebc3da | 77 | if( status != true ) { |
| davidjhoward | 27:38205cebc3da | 78 | logInfo("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 79 | return false; |
| davidjhoward | 27:38205cebc3da | 80 | } |
| davidjhoward | 27:38205cebc3da | 81 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf)); |
| davidjhoward | 27:38205cebc3da | 82 | } else { |
| davidjhoward | 27:38205cebc3da | 83 | |
| davidjhoward | 27:38205cebc3da | 84 | logInfo("(%d)READING BACK scratch.json FILE", __LINE__); |
| davidjhoward | 27:38205cebc3da | 85 | |
| davidjhoward | 27:38205cebc3da | 86 | char scratch_buf[1024]; |
| davidjhoward | 27:38205cebc3da | 87 | char data_buf[1024]; |
| davidjhoward | 27:38205cebc3da | 88 | |
| davidjhoward | 27:38205cebc3da | 89 | // read the file back |
| davidjhoward | 27:38205cebc3da | 90 | status = dot->readUserFile("scratch.json", (void *)scratch_buf, 1024); |
| davidjhoward | 27:38205cebc3da | 91 | if( status != true ) { |
| davidjhoward | 27:38205cebc3da | 92 | logInfo("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 93 | return false; |
| davidjhoward | 27:38205cebc3da | 94 | } |
| davidjhoward | 27:38205cebc3da | 95 | |
| davidjhoward | 27:38205cebc3da | 96 | snprintf(data_buf, sizeof(data_buf), "%s%s", scratch_buf, from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 27:38205cebc3da | 97 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, data_buf ); |
| davidjhoward | 27:38205cebc3da | 98 | |
| davidjhoward | 27:38205cebc3da | 99 | status = dot->saveUserFile("scratch.json", (void *)data_buf, 1024); |
| davidjhoward | 27:38205cebc3da | 100 | if( status != true ) { |
| davidjhoward | 27:38205cebc3da | 101 | logInfo("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 102 | return false; |
| davidjhoward | 27:38205cebc3da | 103 | } |
| davidjhoward | 27:38205cebc3da | 104 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(data_buf)); |
| davidjhoward | 27:38205cebc3da | 105 | } |
| davidjhoward | 27:38205cebc3da | 106 | |
| davidjhoward | 27:38205cebc3da | 107 | return true; |
| davidjhoward | 27:38205cebc3da | 108 | } |
