Salesforce Case Generator for mbed

Committer:
ansond
Date:
Wed Sep 24 18:52:45 2014 +0000
Revision:
12:8603f9ff1336
Parent:
11:57f6f0f286c0
Child:
13:1e06a3a4740d
updates for temp and lat/long inclusion into case

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:5953127f7c19 1 /* Copyright C2014 ARM, MIT License
ansond 0:5953127f7c19 2 *
ansond 0:5953127f7c19 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:5953127f7c19 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:5953127f7c19 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:5953127f7c19 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:5953127f7c19 7 * furnished to do so, subject to the following conditions:
ansond 0:5953127f7c19 8 *
ansond 0:5953127f7c19 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:5953127f7c19 10 * substantial portions of the Software.
ansond 0:5953127f7c19 11 *
ansond 0:5953127f7c19 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:5953127f7c19 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:5953127f7c19 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:5953127f7c19 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:5953127f7c19 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:5953127f7c19 17 */
ansond 0:5953127f7c19 18
ansond 0:5953127f7c19 19 #include "Definitions.h"
ansond 0:5953127f7c19 20 #include "SalesForceCaseGenerator.h"
ansond 0:5953127f7c19 21 #include "MbedJSONValue.h"
ansond 0:5953127f7c19 22
ansond 0:5953127f7c19 23 // constructor
ansond 10:ebdae744ee07 24 SalesForceCaseGenerator::SalesForceCaseGenerator(ErrorHandler *logger,void *transport) {
ansond 0:5953127f7c19 25 this->m_logger = logger;
ansond 10:ebdae744ee07 26 this->m_http = (HTTPClient *)transport;
ansond 0:5953127f7c19 27 }
ansond 0:5953127f7c19 28
ansond 0:5953127f7c19 29 // destructor
ansond 0:5953127f7c19 30 SalesForceCaseGenerator::~SalesForceCaseGenerator() {
ansond 0:5953127f7c19 31 }
ansond 0:5953127f7c19 32
ansond 0:5953127f7c19 33 // create a case through APEX
ansond 12:8603f9ff1336 34 bool SalesForceCaseGenerator::createCase(char *subject,char *description,char *condition,int temperature,char *latitude,char *longitude) {
ansond 0:5953127f7c19 35 bool success = false;
ansond 0:5953127f7c19 36
ansond 1:5c876ee72cf0 37 // data buffer and result buffer
ansond 11:57f6f0f286c0 38 char result[MAX_BUFFER_LENGTH+1];
ansond 11:57f6f0f286c0 39 memset(result,0,MAX_BUFFER_LENGTH+1);
ansond 1:5c876ee72cf0 40
ansond 3:8ad19b204ddf 41 // set addl header information
ansond 3:8ad19b204ddf 42 char headers[] = "User-Agent: curl/7.33.0\nAccept: */*\n" ;
ansond 3:8ad19b204ddf 43
ansond 12:8603f9ff1336 44 // build the new Salesforce.com report
ansond 12:8603f9ff1336 45 MbedJSONValue report;
ansond 12:8603f9ff1336 46 report["subject"] = subject;
ansond 12:8603f9ff1336 47 report["description"] = description;
ansond 12:8603f9ff1336 48 report["condition"] = condition;
ansond 12:8603f9ff1336 49 report["temperature"] = temperature;
ansond 12:8603f9ff1336 50 report["latitude"] = latitude;
ansond 12:8603f9ff1336 51 report["longitude"] = longitude;
ansond 12:8603f9ff1336 52
ansond 3:8ad19b204ddf 53 // covert to the HTTP data types
ansond 12:8603f9ff1336 54 char data[MAX_BUFFER_LENGTH+1];
ansond 12:8603f9ff1336 55 memset(data,0,MAX_BUFFER_LENGTH+1);
ansond 12:8603f9ff1336 56 strcpy(data,report.serialize().c_str());
ansond 12:8603f9ff1336 57 this->m_logger->log("Case JSON: %s",data);
ansond 5:09d5df94e8ae 58 HTTPJson new_case(data,strlen(data)+1);
ansond 11:57f6f0f286c0 59 HTTPText http_result(result,MAX_BUFFER_LENGTH);
ansond 3:8ad19b204ddf 60
ansond 10:ebdae744ee07 61 this->m_http->setHeader(headers) ;
ansond 10:ebdae744ee07 62 this->m_http->setSSLversion(1) ; /* TLSv1.0 */
ansond 2:670837d3e248 63
ansond 1:5c876ee72cf0 64 // POST the case and check the response
ansond 4:f506cc61e09e 65 this->m_logger->log("Posting new Case...");
ansond 9:6ab83a3326dc 66 this->m_logger->blinkTransportTxLED();
ansond 10:ebdae744ee07 67 int ret = this->m_http->post(DF_CASE_GEN_URL,new_case,&http_result,7500);
ansond 3:8ad19b204ddf 68 if (!ret) {
ansond 2:670837d3e248 69 this->m_logger->log("Parsing Reply...");
ansond 1:5c876ee72cf0 70 success = this->contains(result,"status","ok");
ansond 9:6ab83a3326dc 71 this->m_logger->blinkTransportRxLED();
ansond 1:5c876ee72cf0 72 if (success)
ansond 4:f506cc61e09e 73 this->m_logger->log("Case generation SUCCESS");
ansond 1:5c876ee72cf0 74 else
ansond 10:ebdae744ee07 75 this->m_logger->log("Case generation FAILED: %s",result);
ansond 1:5c876ee72cf0 76 }
ansond 1:5c876ee72cf0 77 else {
ansond 12:8603f9ff1336 78 this->m_logger->log("Failed to post case (%d) (%s)",ret,result);
ansond 0:5953127f7c19 79 }
ansond 0:5953127f7c19 80
ansond 0:5953127f7c19 81 // return our status
ansond 0:5953127f7c19 82 return success;
ansond 0:5953127f7c19 83 }
ansond 0:5953127f7c19 84
ansond 0:5953127f7c19 85 // look for a specific key value pair in a json message
ansond 0:5953127f7c19 86 bool SalesForceCaseGenerator::contains(char *json,char *key,char *value) {
ansond 0:5953127f7c19 87 bool has_it = false;
ansond 0:5953127f7c19 88
ansond 0:5953127f7c19 89 if (json != NULL && key != NULL && value != NULL) {
ansond 0:5953127f7c19 90 // parse the json and look for a specific <key,value> pair...
ansond 0:5953127f7c19 91 MbedJSONValue parsed;
ansond 0:5953127f7c19 92 parse(parsed,json);
ansond 0:5953127f7c19 93 char *answer = (char *)parsed[key].get<std::string>().c_str();
ansond 0:5953127f7c19 94 if (strcmp(answer,value) == 0) has_it = true;
ansond 0:5953127f7c19 95 }
ansond 0:5953127f7c19 96
ansond 0:5953127f7c19 97 return has_it;
ansond 0:5953127f7c19 98 }