FIAP (IEEE1888) library

Dependents:   Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW

Fork of FiapV2 by Yasushi TAUCHI

Revision:
15:3bd6f70e57e2
Parent:
14:a9ec0e6e22c8
Child:
16:3a54e2b235ec
--- a/fiap.cpp	Sun Feb 24 05:57:19 2013 +0000
+++ b/fiap.cpp	Wed Feb 27 14:19:33 2013 +0000
@@ -5,7 +5,7 @@
 #include "spxmlnode.hpp"
 #include "spxmlhandle.hpp"
 
-void FIAP::xml_initialize(void)
+void FIAP::setMode(bool _use_fetch)
 {
     strcpy(_soap_header,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
     strcat(_soap_header,"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">");
@@ -15,38 +15,30 @@
     if (_use_fetch) {
         strcat(_soap_header,"queryRQ");
         strcat(_soap_action,"query");
+        strcpy(_soap_footer,"</header>");
     } else {
         strcat(_soap_header,"dataRQ");
         strcat(_soap_action,"data");
+        strcpy(_soap_footer,"</body>");
     }
 
     strcat(_soap_header," xmlns:ns2=\"http://soap.fiap.org/\">");
     strcat(_soap_header,"<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">");
+    strcat(_soap_footer,"</transport></ns2:");
 
     if (_use_fetch) {
         strcat(_soap_header,"<header>");
-        strcpy(_soap_footer,"</header>");
+        strcat(_soap_footer,"queryRQ>");
     } else {
         strcat(_soap_header,"<body>");
-        strcpy(_soap_footer,"</body>");
-    }
-
-    strcat(_soap_footer,"</transport></ns2:");
-
-    if (_use_fetch) {
-        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");
-}
 
-void FIAP::generateUUID()
-{
-    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);
+    return;
 }
 
 char* FIAP::HTTPStatusText(HTTPResult r)
@@ -97,21 +89,21 @@
 {
     debug_mode = false;
     _use_fetch = false;
-    xml_initialize();
+    setMode(_use_fetch);
 }
 
 FIAP::FIAP(char Storage[])
 {
     debug_mode = false;
     _use_fetch = false;
-    xml_initialize();
+    setMode(_use_fetch);
     strcpy(_fiap_storage,Storage);
 }
 
 FIAP::FIAP(char Storage[], bool _use_fetch)
 {
     debug_mode = false;
-    xml_initialize();
+    setMode(_use_fetch);
     strcpy(_fiap_storage,Storage);
 }
 
@@ -121,15 +113,23 @@
     return;
 }
 
-#if USE_FETCH_MULTI
+#if USE_FETCH
 int FIAP::fetch_last_data(struct fiap_element* v, unsigned int esize)
 {
     HTTPClient http;
-    int i,ii,ll,k1,k2;
+    int i,ii,j,ll,k1,k2;
+    char rstr[800];
+    int yy,mo,dd,hh,mm,ss;//date, time
+    char str[50];
+    char uuid[37];
+
     strcpy( _soap_text, _soap_header);
     strcat( _soap_text , "<query id=\"");
-    generateUUID();
-    strcat( _soap_text , _uuid);
+    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 , uuid);
     strcat( _soap_text , "\" type=\"storage\">");
     for (i=0; i < esize; i++) {
         strcat(_soap_text , "<key id=\"");
@@ -147,7 +147,6 @@
     //HTTPText InData("text/html", 800);
     //InData.set(_soap_text);
     HTTPText InData(_soap_text);
-    char rstr[800];
     HTTPText stream(rstr,800);
     //stream.readNext((byte*)outBuffer,1500);
     myprintf("post.start \n\r");
@@ -166,169 +165,76 @@
     
     SP_XmlNodeList * points;
     SP_XmlNodeList * trans_point;
-    if (strlen(rstr)>0) {
-        myprintf("\n\r data Get Ok\n\r");
-        SP_XmlDomParser parser;
-        parser.append(rstr,strlen(rstr));
-        //stream.readNext((byte*)outBuffer,4096);
-        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",transport.toElement()->getName());
-        if (fiapError==NULL) {
-            SP_XmlElementNode * trans_element = transport.toElement();
-            trans_point=(SP_XmlNodeList *)trans_element->getChildren() ;
-            k1=trans_point->getLength();
-            if(debug_mode)printf("Trans Child has Element of No %d \n\r",k1);
-            SP_XmlElementNode * values;
-            for(k2=0; k2<k1; k2++) {
-                values =transport.getChild(k2).toElement();
+    if (strlen(rstr) <= 0) {
+        myprintf("can not get data\n\r");
+        return -1;
+    }
+
+    myprintf("\n\r data Get Ok\n\r");
+    SP_XmlDomParser parser;
+    parser.append(rstr,strlen(rstr));
+    //stream.readNext((byte*)outBuffer,4096);
+    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();
+    if(debug_mode)printf ("%s \n",transport.toElement()->getName());
+    if(fiapError != NULL) {
+        myprintf("XmlElementNode error\n\r");
+        return -1;
+    }
+
+    SP_XmlElementNode * trans_element = transport.toElement();
+    trans_point=(SP_XmlNodeList *)trans_element->getChildren() ;
+    k1=trans_point->getLength();
+    if(debug_mode)printf("Trans Child has Element of No %d \n\r",k1);
+    SP_XmlElementNode * values;
+    for(k2=0; k2<k1; k2++) {
+        values =transport.getChild(k2).toElement();
+        if(debug_mode){
+            printf(values->getName());
+            printf("\n\r");
+        }
+        if(strstr((values->getName()),"body") !=NULL) {
+            myprintf("find body \n\r");
+            points=(SP_XmlNodeList *)values->getChildren() ;
+            j=points->getLength();
+            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++) {
+                SP_XmlHandle data_handle(points->get(i));//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();
+                data_value_node=data_handle.getChild(0).toElement();
+                data_value_cnode=data_handle.getChild(0).getChild(0).toCData();
                 if(debug_mode){
-                    printf(values->getName());
-                    printf("\n\r");
+                    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());
                 }
-                if(strstr((values->getName()),"body") !=NULL) {
-                    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);
-                    SP_XmlElementNode *data_point_node,*data_value_node;
-                    SP_XmlCDataNode *data_value_cnode;
-                    for (i=0; i<j; i++) {
-                        SP_XmlHandle data_handle(points->get(i));//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();
-                        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"));
-                            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);
-                                v[ii].year=yy;
-                                v[ii].month=mo;
-                                v[ii].day=dd;
-                                v[ii].hour=hh;
-                                v[ii].minute=mm;
-                                v[ii].second=ss;
-                                sprintf(v[ii].value,"%s",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);
+                        v[ii].year=yy;
+                        v[ii].month=mo;
+                        v[ii].day=dd;
+                        v[ii].hour=hh;
+                        v[ii].minute=mm;
+                        v[ii].second=ss;
+                        sprintf(v[ii].value,"%s",data_value_cnode->getText());
                     }
-
                 }
-
-
             }
-
-        } else {
-            myprintf("ERROR\n\r");
         }
-    } else {
-        myprintf("error\n\r");
     }
     return 0;
 }
-#endif //USE_FETCH_MULTI
-
-#if USE_FETCH_ONLY_1
-int FIAP::fetch_last_data(struct fiap_element* v)
-{
-    HTTPClient http;
-    int ll;
-    char rstr[800];
-    strcpy(_soap_text, _soap_header);
-    strcat(_soap_text , "<query id=\"");
-    generateUUID();
-    strcat(_soap_text , _uuid);
-    strcat(_soap_text , "\" type=\"storage\">");
-    strcat(_soap_text , "<key id=\"");
-    strcat(_soap_text , v->cid);
-    strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>");
-    strcat(_soap_text , "</query>");
-    strcat(_soap_text , _soap_footer);
-    if (debug_mode) {
-        printf("\r\n");
-        printf(_fiap_storage);
-        printf("\r\n");
-        printf(_soap_text);
-        printf("<<< Request(end)\n");
-    }
-//    http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
-//    http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/query\"");
-    HTTPText InData(_soap_text);
-    HTTPText stream(rstr,800);
-//    stream.readNext((byte*)outBuffer,strlen(outBuffer));
-    myprintf("post.start \n\r");
-    HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream);
-    myprintf("post.end \n\r");
-    
-    myprintf(HTTPStatusText(r));
-    if (r != HTTP_OK) {
-        return -1;
-    }
-    
-    SP_XmlNodeList * points;
-    if (strlen(rstr)>0) {
-        myprintf("\n\r data Get Ok\n\r");
-
-        SP_XmlDomParser parser;
-
-        // myprintf("stream readlen = %d \n\r",stream.readLen());
-        // outBuffer[stream.readLen()]=0;
-        myprintf("check0\n\r");
-        parser.append(rstr,strlen(rstr));
-        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 (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);
-            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"));
-                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);
-            v->year=yy;
-            v->month=mo;
-            v->day=dd;
-            v->hour=hh;
-            v->minute=mm;
-            v->second=ss;
-            sprintf(v->value,"%s", data_value_cnode->getText());
-        } else {
-            myprintf("ERROR\n\r");
-        }
-    } else {
-        myprintf("error\n\r");
-    }
-    return 0;
-}
-#endif //USE_FETCH_ONLY_1
+#endif //USE_FETCH
 
 int FIAP::post(struct fiap_element* v, unsigned int esize)
 {
@@ -338,9 +244,10 @@
     char requestBuffer[50];
     strcpy(_soap_text,_soap_header);
     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);
+        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 , v[i].cid);
         strcat(_soap_text , "\">");
         strcat(_soap_text , "<value time=\"");