FIAP (IEEE1888) library
Dependents: Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW
Fork of FiapV2 by
Diff: fiap.cpp
- Revision:
- 11:db42339d6cce
- Parent:
- 10:b355957e01f4
- Child:
- 12:f4e59ab2bab0
--- a/fiap.cpp Thu Feb 21 16:43:59 2013 +0000 +++ b/fiap.cpp Fri Feb 22 05:00:50 2013 +0000 @@ -5,50 +5,48 @@ #include "spxmlnode.hpp" #include "spxmlhandle.hpp" -char outBuffer[2000+1]= {0}; -char _soap_text[2000]; -char uuid[37]; -// HTTPClient http; -// HTTPText InData("text/html", 800); -// HTTPStream stream; -//char str[50]; -//int yy,mo,dd,hh,mm,ss; -//char requestBuffer[50]; -//int i,ii,ll; -//HTTPResult r; -//FIAP::FIAP(string Storage,string PointSetId) { - -void FIAP::post_xml_initialize(void) +void FIAP::xml_initialize(void) { strcpy(_soap_header,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); strcat(_soap_header,"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"); - strcat(_soap_header,"<soapenv:Body>"); - strcat(_soap_header,"<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">"); + strcat(_soap_header,"<soapenv:Body><ns2:"); + strcpy(_soap_action,"http://soap.fiap.org/"); + + if (USE_FETCH_PROCEDURE) { + strcat(_soap_header,"queryRQ"); + strcat(_soap_action,"query"); + } else { + strcat(_soap_header,"dataRQ"); + strcat(_soap_action,"data"); + } + + strcat(_soap_header," xmlns:ns2=\"http://soap.fiap.org/\">"); strcat(_soap_header,"<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"); - strcat(_soap_header,"<body>"); - strcpy(_soap_footer,"</body>"); - strcat(_soap_footer,"</transport>"); - strcat(_soap_footer,"</ns2:dataRQ>"); + + if (USE_FETCH_PROCEDURE) { + strcat(_soap_header,"<header>"); + strcpy(_soap_footer,"</header>"); + } else { + strcat(_soap_header,"<body>"); + strcpy(_soap_footer,"</body>"); + } + + strcat(_soap_footer,"</transport></ns2:"); + + if (USE_FETCH_PROCEDURE) { + strcat(_soap_footer,"queryRQ>"); + } else { + strcat(_soap_footer,"dataRQ>"); + } + strcat(_soap_footer,"</soapenv:Body>"); strcat(_soap_footer,"</soapenv:Envelope>"); strcat(_soap_footer,"\r\n\r\n"); - strcpy(_soap_action,"\"http://soap.fiap.org/data\""); } -void FIAP::fetch_xml_initialize(void) + +void FIAP::generateUUID() { - strcpy(_soap_header,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); - strcat(_soap_header,"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"); - strcat(_soap_header,"<soapenv:Body>"); - strcat(_soap_header,"<ns2:queryRQ xmlns:ns2=\"http://soap.fiap.org/\">"); - strcat(_soap_header,"<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"); - strcat(_soap_header,"<header>"); - strcpy(_soap_footer,"</header>"); - strcat(_soap_footer,"</transport>"); - strcat(_soap_footer,"</ns2:queryRQ>"); - strcat(_soap_footer,"</soapenv:Body>"); - strcat(_soap_footer,"</soapenv:Envelope>"); - strcat(_soap_footer,"\r\n\r\n"); - strcpy(_soap_action,"\"http://soap.fiap.org/query\""); + sprintf(_uuid,"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",rand()%0x10000,rand()%0x10000,rand()%0x10000,rand()%1000|0x4000,rand()%0x1000|0x8000,rand()%0x10000,rand()%10000,(rand()+1)%0x10000); } char* FIAP::HTTPStatusText(HTTPResult r) @@ -90,9 +88,16 @@ return "Unknown Network Error\n"; } +void FIAP::myprintf(char text[2000]) +{ + if (debug_mode)printf(text); +} + FIAP::FIAP() { debug_mode=false; + + xml_initialize(); } void FIAP::setStorage(char Storage[]) @@ -101,17 +106,18 @@ return; } +#if USE_FETCH_PROCEDURE + int FIAP::fetch_last_data(struct fiap_element* v,unsigned int esize) { HTTPClient http; int i,ii,ll,k1,k2; - fetch_xml_initialize(); strcpy( _soap_text, _soap_header); - sprintf(uuid,"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",rand()%0x10000,rand()%0x10000,rand()%0x10000,rand()%1000|0x4000,rand()%0x1000|0x8000,rand()%0x10000,rand()%10000,(rand()+1)%0x10000); strcat( _soap_text , "<query id=\""); - strcat( _soap_text , uuid); + generateUUID(); + strcat( _soap_text , _uuid); strcat( _soap_text , "\" type=\"storage\">"); - for (i=0; i<esize; i++) { + for (i=0; i < esize; i++) { strcat(_soap_text , "<key id=\""); strcat(_soap_text , v[i].cid); strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>"); @@ -130,16 +136,16 @@ char rstr[800]; HTTPText stream(rstr,800); //stream.readNext((byte*)outBuffer,1500); - if (debug_mode)printf("post.start \n\r"); - HTTPResult r = http.postXML(_fiap_storage,"http://soap.fiap.org/query",InData,&stream); - if (debug_mode) printf("post.end \n\r"); + myprintf("post.start \n\r"); + HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream); + myprintf("post.end \n\r"); if (debug_mode) { printf("****\n\r"); printf(rstr); printf("\n\r****\n\r"); } - if (debug_mode)printf(HTTPStatusText(r)); + myprintf(HTTPStatusText(r)); if (r != HTTP_OK) { return -1; } @@ -147,7 +153,7 @@ SP_XmlNodeList * points; SP_XmlNodeList * trans_point; if (strlen(rstr)>0) { - if (debug_mode)printf("\n\r data Get Ok\n\r"); + myprintf("\n\r data Get Ok\n\r"); SP_XmlDomParser parser; parser.append(rstr,strlen(rstr)); //stream.readNext((byte*)outBuffer,4096); @@ -156,7 +162,7 @@ SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement(); int yy,mo,dd,hh,mm,ss; char str[50]; - if (debug_mode) printf ("%s \n",transport.toElement()->getName()); + if(debug_mode)printf ("%s \n",transport.toElement()->getName()); if (fiapError==NULL) { SP_XmlElementNode * trans_element = transport.toElement(); trans_point=(SP_XmlNodeList *)trans_element->getChildren() ; @@ -165,13 +171,15 @@ SP_XmlElementNode * values; for(k2=0; k2<k1; k2++) { values =transport.getChild(k2).toElement(); - if(debug_mode)printf(values->getName()); - if(debug_mode)printf("\n\r"); + if(debug_mode){ + printf(values->getName()); + printf("\n\r"); + } if(strstr((values->getName()),"body") !=NULL) { - if(debug_mode)printf("find body \n\r"); + myprintf("find body \n\r"); points=(SP_XmlNodeList *)values->getChildren() ; int j=points->getLength(); - if (debug_mode) printf("GetValues of No %d \n\r",j); + if(debug_mode)printf("GetValues of No %d \n\r",j); SP_XmlElementNode *data_point_node,*data_value_node; SP_XmlCDataNode *data_value_cnode; for (i=0; i<j; i++) { @@ -181,14 +189,16 @@ //data_value_cnode=data_handle.getChild("value").getChild(0).toCData(); data_value_node=data_handle.getChild(0).toElement(); data_value_cnode=data_handle.getChild(0).getChild(0).toCData(); - if (debug_mode) printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); - if (debug_mode) printf("date=%s \r\n",data_value_node->getAttrValue("time")); - if (debug_mode) printf("data=%s \r\n",data_value_cnode->getText()); + if(debug_mode){ + printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); + printf("date=%s \r\n",data_value_node->getAttrValue("time")); + printf("data=%s \r\n",data_value_cnode->getText()); + } for (ii=0; ii<esize; ii++) { if (strcmp(v[ii].cid,data_point_node->getAttrValue("id"))==0) { sprintf(str,"%s",data_value_node->getAttrValue("time")); ll=sscanf(str,"%d-%d-%dT%d:%d:%d.",&yy,&mo,&dd,&hh,&mm,&ss); - if (debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); + if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); v[ii].year=yy; v[ii].month=mo; v[ii].day=dd; @@ -206,10 +216,10 @@ } } else { - printf("ERROR\n\r"); + myprintf("ERROR\n\r"); } } else { - if (debug_mode)printf("error\n\r"); + myprintf("error\n\r"); } return 0; } @@ -219,11 +229,10 @@ HTTPClient http; int ll; char rstr[800]; - fetch_xml_initialize(); strcpy(_soap_text, _soap_header); - sprintf(uuid,"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",rand()%0x10000,rand()%0x10000,rand()%0x10000,rand()%1000|0x4000,rand()%0x1000|0x8000,rand()%0x10000,rand()%10000,(rand()+1)%0x10000); strcat(_soap_text , "<query id=\""); - strcat(_soap_text , uuid); + generateUUID(); + strcat(_soap_text , _uuid); strcat(_soap_text , "\" type=\"storage\">"); strcat(_soap_text , "<key id=\""); strcat(_soap_text , v->cid); @@ -242,50 +251,52 @@ HTTPText InData(_soap_text); HTTPText stream(rstr,800); // stream.readNext((byte*)outBuffer,strlen(outBuffer)); - if (debug_mode)printf("post.start \n\r"); - HTTPResult r = http.postXML(_fiap_storage,"http://soap.fiap.org/query",InData,&stream); - if (debug_mode)printf("post.end \n\r"); + myprintf("post.start \n\r"); + HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream); + myprintf("post.end \n\r"); - if (debug_mode)printf(HTTPStatusText(r)); + myprintf(HTTPStatusText(r)); if (r != HTTP_OK) { return -1; } SP_XmlNodeList * points; if (strlen(rstr)>0) { - if (debug_mode)printf("\n\r data Get Ok\n\r"); + myprintf("\n\r data Get Ok\n\r"); SP_XmlDomParser parser; - // if (debug_mode)printf("stream readlen = %d \n\r",stream.readLen()); + // myprintf("stream readlen = %d \n\r",stream.readLen()); // outBuffer[stream.readLen()]=0; - if (debug_mode)printf("check0\n\r"); + myprintf("check0\n\r"); parser.append(rstr,strlen(rstr)); - if (debug_mode)printf("check1\n\r"); + myprintf("check1\n\r"); SP_XmlHandle rootHandle(parser.getDocument()->getRootElement()); SP_XmlHandle transport = rootHandle.getChild(0).getChild(0).getChild(0); SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement(); int yy,mo,dd,hh,mm,ss; char str[50]; - if (debug_mode)printf ("%s \n\r",transport.toElement()->getName()); + if(debug_mode)printf ("%s \n\r",transport.toElement()->getName()); if (fiapError==NULL) { SP_XmlElementNode * values =transport.getChild("body").toElement(); points=(SP_XmlNodeList *)values->getChildren() ; // points=values->getChildren(); int j=points->getLength(); - if (debug_mode) printf("GetValues of No %d \n\r",j); + if(debug_mode)printf("GetValues of No %d \n\r",j); SP_XmlElementNode *data_point_node,*data_value_node; SP_XmlCDataNode *data_value_cnode; SP_XmlHandle data_handle(points->get(0));//Point data_point_node=data_handle.toElement(); data_value_node=data_handle.getChild("value").toElement(); data_value_cnode=data_handle.getChild("value").getChild(0).toCData(); - if (debug_mode) printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); - if (debug_mode) printf("date=%s \r\n",data_value_node->getAttrValue("time")); - if (debug_mode) printf("data=%s \r\n",data_value_cnode->getText()); + if(debug_mode){ + printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); + printf("date=%s \r\n",data_value_node->getAttrValue("time")); + printf("data=%s \r\n",data_value_cnode->getText()); + } sprintf(str,"%s",data_value_node->getAttrValue("time")); ll=sscanf(str,"%d-%d-%dT%d:%d:%d.",&yy,&mo,&dd,&hh,&mm,&ss); - if (debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); + if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); v->year=yy; v->month=mo; v->day=dd; @@ -294,15 +305,15 @@ v->second=ss; sprintf(v->value,"%s", data_value_cnode->getText()); } else { - printf("ERROR\n\r"); + myprintf("ERROR\n\r"); } } else { - if (debug_mode)printf("error\n\r"); + myprintf("error\n\r"); } return 0; } - +#endif //USE_FETCH_PROCEDURE int FIAP::post(struct fiap_element* v, unsigned int esize) { @@ -310,14 +321,13 @@ int i; char rstr[1200]; char requestBuffer[50]; - post_xml_initialize(); strcpy(_soap_text,_soap_header); - for (i=0; i<esize; i++) { + for (i=0; i < esize; i++) { sprintf(requestBuffer,"%04d-%02d-%02dT%02d:%02d:%02d.0000000",v[i].year,v[i].month,v[i].day,v[i].hour,v[i].minute,v[i].second); strcat(_soap_text , "<point id=\""); - strcat(_soap_text , _fiap_id_prefix); + //!!strcat(_soap_text , _fiap_id_prefix); strcat(_soap_text , v[i].cid); - strcat( _soap_text, "\">"); + strcat(_soap_text , "\">"); strcat(_soap_text , "<value time=\""); strcat(_soap_text , requestBuffer); strcat(_soap_text , v[i].timezone); @@ -327,7 +337,8 @@ strcat(_soap_text , "</point>"); } strcat(_soap_text , _soap_footer); - if (debug_mode) { + + if(debug_mode){ printf(_soap_text); printf("<<< Request(end)\n"); } @@ -336,17 +347,17 @@ // InData=new HTTPText(); HTTPText InData(_soap_text); HTTPText OutData(rstr,1200); - HTTPResult r = http.postXML(_fiap_storage,"http://soap.fiap.org/data",InData,&OutData); + HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&OutData); - if (debug_mode)printf(HTTPStatusText(r)); + myprintf(HTTPStatusText(r)); if (r != HTTP_OK) { return -1; } - - if (debug_mode) { + + if(debug_mode){ printf("****\n\r"); printf(rstr); printf("\n\r****\n\r"); } return 0; -} \ No newline at end of file +}