Yasushi TAUCHI / Fiap
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fiap.cpp Source File

fiap.cpp

00001 #include "fiap.h"
00002 #include "mbed.h"
00003 FIAP::FIAP(string Storage,string PointSetId) {
00004     debug_mode=false;
00005     _fiap_storage=Storage;
00006     _fiap_id_prefix=PointSetId;
00007 
00008     _soap_header ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
00009     _soap_header += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">";
00010     _soap_header += "<soapenv:Body>";
00011     _soap_header += "<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">";
00012     _soap_header += "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">";
00013     _soap_header += "<body>";
00014     _soap_header += "<pointSet id=\"" + _fiap_id_prefix +"\">";
00015 
00016     _soap_footer ="</pointSet>";
00017     _soap_footer+="</body>";
00018     _soap_footer+="</transport>";
00019     _soap_footer+="</ns2:dataRQ>";
00020     _soap_footer+="</soapenv:Body>";
00021     _soap_footer+="</soapenv:Envelope>";
00022     _soap_footer+="\r\n\r\n";
00023 }
00024 
00025 int FIAP::post(struct fiap_element* v, unsigned int esize) {
00026     HTTPClient http;
00027     int i;
00028     char requestBuffer[50];
00029     string _date_time;
00030     string _soap_text=_soap_header;
00031     for (i=0; i<esize; i++) {
00032         strftime(&requestBuffer[0],50,"%Y-%m-%dT%H:%M:%S.0000000",v[i].t);
00033         _soap_text += "<point id=\"";
00034         _soap_text += _fiap_id_prefix;
00035         _soap_text += v[i].cid;
00036         _soap_text += "\">";
00037         _soap_text += "<value time=\"";
00038         _soap_text += requestBuffer;
00039         _soap_text += v[i].timezone;
00040         _soap_text += "\">";
00041         _soap_text += v[i].value;
00042         _soap_text += "</value>";
00043         _soap_text += "</point>";
00044     }
00045     _soap_text += _soap_footer;
00046 if(debug_mode){
00047 printf(_soap_text.c_str());
00048 printf("<<< Request(end)\n");
00049 }
00050     http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
00051     http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\"");
00052     HTTPText InData;
00053     InData.set(_soap_text.c_str());
00054     HTTPText OutData;
00055     HTTPResult r = http.post(_fiap_storage.c_str(),InData,&OutData);
00056     if (r==HTTP_PROCESSING) {
00057          if (debug_mode)printf("Processing \n");
00058         return -1;
00059     }
00060     if (r==HTTP_PARSE) {
00061            if (debug_mode) printf("URI Parse error \n");
00062         return -1;
00063     }
00064     if (r==HTTP_DNS) {
00065            if (debug_mode) printf("Could not resolve name\n");
00066         return -1;
00067     }
00068     if (r==HTTP_PRTCL) {
00069            if (debug_mode)printf("Protocol error\n");
00070         return -1;
00071     }
00072     if (r==HTTP_NOTFOUND) {
00073            if (debug_mode)printf("HTTP 404 Error\n");
00074         return -1;
00075     }
00076     if (r==HTTP_REFUSED) {
00077            if (debug_mode) printf("HTTP 403 Error \n");
00078         return -1;
00079     }
00080     if (r==HTTP_ERROR) {
00081            if (debug_mode)printf("HTTP xxx error \n");
00082         return -1;
00083     }
00084     if (r==HTTP_TIMEOUT) {
00085            if (debug_mode)printf("Connection timeout\n");
00086         return -1;
00087     }
00088     if (r==HTTP_CONN) {
00089            if (debug_mode)printf("Connection error\n");
00090         return -1;
00091     }
00092 
00093     return 0;
00094 }