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@147:105bcf234f81, 2016-09-28 (annotated)
- Committer:
- davidjhoward
- Date:
- Wed Sep 28 12:34:34 2016 +0000
- Revision:
- 147:105bcf234f81
- Parent:
- 145:c1dfbb5eed0e
- Child:
- 149:950c90425f7c
clean up destroy command from cloud
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| davidjhoward | 27:38205cebc3da | 1 | #include "CloudFileReceiver.h" |
| davidjhoward | 116:7337ed514891 | 2 | #include "CloudDataHandler.h" |
| davidjhoward | 27:38205cebc3da | 3 | #include "MTSLog.h" |
| davidjhoward | 27:38205cebc3da | 4 | #include "MbedJSONValue.h" |
| davidjhoward | 31:4b1587034318 | 5 | #include "global.h" |
| davidjhoward | 27:38205cebc3da | 6 | |
| davidjhoward | 49:0456ee2271be | 7 | char CloudFileReceiverWriteBuf[1024]; |
| davidjhoward | 49:0456ee2271be | 8 | char CloudFileReceiverReadBuf[1024]; |
| davidjhoward | 27:38205cebc3da | 9 | |
| davidjhoward | 49:0456ee2271be | 10 | bool StoreReceivedFile( mDot *dot, MbedJSONValue &from_json ) |
| davidjhoward | 49:0456ee2271be | 11 | { |
| davidjhoward | 99:55317f374a94 | 12 | FILENAME_STRING filename; |
| davidjhoward | 27:38205cebc3da | 13 | |
| davidjhoward | 99:55317f374a94 | 14 | memset( filename, '\0', sizeof(FILENAME_STRING) ); |
| davidjhoward | 27:38205cebc3da | 15 | |
| davidjhoward | 99:55317f374a94 | 16 | int mType = from_json["pay"]["mtype"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 17 | logInfo("mtype=%d", mType); |
| davidjhoward | 99:55317f374a94 | 18 | switch( mType ) { |
| davidjhoward | 99:55317f374a94 | 19 | case SETPOINT_CONTROL_MTYPE: { |
| davidjhoward | 99:55317f374a94 | 20 | logInfo("PROCESSING SETPONT CONTROL FILE"); |
| davidjhoward | 134:f90154ff33ec | 21 | snprintf( filename, sizeof(FILENAME_STRING), "control_sp_%s.json", |
| davidjhoward | 134:f90154ff33ec | 22 | from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 99:55317f374a94 | 23 | logInfo("SETPONT CONTROL FILENAME=%s", filename); |
| davidjhoward | 134:f90154ff33ec | 24 | |
| davidjhoward | 134:f90154ff33ec | 25 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 134:f90154ff33ec | 26 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 134:f90154ff33ec | 27 | "{ " |
| davidjhoward | 134:f90154ff33ec | 28 | "\"id\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 29 | "\"priority\": \"%s\"," |
| davidjhoward | 134:f90154ff33ec | 30 | "\"input\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 31 | "\"output\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 32 | "\"setpoint\": \"%s\"," |
| davidjhoward | 134:f90154ff33ec | 33 | "\"prodfact\": \"%s\"," |
| davidjhoward | 134:f90154ff33ec | 34 | "\"actingDir\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 35 | "\"halert\": \"%s\"," |
| davidjhoward | 134:f90154ff33ec | 36 | "\"lalert\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 37 | "\"hfs\": \"%s\"," |
| davidjhoward | 134:f90154ff33ec | 38 | "\"lfs\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 39 | "\"tol\": \"%s\" }", |
| davidjhoward | 134:f90154ff33ec | 40 | from_json["pay"]["spcontrol"]["id"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 41 | from_json["pay"]["spcontrol"]["priority"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 42 | from_json["pay"]["spcontrol"]["input"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 43 | from_json["pay"]["spcontrol"]["output"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 44 | from_json["pay"]["spcontrol"]["setpoint"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 45 | from_json["pay"]["spcontrol"]["prodfact"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 46 | from_json["pay"]["spcontrol"]["actingDir"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 47 | from_json["pay"]["spcontrol"]["halert"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 48 | from_json["pay"]["spcontrol"]["lalert"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 49 | from_json["pay"]["spcontrol"]["hfs"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 50 | from_json["pay"]["spcontrol"]["lfs"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 51 | from_json["pay"]["spcontrol"]["tol"].get<std::string>().c_str() |
| davidjhoward | 134:f90154ff33ec | 52 | ); |
| davidjhoward | 134:f90154ff33ec | 53 | |
| davidjhoward | 134:f90154ff33ec | 54 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 134:f90154ff33ec | 55 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 134:f90154ff33ec | 56 | if( status != true ) { |
| davidjhoward | 134:f90154ff33ec | 57 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 134:f90154ff33ec | 58 | } |
| davidjhoward | 134:f90154ff33ec | 59 | |
| davidjhoward | 134:f90154ff33ec | 60 | // send a message to the configuration handler to create the control |
| davidjhoward | 134:f90154ff33ec | 61 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 134:f90154ff33ec | 62 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 134:f90154ff33ec | 63 | msg->action = ACTION_CREATE; |
| davidjhoward | 134:f90154ff33ec | 64 | msg->control = CONTROL_SETPOINT; |
| davidjhoward | 134:f90154ff33ec | 65 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 134:f90154ff33ec | 66 | |
| davidjhoward | 134:f90154ff33ec | 67 | printf("%s: Sending a create request for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 134:f90154ff33ec | 68 | |
| davidjhoward | 134:f90154ff33ec | 69 | MailBox.put(msg); |
| davidjhoward | 134:f90154ff33ec | 70 | break; |
| davidjhoward | 134:f90154ff33ec | 71 | } |
| davidjhoward | 145:c1dfbb5eed0e | 72 | case MANUAL_CONTROL_MTYPE: { |
| davidjhoward | 134:f90154ff33ec | 73 | logInfo("PROCESSING MANUAL CONTROL FILE"); |
| davidjhoward | 134:f90154ff33ec | 74 | snprintf( filename, sizeof(FILENAME_STRING), "control_mn_%s.json", |
| davidjhoward | 134:f90154ff33ec | 75 | from_json["pay"]["mncontrol"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 134:f90154ff33ec | 76 | logInfo("MANUAL CONTROL FILENAME=%s", filename); |
| davidjhoward | 134:f90154ff33ec | 77 | |
| davidjhoward | 134:f90154ff33ec | 78 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 134:f90154ff33ec | 79 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 134:f90154ff33ec | 80 | "{ " |
| davidjhoward | 134:f90154ff33ec | 81 | "\"id\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 82 | "\"output\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 83 | "\"type\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 84 | "\"priority\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 85 | "\"duration\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 86 | "\"setpoint\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 87 | "\"state\": \"%s\", " |
| davidjhoward | 134:f90154ff33ec | 88 | "\"percent\": \"%s\" }", |
| davidjhoward | 134:f90154ff33ec | 89 | from_json["pay"]["mncontrol"]["id"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 90 | from_json["pay"]["mncontrol"]["output"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 91 | from_json["pay"]["mncontrol"]["type"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 92 | from_json["pay"]["mncontrol"]["priority"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 93 | from_json["pay"]["mncontrol"]["duration"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 94 | from_json["pay"]["mncontrol"]["setpoint"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 95 | from_json["pay"]["mncontrol"]["state"].get<std::string>().c_str(), |
| davidjhoward | 134:f90154ff33ec | 96 | from_json["pay"]["mncontrol"]["percent"].get<std::string>().c_str() |
| davidjhoward | 134:f90154ff33ec | 97 | ); |
| davidjhoward | 134:f90154ff33ec | 98 | |
| davidjhoward | 134:f90154ff33ec | 99 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 134:f90154ff33ec | 100 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 134:f90154ff33ec | 101 | if( status != true ) { |
| davidjhoward | 134:f90154ff33ec | 102 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 134:f90154ff33ec | 103 | } |
| davidjhoward | 134:f90154ff33ec | 104 | |
| davidjhoward | 134:f90154ff33ec | 105 | // send a message to the configuration handler to create the control |
| davidjhoward | 134:f90154ff33ec | 106 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 134:f90154ff33ec | 107 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 134:f90154ff33ec | 108 | msg->action = ACTION_CREATE; |
| davidjhoward | 134:f90154ff33ec | 109 | msg->control = CONTROL_MANUAL; |
| davidjhoward | 134:f90154ff33ec | 110 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 134:f90154ff33ec | 111 | |
| davidjhoward | 134:f90154ff33ec | 112 | printf("%s: Sending a create manual for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 134:f90154ff33ec | 113 | |
| davidjhoward | 134:f90154ff33ec | 114 | MailBox.put(msg); |
| davidjhoward | 99:55317f374a94 | 115 | break; |
| davidjhoward | 99:55317f374a94 | 116 | } |
| davidjhoward | 139:f1f4a523399f | 117 | case TIMER_CONTROL_MTYPE: { |
| davidjhoward | 139:f1f4a523399f | 118 | logInfo("PROCESSING TIMER CONTROL FILE"); |
| davidjhoward | 139:f1f4a523399f | 119 | snprintf( filename, sizeof(FILENAME_STRING), "control_tm_%s.json", |
| davidjhoward | 139:f1f4a523399f | 120 | from_json["pay"]["tmcontrol"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 139:f1f4a523399f | 121 | logInfo("TIMER CONTROL FILENAME=%s", filename); |
| davidjhoward | 139:f1f4a523399f | 122 | |
| davidjhoward | 139:f1f4a523399f | 123 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 139:f1f4a523399f | 124 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 139:f1f4a523399f | 125 | "{ " |
| davidjhoward | 139:f1f4a523399f | 126 | "\"id\": \"%s\", " |
| davidjhoward | 139:f1f4a523399f | 127 | "\"output\": \"%s\", " |
| davidjhoward | 139:f1f4a523399f | 128 | "\"priority\": \"%s\", " |
| davidjhoward | 139:f1f4a523399f | 129 | "\"starttime\": \"%s\", " |
| davidjhoward | 139:f1f4a523399f | 130 | "\"duration\": \"%s\", ", |
| davidjhoward | 139:f1f4a523399f | 131 | from_json["pay"]["tmcontrol"]["id"].get<std::string>().c_str(), |
| davidjhoward | 139:f1f4a523399f | 132 | from_json["pay"]["tmcontrol"]["output"].get<std::string>().c_str(), |
| davidjhoward | 139:f1f4a523399f | 133 | from_json["pay"]["tmcontrol"]["priority"].get<std::string>().c_str(), |
| davidjhoward | 139:f1f4a523399f | 134 | from_json["pay"]["tmcontrol"]["starttime"].get<std::string>().c_str(), |
| davidjhoward | 139:f1f4a523399f | 135 | from_json["pay"]["tmcontrol"]["duration"].get<std::string>().c_str() |
| davidjhoward | 139:f1f4a523399f | 136 | ); |
| davidjhoward | 139:f1f4a523399f | 137 | |
| davidjhoward | 139:f1f4a523399f | 138 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 139:f1f4a523399f | 139 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 139:f1f4a523399f | 140 | if( status != true ) { |
| davidjhoward | 139:f1f4a523399f | 141 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 139:f1f4a523399f | 142 | } |
| davidjhoward | 139:f1f4a523399f | 143 | |
| davidjhoward | 139:f1f4a523399f | 144 | // send a message to the configuration handler to create the control |
| davidjhoward | 139:f1f4a523399f | 145 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 139:f1f4a523399f | 146 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 139:f1f4a523399f | 147 | msg->action = ACTION_CREATE; |
| davidjhoward | 139:f1f4a523399f | 148 | msg->control = CONTROL_TIMER; |
| davidjhoward | 139:f1f4a523399f | 149 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 139:f1f4a523399f | 150 | |
| davidjhoward | 139:f1f4a523399f | 151 | printf("%s: Sending a create request for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 139:f1f4a523399f | 152 | |
| davidjhoward | 139:f1f4a523399f | 153 | MailBox.put(msg); |
| davidjhoward | 99:55317f374a94 | 154 | break; |
| davidjhoward | 139:f1f4a523399f | 155 | } |
| davidjhoward | 99:55317f374a94 | 156 | case INPUT_CONFIG_MTYPE: { |
| davidjhoward | 99:55317f374a94 | 157 | logInfo("id=%s", from_json["pay"]["input"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 99:55317f374a94 | 158 | snprintf( filename, sizeof(FILENAME_STRING), "input_%s%s", |
| davidjhoward | 99:55317f374a94 | 159 | from_json["pay"]["input"]["id"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 160 | ".json" ); |
| davidjhoward | 99:55317f374a94 | 161 | logInfo("INPUT CONFIG FILENAME=%s", filename); |
| davidjhoward | 31:4b1587034318 | 162 | |
| davidjhoward | 99:55317f374a94 | 163 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 99:55317f374a94 | 164 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 99:55317f374a94 | 165 | "{ " |
| davidjhoward | 99:55317f374a94 | 166 | "\"id\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 167 | "\"name\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 168 | "\"units\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 169 | "\"min\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 170 | "\"max\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 171 | "\"node\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 172 | "\"reg\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 173 | "\"rtype\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 174 | "\"type\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 175 | "\"size\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 176 | "\"order\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 177 | "\"fmt\":\"%s\", " |
| davidjhoward | 99:55317f374a94 | 178 | "\"rfreq\":\"%s\" } ", |
| davidjhoward | 99:55317f374a94 | 179 | from_json["pay"]["input"]["id"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 180 | from_json["pay"]["input"]["name"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 181 | from_json["pay"]["input"]["units"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 182 | from_json["pay"]["input"]["min"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 183 | from_json["pay"]["input"]["max"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 184 | from_json["pay"]["input"]["node"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 185 | from_json["pay"]["input"]["reg"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 186 | from_json["pay"]["input"]["rtype"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 187 | from_json["pay"]["input"]["type"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 188 | from_json["pay"]["input"]["size"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 189 | from_json["pay"]["input"]["order"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 190 | from_json["pay"]["input"]["fmt"].get<std::string>().c_str(), |
| davidjhoward | 99:55317f374a94 | 191 | from_json["pay"]["input"]["rfreq"].get<std::string>().c_str() |
| davidjhoward | 99:55317f374a94 | 192 | ); |
| davidjhoward | 45:3b9e1923cb15 | 193 | |
| davidjhoward | 99:55317f374a94 | 194 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 99:55317f374a94 | 195 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 99:55317f374a94 | 196 | if( status != true ) { |
| davidjhoward | 99:55317f374a94 | 197 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 99:55317f374a94 | 198 | } |
| davidjhoward | 45:3b9e1923cb15 | 199 | |
| davidjhoward | 99:55317f374a94 | 200 | logInfo("Sending Mail To ModbusMasterMailBox, filename=%s", filename); |
| davidjhoward | 99:55317f374a94 | 201 | Message_t *mail = ModbusMasterMailBox.alloc(); |
| davidjhoward | 99:55317f374a94 | 202 | mail->action = ACTION_READ_FILE; |
| davidjhoward | 99:55317f374a94 | 203 | strncpy( mail->controlFile, filename, (sizeof(mail->controlFile)-1)); |
| davidjhoward | 99:55317f374a94 | 204 | ModbusMasterMailBox.put(mail); |
| davidjhoward | 99:55317f374a94 | 205 | break; |
| davidjhoward | 27:38205cebc3da | 206 | } |
| davidjhoward | 144:b32d6a6f445f | 207 | case OUTPUT_CONFIG_MTYPE: { |
| davidjhoward | 144:b32d6a6f445f | 208 | logInfo("id=%s", from_json["pay"]["output"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 144:b32d6a6f445f | 209 | snprintf( filename, sizeof(FILENAME_STRING), "output_%s%s", |
| davidjhoward | 144:b32d6a6f445f | 210 | from_json["pay"]["output"]["id"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 211 | ".json" ); |
| davidjhoward | 144:b32d6a6f445f | 212 | logInfo("OUTPUT CONFIG FILENAME=%s", filename); |
| davidjhoward | 144:b32d6a6f445f | 213 | |
| davidjhoward | 144:b32d6a6f445f | 214 | memset( CloudFileReceiverWriteBuf, '\0', 1024 ); |
| davidjhoward | 144:b32d6a6f445f | 215 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), |
| davidjhoward | 144:b32d6a6f445f | 216 | "{ " |
| davidjhoward | 144:b32d6a6f445f | 217 | "\"id\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 218 | "\"name\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 219 | "\"units\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 220 | "\"min\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 221 | "\"max\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 222 | "\"node\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 223 | "\"reg\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 224 | "\"rtype\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 225 | "\"type\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 226 | "\"size\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 227 | "\"order\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 228 | "\"fmt\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 229 | "\"rfreq\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 230 | "\"toperiod\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 231 | "\"scalelo\":\"%s\", " |
| davidjhoward | 144:b32d6a6f445f | 232 | "\"scalehi\":\"%s\" }", |
| davidjhoward | 144:b32d6a6f445f | 233 | from_json["pay"]["output"]["id"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 234 | from_json["pay"]["output"]["name"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 235 | from_json["pay"]["output"]["units"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 236 | from_json["pay"]["output"]["min"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 237 | from_json["pay"]["output"]["max"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 238 | from_json["pay"]["output"]["node"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 239 | from_json["pay"]["output"]["reg"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 240 | from_json["pay"]["output"]["rtype"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 241 | from_json["pay"]["output"]["type"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 242 | from_json["pay"]["output"]["size"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 243 | from_json["pay"]["output"]["order"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 244 | from_json["pay"]["output"]["fmt"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 245 | from_json["pay"]["output"]["rfreq"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 246 | from_json["pay"]["output"]["toperiod"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 247 | from_json["pay"]["output"]["scalelo"].get<std::string>().c_str(), |
| davidjhoward | 144:b32d6a6f445f | 248 | from_json["pay"]["output"]["scalehi"].get<std::string>().c_str() |
| davidjhoward | 144:b32d6a6f445f | 249 | ); |
| davidjhoward | 144:b32d6a6f445f | 250 | |
| davidjhoward | 144:b32d6a6f445f | 251 | logInfo("saving string%s", CloudFileReceiverWriteBuf); |
| davidjhoward | 144:b32d6a6f445f | 252 | bool status = GLOBAL_mdot->saveUserFile(filename, (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 144:b32d6a6f445f | 253 | if( status != true ) { |
| davidjhoward | 144:b32d6a6f445f | 254 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 144:b32d6a6f445f | 255 | } |
| davidjhoward | 144:b32d6a6f445f | 256 | |
| davidjhoward | 144:b32d6a6f445f | 257 | logInfo("Sending Mail To ModbusMasterMailBox, filename=%s", filename); |
| davidjhoward | 144:b32d6a6f445f | 258 | Message_t *mail = ModbusMasterMailBox.alloc(); |
| davidjhoward | 144:b32d6a6f445f | 259 | mail->action = ACTION_READ_FILE; |
| davidjhoward | 144:b32d6a6f445f | 260 | strncpy( mail->controlFile, filename, (sizeof(mail->controlFile)-1)); |
| davidjhoward | 144:b32d6a6f445f | 261 | ModbusMasterMailBox.put(mail); |
| davidjhoward | 144:b32d6a6f445f | 262 | break; |
| davidjhoward | 144:b32d6a6f445f | 263 | } |
| davidjhoward | 145:c1dfbb5eed0e | 264 | case DESTROY_SETPOINT_MTYPE: { |
| davidjhoward | 145:c1dfbb5eed0e | 265 | logInfo("PROCESSING DESTORY SETPOINT CONTROL MESSAGE"); |
| davidjhoward | 145:c1dfbb5eed0e | 266 | snprintf( filename, sizeof(FILENAME_STRING), "control_sp_%s.json", |
| davidjhoward | 145:c1dfbb5eed0e | 267 | from_json["pay"]["destroy"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 145:c1dfbb5eed0e | 268 | logInfo("SETPOINT CONTROL FILENAME=%s", filename); |
| davidjhoward | 145:c1dfbb5eed0e | 269 | |
| davidjhoward | 145:c1dfbb5eed0e | 270 | // send a message to the configuration handler to create the control |
| davidjhoward | 145:c1dfbb5eed0e | 271 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 145:c1dfbb5eed0e | 272 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 145:c1dfbb5eed0e | 273 | msg->action = ACTION_DESTROY; |
| davidjhoward | 145:c1dfbb5eed0e | 274 | msg->control = CONTROL_SETPOINT; |
| davidjhoward | 145:c1dfbb5eed0e | 275 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 145:c1dfbb5eed0e | 276 | |
| davidjhoward | 145:c1dfbb5eed0e | 277 | printf("%s: Sending a destroy request for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 145:c1dfbb5eed0e | 278 | |
| davidjhoward | 145:c1dfbb5eed0e | 279 | MailBox.put(msg); |
| davidjhoward | 145:c1dfbb5eed0e | 280 | break; |
| davidjhoward | 145:c1dfbb5eed0e | 281 | } |
| davidjhoward | 145:c1dfbb5eed0e | 282 | case DESTROY_MANUAL_MTYPE: { |
| davidjhoward | 145:c1dfbb5eed0e | 283 | logInfo("PROCESSING DESTORY MANUAL CONTROL MESSAGE"); |
| davidjhoward | 145:c1dfbb5eed0e | 284 | snprintf( filename, sizeof(FILENAME_STRING), "control_mn_%s.json", |
| davidjhoward | 145:c1dfbb5eed0e | 285 | from_json["pay"]["destroy"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 145:c1dfbb5eed0e | 286 | logInfo("MANUAL CONTROL FILENAME=%s", filename); |
| davidjhoward | 145:c1dfbb5eed0e | 287 | |
| davidjhoward | 145:c1dfbb5eed0e | 288 | // send a message to the configuration handler to create the control |
| davidjhoward | 145:c1dfbb5eed0e | 289 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 145:c1dfbb5eed0e | 290 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 145:c1dfbb5eed0e | 291 | msg->action = ACTION_DESTROY; |
| davidjhoward | 145:c1dfbb5eed0e | 292 | msg->control = CONTROL_MANUAL; |
| davidjhoward | 145:c1dfbb5eed0e | 293 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 145:c1dfbb5eed0e | 294 | |
| davidjhoward | 145:c1dfbb5eed0e | 295 | printf("%s: Sending a destroy request for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 145:c1dfbb5eed0e | 296 | |
| davidjhoward | 145:c1dfbb5eed0e | 297 | MailBox.put(msg); |
| davidjhoward | 145:c1dfbb5eed0e | 298 | break; |
| davidjhoward | 145:c1dfbb5eed0e | 299 | } |
| davidjhoward | 145:c1dfbb5eed0e | 300 | case DESTROY_TIMER_MTYPE: { |
| davidjhoward | 145:c1dfbb5eed0e | 301 | logInfo("PROCESSING DESTORY TIMER CONTROL MESSAGE"); |
| davidjhoward | 145:c1dfbb5eed0e | 302 | snprintf( filename, sizeof(FILENAME_STRING), "control_tm_%s.json", |
| davidjhoward | 145:c1dfbb5eed0e | 303 | from_json["pay"]["destroy"]["id"].get<std::string>().c_str() ); |
| davidjhoward | 145:c1dfbb5eed0e | 304 | logInfo("TIMER CONTROL FILENAME=%s", filename); |
| davidjhoward | 145:c1dfbb5eed0e | 305 | |
| davidjhoward | 145:c1dfbb5eed0e | 306 | // send a message to the configuration handler to create the control |
| davidjhoward | 145:c1dfbb5eed0e | 307 | Message_t *msg = MailBox.alloc(); |
| davidjhoward | 145:c1dfbb5eed0e | 308 | memset(msg, 0, sizeof(Message_t)); |
| davidjhoward | 145:c1dfbb5eed0e | 309 | msg->action = ACTION_DESTROY; |
| davidjhoward | 145:c1dfbb5eed0e | 310 | msg->control = CONTROL_TIMER; |
| davidjhoward | 145:c1dfbb5eed0e | 311 | strncpy(msg->controlFile, filename, sizeof(msg->controlFile)-1); |
| davidjhoward | 145:c1dfbb5eed0e | 312 | |
| davidjhoward | 145:c1dfbb5eed0e | 313 | printf("%s: Sending a destroy request for control %s type = %u\r\n", __func__, msg->controlFile, msg->control); |
| davidjhoward | 145:c1dfbb5eed0e | 314 | |
| davidjhoward | 145:c1dfbb5eed0e | 315 | MailBox.put(msg); |
| davidjhoward | 145:c1dfbb5eed0e | 316 | break; |
| davidjhoward | 145:c1dfbb5eed0e | 317 | } |
| davidjhoward | 99:55317f374a94 | 318 | default: |
| davidjhoward | 99:55317f374a94 | 319 | logInfo("DEFAULT"); |
| davidjhoward | 99:55317f374a94 | 320 | break; |
| davidjhoward | 99:55317f374a94 | 321 | } |
| davidjhoward | 99:55317f374a94 | 322 | return true; |
| davidjhoward | 49:0456ee2271be | 323 | } |
| davidjhoward | 27:38205cebc3da | 324 | |
| davidjhoward | 49:0456ee2271be | 325 | bool CloudFileReceiver( std::string *recv_string, mDot *dot ) |
| davidjhoward | 49:0456ee2271be | 326 | { |
| davidjhoward | 49:0456ee2271be | 327 | MbedJSONValue from_json; |
| davidjhoward | 49:0456ee2271be | 328 | bool status; |
| davidjhoward | 49:0456ee2271be | 329 | parse( from_json, recv_string->c_str() ); |
| davidjhoward | 49:0456ee2271be | 330 | int sequence = from_json["seq"].get<int>(); |
| davidjhoward | 49:0456ee2271be | 331 | |
| davidjhoward | 49:0456ee2271be | 332 | // sequence of -1 means string sent in 1 chunk. |
| davidjhoward | 49:0456ee2271be | 333 | if( sequence == -1 ) { |
| davidjhoward | 49:0456ee2271be | 334 | |
| davidjhoward | 49:0456ee2271be | 335 | return StoreReceivedFile( dot, from_json ); |
| davidjhoward | 27:38205cebc3da | 336 | |
| davidjhoward | 27:38205cebc3da | 337 | } else if( sequence == 0 ) { |
| davidjhoward | 27:38205cebc3da | 338 | |
| davidjhoward | 49:0456ee2271be | 339 | memset(CloudFileReceiverWriteBuf,0,sizeof(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 340 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", "{\"seq\":-1, \"pay\":",from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 49:0456ee2271be | 341 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, 1024, CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 342 | |
| davidjhoward | 49:0456ee2271be | 343 | status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 344 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 345 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 346 | return false; |
| davidjhoward | 27:38205cebc3da | 347 | } |
| davidjhoward | 49:0456ee2271be | 348 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 349 | |
| davidjhoward | 27:38205cebc3da | 350 | } else if( sequence == -2 ) { |
| davidjhoward | 27:38205cebc3da | 351 | |
| davidjhoward | 49:0456ee2271be | 352 | MbedJSONValue final_json; |
| davidjhoward | 49:0456ee2271be | 353 | |
| davidjhoward | 27:38205cebc3da | 354 | logInfo("(%d)READING BACK scratch.json FILE FOR LAST PACKET", __LINE__); |
| davidjhoward | 27:38205cebc3da | 355 | |
| davidjhoward | 27:38205cebc3da | 356 | // read the file back |
| davidjhoward | 49:0456ee2271be | 357 | status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 358 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 359 | logError("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 360 | return false; |
| davidjhoward | 27:38205cebc3da | 361 | } |
| davidjhoward | 27:38205cebc3da | 362 | |
| davidjhoward | 49:0456ee2271be | 363 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str(), "}" ); |
| davidjhoward | 49:0456ee2271be | 364 | logInfo("(%d)Final String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 365 | |
| davidjhoward | 49:0456ee2271be | 366 | parse( final_json, CloudFileReceiverWriteBuf ); |
| davidjhoward | 49:0456ee2271be | 367 | |
| davidjhoward | 99:55317f374a94 | 368 | int sequence = final_json["seq"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 369 | int mType = final_json["pay"]["mtype"].get<int>(); |
| davidjhoward | 99:55317f374a94 | 370 | |
| davidjhoward | 138:27c39d0e4e81 | 371 | printf("MESSAGE TYPE=%d\r\n",mType); |
| davidjhoward | 99:55317f374a94 | 372 | |
| davidjhoward | 49:0456ee2271be | 373 | status = StoreReceivedFile( dot, final_json ); |
| davidjhoward | 27:38205cebc3da | 374 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 375 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 376 | } |
| davidjhoward | 49:0456ee2271be | 377 | |
| davidjhoward | 49:0456ee2271be | 378 | status = dot->deleteUserFile("scratch.json"); |
| davidjhoward | 49:0456ee2271be | 379 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 380 | logError("(%d)delete file failed, status=%d", __LINE__, status); |
| davidjhoward | 49:0456ee2271be | 381 | } |
| davidjhoward | 49:0456ee2271be | 382 | |
| davidjhoward | 49:0456ee2271be | 383 | logInfo("(%d)DELETED scratch.json FILE, status:%d", __LINE__, status ); |
| davidjhoward | 49:0456ee2271be | 384 | |
| davidjhoward | 27:38205cebc3da | 385 | } else { |
| davidjhoward | 27:38205cebc3da | 386 | |
| davidjhoward | 27:38205cebc3da | 387 | logInfo("(%d)READING BACK scratch.json FILE", __LINE__); |
| davidjhoward | 27:38205cebc3da | 388 | |
| davidjhoward | 27:38205cebc3da | 389 | // read the file back |
| davidjhoward | 49:0456ee2271be | 390 | status = dot->readUserFile("scratch.json", (void *)CloudFileReceiverReadBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 391 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 392 | logError("(%d)read file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 393 | return false; |
| davidjhoward | 27:38205cebc3da | 394 | } |
| davidjhoward | 27:38205cebc3da | 395 | |
| davidjhoward | 49:0456ee2271be | 396 | snprintf(CloudFileReceiverWriteBuf, sizeof(CloudFileReceiverWriteBuf), "%s%s", CloudFileReceiverReadBuf, from_json["pay"].get<std::string>().c_str() ); |
| davidjhoward | 49:0456ee2271be | 397 | logInfo("(%d)Writing String Length=%d, %s", __LINE__, strlen(CloudFileReceiverWriteBuf), CloudFileReceiverWriteBuf ); |
| davidjhoward | 27:38205cebc3da | 398 | |
| davidjhoward | 49:0456ee2271be | 399 | status = dot->saveUserFile("scratch.json", (void *)CloudFileReceiverWriteBuf, 1024); |
| davidjhoward | 27:38205cebc3da | 400 | if( status != true ) { |
| davidjhoward | 49:0456ee2271be | 401 | logError("(%d)save file failed, status=%d", __LINE__, status); |
| davidjhoward | 27:38205cebc3da | 402 | return false; |
| davidjhoward | 27:38205cebc3da | 403 | } |
| davidjhoward | 49:0456ee2271be | 404 | logInfo("(%d)UPDATED scratch.json FILE, status:%d, strlen=%d", __LINE__, status, strlen(CloudFileReceiverWriteBuf)); |
| davidjhoward | 49:0456ee2271be | 405 | |
| davidjhoward | 27:38205cebc3da | 406 | } |
| davidjhoward | 27:38205cebc3da | 407 | |
| davidjhoward | 27:38205cebc3da | 408 | return true; |
| davidjhoward | 27:38205cebc3da | 409 | } |
