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.
ICE-Application/src/CloudDataHandler/CloudFileReceiver.cpp@0:61364762ee0e, 2017-01-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Jan 24 19:05:33 2017 +0000
- Revision:
- 0:61364762ee0e
Port from IAR to Nucleo-F412 board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmarkel44 | 0:61364762ee0e | 1 | #include "CloudFileReceiver.h" |
jmarkel44 | 0:61364762ee0e | 2 | #include "CloudDataHandler.h" |
jmarkel44 | 0:61364762ee0e | 3 | #include "mbed.h" |
jmarkel44 | 0:61364762ee0e | 4 | #include "global.h" |
jmarkel44 | 0:61364762ee0e | 5 | #include "cJSON.h" |
jmarkel44 | 0:61364762ee0e | 6 | #include <algorithm> |
jmarkel44 | 0:61364762ee0e | 7 | #include <string> |
jmarkel44 | 0:61364762ee0e | 8 | #include "stdio.h" |
jmarkel44 | 0:61364762ee0e | 9 | |
jmarkel44 | 0:61364762ee0e | 10 | char CloudFileReceiverWriteBuf[MAX_FILE_SIZE]; |
jmarkel44 | 0:61364762ee0e | 11 | char CloudFileReceiverReadBuf[MAX_FILE_SIZE]; |
jmarkel44 | 0:61364762ee0e | 12 | |
jmarkel44 | 0:61364762ee0e | 13 | bool StoreReceivedFile( std::string &payload_string ) |
jmarkel44 | 0:61364762ee0e | 14 | { |
jmarkel44 | 0:61364762ee0e | 15 | FILENAME_STRING filename; |
jmarkel44 | 0:61364762ee0e | 16 | |
jmarkel44 | 0:61364762ee0e | 17 | memset( filename, '\0', sizeof(FILENAME_STRING) ); |
jmarkel44 | 0:61364762ee0e | 18 | |
jmarkel44 | 0:61364762ee0e | 19 | cJSON * root = cJSON_Parse(payload_string.c_str()); |
jmarkel44 | 0:61364762ee0e | 20 | int mType = cJSON_GetObjectItem(root,"mtype")->valueint; |
jmarkel44 | 0:61364762ee0e | 21 | // printf("%s:%d: mtype=%d\r\n", __func__,__LINE__,mType); |
jmarkel44 | 0:61364762ee0e | 22 | switch( mType ) { |
jmarkel44 | 0:61364762ee0e | 23 | case SETPOINT_CONTROL_MTYPE: |
jmarkel44 | 0:61364762ee0e | 24 | case MANUAL_CONTROL_MTYPE: |
jmarkel44 | 0:61364762ee0e | 25 | case TIMER_CONTROL_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 26 | |
jmarkel44 | 0:61364762ee0e | 27 | std::string control_json; |
jmarkel44 | 0:61364762ee0e | 28 | ConfigMessage_t *mail = ConfigHandlerMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 29 | memset(mail, 0, sizeof(ConfigMessage_t)); |
jmarkel44 | 0:61364762ee0e | 30 | mail->action = ACTION_CREATE; |
jmarkel44 | 0:61364762ee0e | 31 | |
jmarkel44 | 0:61364762ee0e | 32 | switch( mType ) { |
jmarkel44 | 0:61364762ee0e | 33 | case SETPOINT_CONTROL_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 34 | cJSON * spcontrol = cJSON_GetObjectItem(root,"spcontrol"); |
jmarkel44 | 0:61364762ee0e | 35 | snprintf( filename, sizeof(FILENAME_STRING), "control_sp_%s.json", cJSON_GetObjectItem(spcontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 36 | control_json = cJSON_PrintUnformatted(spcontrol); |
jmarkel44 | 0:61364762ee0e | 37 | mail->control = CONTROL_SETPOINT; |
jmarkel44 | 0:61364762ee0e | 38 | break; |
jmarkel44 | 0:61364762ee0e | 39 | } |
jmarkel44 | 0:61364762ee0e | 40 | case MANUAL_CONTROL_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 41 | cJSON * mncontrol = cJSON_GetObjectItem(root,"mncontrol"); |
jmarkel44 | 0:61364762ee0e | 42 | snprintf( filename, sizeof(FILENAME_STRING), "control_mn_%s.json", cJSON_GetObjectItem(mncontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 43 | control_json = cJSON_PrintUnformatted(mncontrol); |
jmarkel44 | 0:61364762ee0e | 44 | mail->control = CONTROL_MANUAL; |
jmarkel44 | 0:61364762ee0e | 45 | break; |
jmarkel44 | 0:61364762ee0e | 46 | } |
jmarkel44 | 0:61364762ee0e | 47 | case TIMER_CONTROL_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 48 | cJSON * tmcontrol = cJSON_GetObjectItem(root,"tmcontrol"); |
jmarkel44 | 0:61364762ee0e | 49 | snprintf( filename, sizeof(FILENAME_STRING), "control_tm_%s.json", cJSON_GetObjectItem(tmcontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 50 | std::string control_str = cJSON_PrintUnformatted(tmcontrol); |
jmarkel44 | 0:61364762ee0e | 51 | mail->control = CONTROL_TIMER; |
jmarkel44 | 0:61364762ee0e | 52 | break; |
jmarkel44 | 0:61364762ee0e | 53 | } |
jmarkel44 | 0:61364762ee0e | 54 | default: { |
jmarkel44 | 0:61364762ee0e | 55 | printf("%s:%d: Unknown message type: %d\r\n",__func__,__LINE__,mType); |
jmarkel44 | 0:61364762ee0e | 56 | cJSON_Delete(root); |
jmarkel44 | 0:61364762ee0e | 57 | return false; |
jmarkel44 | 0:61364762ee0e | 58 | } |
jmarkel44 | 0:61364762ee0e | 59 | } |
jmarkel44 | 0:61364762ee0e | 60 | |
jmarkel44 | 0:61364762ee0e | 61 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)control_json.c_str(), MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 62 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 63 | printf("(%d)save file failed, status=%d\r\n", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 64 | break; |
jmarkel44 | 0:61364762ee0e | 65 | } |
jmarkel44 | 0:61364762ee0e | 66 | |
jmarkel44 | 0:61364762ee0e | 67 | strncpy(mail->controlFile, filename, sizeof(mail->controlFile)-1); |
jmarkel44 | 0:61364762ee0e | 68 | ConfigHandlerMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 69 | |
jmarkel44 | 0:61364762ee0e | 70 | printf("%s:%d: Control JSON: %s\r\n", __func__,__LINE__, control_json.c_str() ); |
jmarkel44 | 0:61364762ee0e | 71 | printf("%s:%d: Sending a create request for control %s type = %u\r\n", __func__,__LINE__, mail->controlFile, mail->control); |
jmarkel44 | 0:61364762ee0e | 72 | break; |
jmarkel44 | 0:61364762ee0e | 73 | } |
jmarkel44 | 0:61364762ee0e | 74 | case INPUT_CONFIG_MTYPE: |
jmarkel44 | 0:61364762ee0e | 75 | case VINPUT_CONFIG_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 76 | |
jmarkel44 | 0:61364762ee0e | 77 | cJSON * input = cJSON_GetObjectItem(root,"input"); |
jmarkel44 | 0:61364762ee0e | 78 | if( mType == INPUT_CONFIG_MTYPE ) { |
jmarkel44 | 0:61364762ee0e | 79 | snprintf( filename, sizeof(FILENAME_STRING), "input_%s%s", cJSON_GetObjectItem(input,"id")->valuestring, ".json" ); |
jmarkel44 | 0:61364762ee0e | 80 | } else { |
jmarkel44 | 0:61364762ee0e | 81 | snprintf( filename, sizeof(FILENAME_STRING), "vinput_%s%s", cJSON_GetObjectItem(input,"id")->valuestring, ".json" ); |
jmarkel44 | 0:61364762ee0e | 82 | } |
jmarkel44 | 0:61364762ee0e | 83 | std::string input_str = cJSON_PrintUnformatted(input); |
jmarkel44 | 0:61364762ee0e | 84 | |
jmarkel44 | 0:61364762ee0e | 85 | |
jmarkel44 | 0:61364762ee0e | 86 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)input_str.c_str(), MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 87 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 88 | printf("(%d)save file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 89 | break; |
jmarkel44 | 0:61364762ee0e | 90 | } |
jmarkel44 | 0:61364762ee0e | 91 | |
jmarkel44 | 0:61364762ee0e | 92 | ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 93 | mail->action = ACTION_READ_FILE; |
jmarkel44 | 0:61364762ee0e | 94 | strncpy( mail->msg, filename, (sizeof(mail->msg)-1)); |
jmarkel44 | 0:61364762ee0e | 95 | ModbusMasterMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 96 | |
jmarkel44 | 0:61364762ee0e | 97 | printf("%s:%d: INPUT JSON: %s\r\n", __func__,__LINE__, input_str.c_str() ); |
jmarkel44 | 0:61364762ee0e | 98 | printf("%s:%d: Sending New INPUT to ModbusMasterMailBox, filename=%s\r\n", __func__,__LINE__, filename); |
jmarkel44 | 0:61364762ee0e | 99 | break; |
jmarkel44 | 0:61364762ee0e | 100 | } |
jmarkel44 | 0:61364762ee0e | 101 | case OUTPUT_CONFIG_MTYPE: |
jmarkel44 | 0:61364762ee0e | 102 | case VOUTPUT_CONFIG_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 103 | |
jmarkel44 | 0:61364762ee0e | 104 | cJSON * output = cJSON_GetObjectItem(root,"output"); |
jmarkel44 | 0:61364762ee0e | 105 | if( mType == OUTPUT_CONFIG_MTYPE ) { |
jmarkel44 | 0:61364762ee0e | 106 | snprintf( filename, sizeof(FILENAME_STRING), "output_%s%s", cJSON_GetObjectItem(output,"id")->valuestring, ".json" ); |
jmarkel44 | 0:61364762ee0e | 107 | } else { |
jmarkel44 | 0:61364762ee0e | 108 | snprintf( filename, sizeof(FILENAME_STRING), "voutput_%s%s", cJSON_GetObjectItem(output,"id")->valuestring, ".json" ); |
jmarkel44 | 0:61364762ee0e | 109 | } |
jmarkel44 | 0:61364762ee0e | 110 | std::string output_str = cJSON_PrintUnformatted(output); |
jmarkel44 | 0:61364762ee0e | 111 | |
jmarkel44 | 0:61364762ee0e | 112 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)output_str.c_str(), MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 113 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 114 | printf("(%d)save file failed, status=%d\r\n", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 115 | break; |
jmarkel44 | 0:61364762ee0e | 116 | } |
jmarkel44 | 0:61364762ee0e | 117 | |
jmarkel44 | 0:61364762ee0e | 118 | // send a message to the modbus master |
jmarkel44 | 0:61364762ee0e | 119 | ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 120 | mail->action = ACTION_READ_FILE; |
jmarkel44 | 0:61364762ee0e | 121 | strncpy( mail->msg, filename, (sizeof(mail->msg)-1)); |
jmarkel44 | 0:61364762ee0e | 122 | ModbusMasterMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 123 | |
jmarkel44 | 0:61364762ee0e | 124 | // send a message to the output master |
jmarkel44 | 0:61364762ee0e | 125 | OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 126 | output_mail->action = ACTION_NEW; |
jmarkel44 | 0:61364762ee0e | 127 | strncpy(output_mail->controlFile, filename, sizeof(output_mail->controlFile)-1); |
jmarkel44 | 0:61364762ee0e | 128 | OutputMasterMailBox.put(output_mail); |
jmarkel44 | 0:61364762ee0e | 129 | |
jmarkel44 | 0:61364762ee0e | 130 | printf("%s:%d: OUTPUT JSON: %s\r\n", __func__,__LINE__, output_str.c_str() ); |
jmarkel44 | 0:61364762ee0e | 131 | printf("%s:%d: Sending New OUTPUT to ModbusMaster and OutputMaster, filename=%s\r\n", __func__,__LINE__, filename); |
jmarkel44 | 0:61364762ee0e | 132 | |
jmarkel44 | 0:61364762ee0e | 133 | break; |
jmarkel44 | 0:61364762ee0e | 134 | } |
jmarkel44 | 0:61364762ee0e | 135 | case HOLDING_CONFIG_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 136 | |
jmarkel44 | 0:61364762ee0e | 137 | cJSON * holding = cJSON_GetObjectItem(root,"holding"); |
jmarkel44 | 0:61364762ee0e | 138 | snprintf( filename, sizeof(FILENAME_STRING), "hold_%s%s", cJSON_GetObjectItem(holding,"id")->valuestring, ".json" ); |
jmarkel44 | 0:61364762ee0e | 139 | std::string holding_str = cJSON_PrintUnformatted(holding); |
jmarkel44 | 0:61364762ee0e | 140 | |
jmarkel44 | 0:61364762ee0e | 141 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)holding_str.c_str(), MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 142 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 143 | printf("(%d)save file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 144 | break; |
jmarkel44 | 0:61364762ee0e | 145 | } |
jmarkel44 | 0:61364762ee0e | 146 | |
jmarkel44 | 0:61364762ee0e | 147 | ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 148 | mail->action = ACTION_READ_FILE; |
jmarkel44 | 0:61364762ee0e | 149 | strncpy( mail->msg, filename, (sizeof(mail->msg)-1)); |
jmarkel44 | 0:61364762ee0e | 150 | ModbusMasterMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 151 | |
jmarkel44 | 0:61364762ee0e | 152 | printf("%s:%d: HOLDING JSON: %s\r\n", __func__,__LINE__, holding_str.c_str() ); |
jmarkel44 | 0:61364762ee0e | 153 | printf("%s:%d: Sending New HOLDING REGISTER to ModbusMasterMailBox, filename=%s\r\n", __func__,__LINE__, filename); |
jmarkel44 | 0:61364762ee0e | 154 | break; |
jmarkel44 | 0:61364762ee0e | 155 | } |
jmarkel44 | 0:61364762ee0e | 156 | case DESTROY_SETPOINT_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 157 | |
jmarkel44 | 0:61364762ee0e | 158 | cJSON * spcontrol = cJSON_GetObjectItem(root,"spcontrol"); |
jmarkel44 | 0:61364762ee0e | 159 | snprintf( filename, sizeof(FILENAME_STRING), "control_sp_%s.json", cJSON_GetObjectItem(spcontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 160 | |
jmarkel44 | 0:61364762ee0e | 161 | // send a message to the configuration handler to create the control |
jmarkel44 | 0:61364762ee0e | 162 | ConfigMessage_t *msg = ConfigHandlerMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 163 | memset(msg, 0, sizeof(ConfigMessage_t)); |
jmarkel44 | 0:61364762ee0e | 164 | msg->action = ACTION_DESTROY; |
jmarkel44 | 0:61364762ee0e | 165 | msg->control = CONTROL_SETPOINT; |
jmarkel44 | 0:61364762ee0e | 166 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
jmarkel44 | 0:61364762ee0e | 167 | |
jmarkel44 | 0:61364762ee0e | 168 | printf("%s:%d: Sending a destroy request for setpoint control %s type = %u\r\n", __func__,__LINE__, msg->controlFile, msg->control); |
jmarkel44 | 0:61364762ee0e | 169 | |
jmarkel44 | 0:61364762ee0e | 170 | ConfigHandlerMailBox.put(msg); |
jmarkel44 | 0:61364762ee0e | 171 | break; |
jmarkel44 | 0:61364762ee0e | 172 | } |
jmarkel44 | 0:61364762ee0e | 173 | case DESTROY_MANUAL_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 174 | |
jmarkel44 | 0:61364762ee0e | 175 | cJSON * mncontrol = cJSON_GetObjectItem(root,"mncontrol"); |
jmarkel44 | 0:61364762ee0e | 176 | snprintf( filename, sizeof(FILENAME_STRING), "control_mn_%s.json", cJSON_GetObjectItem(mncontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 177 | |
jmarkel44 | 0:61364762ee0e | 178 | // send a message to the configuration handler to create the control |
jmarkel44 | 0:61364762ee0e | 179 | ConfigMessage_t *msg = ConfigHandlerMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 180 | memset(msg, 0, sizeof(ConfigMessage_t)); |
jmarkel44 | 0:61364762ee0e | 181 | msg->action = ACTION_DESTROY; |
jmarkel44 | 0:61364762ee0e | 182 | msg->control = CONTROL_MANUAL; |
jmarkel44 | 0:61364762ee0e | 183 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
jmarkel44 | 0:61364762ee0e | 184 | |
jmarkel44 | 0:61364762ee0e | 185 | printf("%s:%d: Sending a destroy request for manual control %s type = %u\r\n", __func__,__LINE__, msg->controlFile, msg->control); |
jmarkel44 | 0:61364762ee0e | 186 | |
jmarkel44 | 0:61364762ee0e | 187 | ConfigHandlerMailBox.put(msg); |
jmarkel44 | 0:61364762ee0e | 188 | break; |
jmarkel44 | 0:61364762ee0e | 189 | } |
jmarkel44 | 0:61364762ee0e | 190 | case DESTROY_TIMER_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 191 | |
jmarkel44 | 0:61364762ee0e | 192 | cJSON * tmcontrol = cJSON_GetObjectItem(root,"tmcontrol"); |
jmarkel44 | 0:61364762ee0e | 193 | snprintf( filename, sizeof(FILENAME_STRING), "control_tm_%s.json", cJSON_GetObjectItem(tmcontrol,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 194 | |
jmarkel44 | 0:61364762ee0e | 195 | ConfigMessage_t *msg = ConfigHandlerMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 196 | memset(msg, 0, sizeof(ConfigMessage_t)); |
jmarkel44 | 0:61364762ee0e | 197 | msg->action = ACTION_DESTROY; |
jmarkel44 | 0:61364762ee0e | 198 | msg->control = CONTROL_TIMER; |
jmarkel44 | 0:61364762ee0e | 199 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
jmarkel44 | 0:61364762ee0e | 200 | |
jmarkel44 | 0:61364762ee0e | 201 | printf("%s:%d: Sending a destroy request for timer control %s type = %u\r\n", __func__,__LINE__, msg->controlFile, msg->control); |
jmarkel44 | 0:61364762ee0e | 202 | |
jmarkel44 | 0:61364762ee0e | 203 | ConfigHandlerMailBox.put(msg); |
jmarkel44 | 0:61364762ee0e | 204 | break; |
jmarkel44 | 0:61364762ee0e | 205 | } |
jmarkel44 | 0:61364762ee0e | 206 | case VIRTUAL_COMMAND_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 207 | |
jmarkel44 | 0:61364762ee0e | 208 | cJSON * command = cJSON_GetObjectItem(root,"command"); |
jmarkel44 | 0:61364762ee0e | 209 | snprintf( filename, sizeof(FILENAME_STRING), "cmd_%s.json", cJSON_GetObjectItem(command,"id")->valuestring ); |
jmarkel44 | 0:61364762ee0e | 210 | std::string command_json = cJSON_PrintUnformatted(command); |
jmarkel44 | 0:61364762ee0e | 211 | |
jmarkel44 | 0:61364762ee0e | 212 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)command_json.c_str(), MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 213 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 214 | printf("(%d)save file failed, status=%d\r\n", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 215 | break; |
jmarkel44 | 0:61364762ee0e | 216 | } |
jmarkel44 | 0:61364762ee0e | 217 | |
jmarkel44 | 0:61364762ee0e | 218 | ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 219 | mail->action = ACTION_READ_FILE; |
jmarkel44 | 0:61364762ee0e | 220 | strncpy( mail->msg, filename, (sizeof(mail->msg)-1)); |
jmarkel44 | 0:61364762ee0e | 221 | ModbusMasterMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 222 | |
jmarkel44 | 0:61364762ee0e | 223 | printf("%s:%d: COMMAND: %s\r\n", __func__,__LINE__, command_json.c_str() ); |
jmarkel44 | 0:61364762ee0e | 224 | printf("%s:%d: Sending a create command requst to ModbusMaster %s type = %u\r\n", __func__,__LINE__, mail->msg, mail->control); |
jmarkel44 | 0:61364762ee0e | 225 | |
jmarkel44 | 0:61364762ee0e | 226 | break; |
jmarkel44 | 0:61364762ee0e | 227 | } |
jmarkel44 | 0:61364762ee0e | 228 | case BT_MODBUS_HOLD_COMMAND_MTYPE: { |
jmarkel44 | 0:61364762ee0e | 229 | |
jmarkel44 | 0:61364762ee0e | 230 | cJSON * mbcommand = cJSON_GetObjectItem(root,"mbcommand"); |
jmarkel44 | 0:61364762ee0e | 231 | std::string mbcommand_json = cJSON_PrintUnformatted(mbcommand); |
jmarkel44 | 0:61364762ee0e | 232 | ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc(); |
jmarkel44 | 0:61364762ee0e | 233 | mail->action = ACTION_EXEC_CMD; |
jmarkel44 | 0:61364762ee0e | 234 | mail->replyThread = CLOUD_DATA_HANDLER; |
jmarkel44 | 0:61364762ee0e | 235 | strncpy( mail->msg, mbcommand_json.c_str(), (sizeof(mail->msg)-1)); |
jmarkel44 | 0:61364762ee0e | 236 | ModbusMasterMailBox.put(mail); |
jmarkel44 | 0:61364762ee0e | 237 | |
jmarkel44 | 0:61364762ee0e | 238 | printf("%s:%d: MODBUS Command: %s\r\n", __func__,__LINE__, mbcommand_json.c_str()); |
jmarkel44 | 0:61364762ee0e | 239 | |
jmarkel44 | 0:61364762ee0e | 240 | break; |
jmarkel44 | 0:61364762ee0e | 241 | } |
jmarkel44 | 0:61364762ee0e | 242 | default: |
jmarkel44 | 0:61364762ee0e | 243 | // printf("%s:%d: DEFAULT\r\n", __func__,__LINE__); |
jmarkel44 | 0:61364762ee0e | 244 | break; |
jmarkel44 | 0:61364762ee0e | 245 | } |
jmarkel44 | 0:61364762ee0e | 246 | cJSON_Delete(root); |
jmarkel44 | 0:61364762ee0e | 247 | return true; |
jmarkel44 | 0:61364762ee0e | 248 | } |
jmarkel44 | 0:61364762ee0e | 249 | |
jmarkel44 | 0:61364762ee0e | 250 | bool CloudDataHandler_RcvFile = false; |
jmarkel44 | 0:61364762ee0e | 251 | bool CloudFileReceiver( std::string *recv_string ) |
jmarkel44 | 0:61364762ee0e | 252 | { |
jmarkel44 | 0:61364762ee0e | 253 | bool status; |
jmarkel44 | 0:61364762ee0e | 254 | CloudDataHandler_RcvFile = false; |
jmarkel44 | 0:61364762ee0e | 255 | int sequence; |
jmarkel44 | 0:61364762ee0e | 256 | std::string seq_str = recv_string->c_str(); |
jmarkel44 | 0:61364762ee0e | 257 | std::string remove_seq = "{\"seq\":"; |
jmarkel44 | 0:61364762ee0e | 258 | std::string::size_type i = seq_str.find(remove_seq); |
jmarkel44 | 0:61364762ee0e | 259 | if (i != std::string::npos) { |
jmarkel44 | 0:61364762ee0e | 260 | seq_str.erase(i, remove_seq.length()); |
jmarkel44 | 0:61364762ee0e | 261 | std::string remove_comma = ","; |
jmarkel44 | 0:61364762ee0e | 262 | i = seq_str.find(remove_comma); |
jmarkel44 | 0:61364762ee0e | 263 | if (i != std::string::npos) { |
jmarkel44 | 0:61364762ee0e | 264 | seq_str.erase(i, (seq_str.length()-i)); |
jmarkel44 | 0:61364762ee0e | 265 | sequence = atoi(seq_str.c_str()); |
jmarkel44 | 0:61364762ee0e | 266 | } |
jmarkel44 | 0:61364762ee0e | 267 | } |
jmarkel44 | 0:61364762ee0e | 268 | std::string payload_string = recv_string->c_str(); |
jmarkel44 | 0:61364762ee0e | 269 | std::string extract_pay = "\"pay\":"; |
jmarkel44 | 0:61364762ee0e | 270 | i = payload_string.find(extract_pay); |
jmarkel44 | 0:61364762ee0e | 271 | if (i != std::string::npos) { |
jmarkel44 | 0:61364762ee0e | 272 | i = i + extract_pay.length(); |
jmarkel44 | 0:61364762ee0e | 273 | payload_string.erase(0, i); |
jmarkel44 | 0:61364762ee0e | 274 | payload_string.erase((payload_string.length()-1), 1); |
jmarkel44 | 0:61364762ee0e | 275 | if( sequence != -1 ) { |
jmarkel44 | 0:61364762ee0e | 276 | payload_string = payload_string.substr(1, payload_string.size() - 2); |
jmarkel44 | 0:61364762ee0e | 277 | } |
jmarkel44 | 0:61364762ee0e | 278 | } |
jmarkel44 | 0:61364762ee0e | 279 | |
jmarkel44 | 0:61364762ee0e | 280 | printf("sequence=%d, payload_string:%s", sequence, payload_string.c_str() ); |
jmarkel44 | 0:61364762ee0e | 281 | |
jmarkel44 | 0:61364762ee0e | 282 | CloudDataHandler_RcvFile = false; |
jmarkel44 | 0:61364762ee0e | 283 | if( sequence == -1 ) { |
jmarkel44 | 0:61364762ee0e | 284 | |
jmarkel44 | 0:61364762ee0e | 285 | // sequence of -1 means string sent in 1 chunk. |
jmarkel44 | 0:61364762ee0e | 286 | // printf("%s:%d: payload=%s\r\n",__func__,__LINE__, payload_string.c_str() ); |
jmarkel44 | 0:61364762ee0e | 287 | status = StoreReceivedFile( payload_string ); |
jmarkel44 | 0:61364762ee0e | 288 | |
jmarkel44 | 0:61364762ee0e | 289 | } else if( sequence == 0 ) { |
jmarkel44 | 0:61364762ee0e | 290 | |
jmarkel44 | 0:61364762ee0e | 291 | memset(CloudFileReceiverWriteBuf,0,sizeof(CloudFileReceiverWriteBuf)); |
jmarkel44 | 0:61364762ee0e | 292 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s", payload_string.c_str() ); |
jmarkel44 | 0:61364762ee0e | 293 | |
jmarkel44 | 0:61364762ee0e | 294 | printf("(%d)Writing String Length=%d, %s", __LINE__, MAX_FILE_SIZE, CloudFileReceiverWriteBuf ); |
jmarkel44 | 0:61364762ee0e | 295 | |
jmarkel44 | 0:61364762ee0e | 296 | status = GLOBAL_mdot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 297 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 298 | printf("(%d)save file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 299 | } else { |
jmarkel44 | 0:61364762ee0e | 300 | CloudDataHandler_RcvFile = true; |
jmarkel44 | 0:61364762ee0e | 301 | printf("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
jmarkel44 | 0:61364762ee0e | 302 | } |
jmarkel44 | 0:61364762ee0e | 303 | |
jmarkel44 | 0:61364762ee0e | 304 | } else if( sequence == -2 ) { |
jmarkel44 | 0:61364762ee0e | 305 | |
jmarkel44 | 0:61364762ee0e | 306 | printf("(%d)READING BACK scratch.json FILE FOR LAST PACKET", __LINE__); |
jmarkel44 | 0:61364762ee0e | 307 | |
jmarkel44 | 0:61364762ee0e | 308 | // read the file back |
jmarkel44 | 0:61364762ee0e | 309 | status = GLOBAL_mdot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 310 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 311 | printf("(%d)read file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 312 | return false; |
jmarkel44 | 0:61364762ee0e | 313 | } |
jmarkel44 | 0:61364762ee0e | 314 | |
jmarkel44 | 0:61364762ee0e | 315 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", CloudFileReceiverReadBuf, payload_string.c_str() ); |
jmarkel44 | 0:61364762ee0e | 316 | printf("(%d)Final String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
jmarkel44 | 0:61364762ee0e | 317 | |
jmarkel44 | 0:61364762ee0e | 318 | std::string final_json = CloudFileReceiverWriteBuf; |
jmarkel44 | 0:61364762ee0e | 319 | printf("%s:%d: finished parse: %s\r\n", __func__,__LINE__,final_json.c_str() ); |
jmarkel44 | 0:61364762ee0e | 320 | |
jmarkel44 | 0:61364762ee0e | 321 | status = StoreReceivedFile( final_json ); |
jmarkel44 | 0:61364762ee0e | 322 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 323 | printf("(%d)save file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 324 | } |
jmarkel44 | 0:61364762ee0e | 325 | |
jmarkel44 | 0:61364762ee0e | 326 | status = GLOBAL_mdot->deleteUserFile("scratch.json"); |
jmarkel44 | 0:61364762ee0e | 327 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 328 | printf("(%d)delete file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 329 | } |
jmarkel44 | 0:61364762ee0e | 330 | |
jmarkel44 | 0:61364762ee0e | 331 | printf("(%d)DELETED scratch.json FILE, status:%d", __LINE__, status ); |
jmarkel44 | 0:61364762ee0e | 332 | |
jmarkel44 | 0:61364762ee0e | 333 | } else { |
jmarkel44 | 0:61364762ee0e | 334 | |
jmarkel44 | 0:61364762ee0e | 335 | printf("(%d)READING BACK scratch.json FILE", __LINE__); |
jmarkel44 | 0:61364762ee0e | 336 | |
jmarkel44 | 0:61364762ee0e | 337 | // read the file back |
jmarkel44 | 0:61364762ee0e | 338 | status = GLOBAL_mdot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 339 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 340 | printf("(%d)read file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 341 | return false; |
jmarkel44 | 0:61364762ee0e | 342 | } |
jmarkel44 | 0:61364762ee0e | 343 | |
jmarkel44 | 0:61364762ee0e | 344 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", CloudFileReceiverReadBuf, payload_string.c_str() ); |
jmarkel44 | 0:61364762ee0e | 345 | printf("(%d)Writing String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
jmarkel44 | 0:61364762ee0e | 346 | |
jmarkel44 | 0:61364762ee0e | 347 | status = GLOBAL_mdot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, MAX_FILE_SIZE); |
jmarkel44 | 0:61364762ee0e | 348 | if( status != true ) { |
jmarkel44 | 0:61364762ee0e | 349 | printf("(%d)save file failed, status=%d", __LINE__, status); |
jmarkel44 | 0:61364762ee0e | 350 | } else { |
jmarkel44 | 0:61364762ee0e | 351 | CloudDataHandler_RcvFile = true; |
jmarkel44 | 0:61364762ee0e | 352 | printf("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
jmarkel44 | 0:61364762ee0e | 353 | } |
jmarkel44 | 0:61364762ee0e | 354 | } |
jmarkel44 | 0:61364762ee0e | 355 | return true; |
jmarkel44 | 0:61364762ee0e | 356 | } |