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@99:55317f374a94, 2016-09-21 (annotated)
- Committer:
- davidjhoward
- Date:
- Wed Sep 21 20:32:19 2016 +0000
- Revision:
- 99:55317f374a94
- Parent:
- 49:0456ee2271be
- Child:
- 116:7337ed514891
logging changes
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 | 31:4b1587034318 | 4 | #include "global.h" |
| davidjhoward | 27:38205cebc3da | 5 | |
| davidjhoward | 49:0456ee2271be | 6 | char CloudFileReceiverWriteBuf[1024]; |
| davidjhoward | 49:0456ee2271be | 7 | char CloudFileReceiverReadBuf[1024]; |
| davidjhoward | 27:38205cebc3da | 8 | |
| davidjhoward | 49:0456ee2271be | 9 | bool StoreReceivedFile( mDot *dot, MbedJSONValue &from_json ) |
| davidjhoward | 49:0456ee2271be | 10 | { |
| davidjhoward | 99:55317f374a94 | 11 | FILENAME_STRING filename; |
| davidjhoward | 27:38205cebc3da | 12 | |
| davidjhoward | 99:55317f374a94 | 13 | memset( filename, '\0', sizeof(FILENAME_STRING) ); |
| davidjhoward | 27:38205cebc3da | 14 | |
| davidjhoward | 99:55317f374a94 | 15 | int mType = from_json["pay"]["mtype"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 16 | logInfo("mtype=%d", mType); |
| davidjhoward | 99:55317f374a94 | 17 | switch( mType ) { |
| davidjhoward | 99:55317f374a94 | 18 | case SETPOINT_CONTROL_MTYPE: { |
| davidjhoward | 99:55317f374a94 | 19 | logInfo("PROCESSING SETPONT CONTROL FILE"); |
| davidjhoward | 99:55317f374a94 | 20 | snprintf( filename, sizeof(FILENAME_STRING), "%s_%s%s", |
| davidjhoward | 99:55317f374a94 | 21 | from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 22 | from_json["pay"]["spcontrol"]["input"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 23 | ".json" ); |
| davidjhoward | 99:55317f374a94 | 24 | logInfo("SETPONT CONTROL FILENAME=%s", filename); |
| davidjhoward | 99:55317f374a94 | 25 | break; |
| davidjhoward | 99:55317f374a94 | 26 | } |
| davidjhoward | 99:55317f374a94 | 27 | case TIMER_CONTROL_MTYPE: |
| davidjhoward | 99:55317f374a94 | 28 | break; |
| davidjhoward | 99:55317f374a94 | 29 | case INPUT_CONFIG_MTYPE: { |
| davidjhoward | 99:55317f374a94 | 30 | logInfo("id=%s", from_json["pay"]["input"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 99:55317f374a94 | 31 | snprintf( filename, sizeof(FILENAME_STRING), "input_%s%s", |
| davidjhoward | 99:55317f374a94 | 32 | from_json["pay"]["input"]["id"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 33 | ".jsn" ); |
| davidjhoward | 99:55317f374a94 | 34 | logInfo("INPUT CONFIG FILENAME=%s", filename); |
| davidjhoward | 31:4b1587034318 | 35 | |
| davidjhoward | 99:55317f374a94 | 36 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 99:55317f374a94 | 37 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 99:55317f374a94 | 38 | "{ " |
| davidjhoward | 99:55317f374a94 | 39 | "\"id\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 40 | "\"name\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 41 | "\"units\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 42 | "\"min\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 43 | "\"max\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 44 | "\"node\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 45 | "\"reg\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 46 | "\"rtype\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 47 | "\"type\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 48 | "\"size\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 49 | "\"order\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 50 | "\"fmt\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 51 | "\"rfreq\":\"%s\" } ", |
| davidjhoward | 99:55317f374a94 | 52 | from_json["pay"]["input"]["id"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 53 | from_json["pay"]["input"]["name"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 54 | from_json["pay"]["input"]["units"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 55 | from_json["pay"]["input"]["min"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 56 | from_json["pay"]["input"]["max"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 57 | from_json["pay"]["input"]["node"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 58 | from_json["pay"]["input"]["reg"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 59 | from_json["pay"]["input"]["rtype"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 60 | from_json["pay"]["input"]["type"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 61 | from_json["pay"]["input"]["size"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 62 | from_json["pay"]["input"]["order"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 63 | from_json["pay"]["input"]["fmt"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 64 | from_json["pay"]["input"]["rfreq"].get<std::string>().c_str() |
| davidjhoward | 99:55317f374a94 | 65 | ); |
| davidjhoward | 45:3b9e1923cb15 | 66 | |
| davidjhoward | 99:55317f374a94 | 67 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 99:55317f374a94 | 68 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 99:55317f374a94 | 69 | if( status != true ) { |
| davidjhoward | 99:55317f374a94 | 70 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 99:55317f374a94 | 71 | } |
| davidjhoward | 45:3b9e1923cb15 | 72 | |
| davidjhoward | 99:55317f374a94 | 73 | logInfo("Sending Mail To ModbusMasterMailBox, filename=%s", filename); |
| davidjhoward | 99:55317f374a94 | 74 | Message_t *mail = ModbusMasterMailBox.alloc(); |
| davidjhoward | 99:55317f374a94 | 75 | mail->action = ACTION_READ_FILE; |
| davidjhoward | 99:55317f374a94 | 76 | strncpy( mail->controlFile, filename, (sizeof(mail->controlFile)-1)); |
| davidjhoward | 99:55317f374a94 | 77 | ModbusMasterMailBox.put(mail); |
| davidjhoward | 99:55317f374a94 | 78 | break; |
| davidjhoward | 27:38205cebc3da | 79 | } |
| davidjhoward | 99:55317f374a94 | 80 | default: |
| davidjhoward | 99:55317f374a94 | 81 | logInfo("DEFAULT"); |
| davidjhoward | 99:55317f374a94 | 82 | break; |
| davidjhoward | 99:55317f374a94 | 83 | } |
| davidjhoward | 99:55317f374a94 | 84 | return true; |
| davidjhoward | 49:0456ee2271be | 85 | } |
| davidjhoward | 27:38205cebc3da | 86 | |
| davidjhoward | 49:0456ee2271be | 87 | bool CloudFileReceiver( std::string *recv_string, mDot *dot ) |
| davidjhoward | 49:0456ee2271be | 88 | { |
| davidjhoward | 49:0456ee2271be | 89 | MbedJSONValue from_json; |
| davidjhoward | 49:0456ee2271be | 90 | bool status; |
| davidjhoward | 49:0456ee2271be | 91 | parse( from_json, recv_string->c_str() ); |
| davidjhoward | 49:0456ee2271be | 92 | int sequence = from_json["seq"].get<int>(); |
| davidjhoward | 49:0456ee2271be | 93 | |
| davidjhoward | 49:0456ee2271be | 94 | // sequence of -1 means string sent in 1 chunk. |
| davidjhoward | 49:0456ee2271be | 95 | if( sequence == -1 ) { |
| davidjhoward | 49:0456ee2271be | 96 | |
| davidjhoward | 49:0456ee2271be | 97 | return StoreReceivedFile( dot, from_json ); |
| davidjhoward | 27:38205cebc3da | 98 | |
| davidjhoward | 27:38205cebc3da | 99 | } else if( sequence == 0 ) { |
| davidjhoward | 27:38205cebc3da | 100 | |
| davidjhoward | 49:0456ee2271be | 101 | memset(CloudFileReceiverWriteBuf,0,sizeof(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 102 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", "{\"seq\":-1, \"pay\":",from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 49:0456ee2271be | 103 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 104 | |
| davidjhoward | 49:0456ee2271be | 105 | status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 106 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 107 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 108 | return false; |
| davidjhoward | 27:38205cebc3da | 109 | } |
| davidjhoward | 49:0456ee2271be | 110 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 111 | |
| davidjhoward | 27:38205cebc3da | 112 | } else if( sequence == -2 ) { |
| davidjhoward | 27:38205cebc3da | 113 | |
| davidjhoward | 49:0456ee2271be | 114 | MbedJSONValue final_json; |
| davidjhoward | 49:0456ee2271be | 115 | |
| davidjhoward | 27:38205cebc3da | 116 | logInfo("(%d)READING BACK scratch.json FILE FOR LAST PACKET", __LINE__); |
| davidjhoward | 27:38205cebc3da | 117 | |
| davidjhoward | 27:38205cebc3da | 118 | // read the file back |
| davidjhoward | 49:0456ee2271be | 119 | status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 120 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 121 | logError("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 122 | return false; |
| davidjhoward | 27:38205cebc3da | 123 | } |
| davidjhoward | 27:38205cebc3da | 124 | |
| davidjhoward | 49:0456ee2271be | 125 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str(), "}" ); |
| davidjhoward | 49:0456ee2271be | 126 | logInfo("(%d)Final String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 127 | |
| davidjhoward | 49:0456ee2271be | 128 | parse( final_json, CloudFileReceiverWriteBuf ); |
| davidjhoward | 49:0456ee2271be | 129 | |
| davidjhoward | 99:55317f374a94 | 130 | int sequence = final_json["seq"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 131 | int mType = final_json["pay"]["mtype"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 132 | std::string input = final_json["pay"]["input"]["id"].get<std::string>().c_str(); |
| davidjhoward | 99:55317f374a94 | 133 | |
| davidjhoward | 99:55317f374a94 | 134 | printf("MESSAGE TYPE=%d, input=%s\r\n",mType, input.c_str()); |
| davidjhoward | 99:55317f374a94 | 135 | |
| davidjhoward | 49:0456ee2271be | 136 | status = StoreReceivedFile( dot, final_json ); |
| davidjhoward | 27:38205cebc3da | 137 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 138 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 139 | } |
| davidjhoward | 49:0456ee2271be | 140 | |
| davidjhoward | 49:0456ee2271be | 141 | status = dot->deleteUserFile("scratch.json"); |
| davidjhoward | 49:0456ee2271be | 142 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 143 | logError("(%d)delete file failed, status=%d", __LINE__, status); |
| davidjhoward | 49:0456ee2271be | 144 | } |
| davidjhoward | 49:0456ee2271be | 145 | |
| davidjhoward | 49:0456ee2271be | 146 | logInfo("(%d)DELETED scratch.json FILE, status:%d", __LINE__, status ); |
| davidjhoward | 49:0456ee2271be | 147 | |
| davidjhoward | 27:38205cebc3da | 148 | } else { |
| davidjhoward | 27:38205cebc3da | 149 | |
| davidjhoward | 27:38205cebc3da | 150 | logInfo("(%d)READING BACK scratch.json FILE", __LINE__); |
| davidjhoward | 27:38205cebc3da | 151 | |
| davidjhoward | 27:38205cebc3da | 152 | // read the file back |
| davidjhoward | 49:0456ee2271be | 153 | status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 154 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 155 | logError("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 156 | return false; |
| davidjhoward | 27:38205cebc3da | 157 | } |
| davidjhoward | 27:38205cebc3da | 158 | |
| davidjhoward | 49:0456ee2271be | 159 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 49:0456ee2271be | 160 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 161 | |
| davidjhoward | 49:0456ee2271be | 162 | status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 163 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 164 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 165 | return false; |
| davidjhoward | 27:38205cebc3da | 166 | } |
| davidjhoward | 49:0456ee2271be | 167 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 168 | |
| davidjhoward | 27:38205cebc3da | 169 | } |
| davidjhoward | 27:38205cebc3da | 170 | |
| davidjhoward | 27:38205cebc3da | 171 | return true; |
| davidjhoward | 27:38205cebc3da | 172 | } |
