FIAP (IEEE1888) library

Dependents:   Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW

Fork of FiapV2 by Yasushi TAUCHI

Revision:
0:d34a9148b19e
Child:
2:2ba00be13585
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fiap.cpp	Mon Aug 20 00:40:00 2012 +0000
@@ -0,0 +1,409 @@
+#include "fiap.h"
+#include "mbed.h"
+#include "stdio.h"
+#include "spdomparser.hpp"
+#include "spxmlnode.hpp"
+#include "spxmlhandle.hpp"
+
+char outBuffer[1500+1]={0};
+//    HTTPClient http;
+
+//HTTPText InData("text/html", 800);
+//    HTTPStream stream;
+
+char _soap_text[800];
+char uuid[37];
+//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) {
+FIAP::FIAP(char Storage[],char PointSetId[]) {
+    debug_mode=false;
+    strcpy(_fiap_storage,Storage);
+    strcpy(_fiap_id_prefix,PointSetId);
+
+    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 , "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">");
+    strcat(_soap_header , "<body>");
+    strcat(_soap_header , "<pointSet id=\"");
+    strcat(_soap_header , _fiap_id_prefix);
+    strcat(_soap_header , "\">");
+
+    strcpy(_soap_footer,"</pointSet>");
+    strcat(_soap_footer ,"</body>");
+    strcat(_soap_footer ,"</transport>");
+    strcat(_soap_footer,"</ns2: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\"");
+}
+
+FIAP::FIAP(char Storage[]) {
+//printf("start-fiap(fetch)\r\n");
+    debug_mode=false;
+    //printf(Storage);
+    strcpy(_fiap_storage,Storage);
+    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>");
+//printf("start-fiap(fetch)2\r\n");
+
+    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\"");
+//   printf("start-fiap(fetch)-define-end\r\n");
+}
+
+int FIAP::fetch_last_data(struct fiap_element* v,unsigned int esize) {
+    HTTPClient http;
+    int i,ii,ll;
+ //   char uuid[37];
+ //   char _soap_text[800];
+    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);
+//sprintf(uuid,"0a90f1fa-bdb4-48ff-87d3-661d2af6ff4c");
+
+    strcat( _soap_text , "<query id=\"");
+    strcat( _soap_text , uuid);
+    strcat( _soap_text , "\" type=\"storage\">");
+    for (i=0; i<esize; i++) {
+        strcat(_soap_text , "<key id=\"");
+        strcat(_soap_text , v[i].cid);
+        strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>");
+    }
+    strcat(_soap_text , "</query>");
+    strcat(_soap_text , _soap_footer);
+    if (debug_mode) {
+        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","query");
+    http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/query\"");
+    HTTPText InData("text/html", 800);
+    InData.set(_soap_text);
+//    HTTPText OutData("text/html", 2048);
+    HTTPStream stream;
+ // char outBuffer[1500+1]={0};
+    stream.readNext((byte*)outBuffer,1500);
+printf("post.start \n\r");
+ //    HTTPResult r = http.post(_fiap_storage,InData,&OutData);
+ HTTPResult    r = http.post(_fiap_storage,InData,&stream);
+printf("post.end \n\r");
+    if (r==HTTP_PROCESSING) {
+        if (debug_mode)printf("Processing \n");
+        return -1;
+    }
+    if (r==HTTP_PARSE) {
+        if (debug_mode) printf("URI Parse error \n");
+        return -1;
+    }
+    if (r==HTTP_DNS) {
+        if (debug_mode) printf("Could not resolve name\n");
+        return -1;
+    }
+    if (r==HTTP_PRTCL) {
+        if (debug_mode)printf("Protocol error\n");
+        return -1;
+    }
+    if (r==HTTP_NOTFOUND) {
+        if (debug_mode)printf("HTTP 404 Error\n");
+        return -1;
+    }
+    if (r==HTTP_REFUSED) {
+        if (debug_mode) printf("HTTP 403 Error \n");
+        return -1;
+    }
+    if (r==HTTP_ERROR) {
+        if (debug_mode)printf("HTTP xxx error %d  \n ",r);
+        return -1;
+    }
+    if (r==HTTP_TIMEOUT) {
+        if (debug_mode)printf("Connection timeout\n");
+        return -1;
+    }
+    if (r==HTTP_CONN) {
+        if (debug_mode)printf("Connection error\n");
+        return -1;
+    }
+   //   printf("Responce(start)>>>\n");
+   //   printf(OutData.gets());
+   //   printf("<<< Response(end)\n");
+    //  return;
+    SP_XmlNodeList * points;
+    if (stream.readable()) {
+        if (debug_mode)printf("\n\r data Get Ok\n\r");
+        SP_XmlDomParser parser;
+        outBuffer[stream.readLen()]=0;
+        parser.append(outBuffer,strlen(outBuffer));
+        //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];
+        printf ("%s \n",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;
+            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();
+                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());
+                //time_t second[50]={0};
+                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 {
+            printf("ERROR\n\r");
+        }
+    } else {
+        if (debug_mode)printf("error\n\r");
+    }
+
+    return 0;
+}
+int FIAP::fetch_last_data(struct fiap_element *v) {
+    HTTPClient http;
+    int ll;
+ //   char uuid[37];
+ //  char _soap_text[800];
+    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);
+//sprintf(uuid,"0a90f1fa-bdb4-48ff-87d3-661d2af6ff4c");
+    strcat(_soap_text , "<query id=\"");
+    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","query");
+    http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/query\"");
+    HTTPText InData;
+    InData.set(_soap_text);
+    //HTTPText OutData("text/html", 2000);
+    //  HTTPText OutData;
+    HTTPStream stream;
+   // char outBuffer[1500+1];
+    stream.readNext((byte*)outBuffer,strlen(outBuffer));
+    printf("post.start \n\r");
+    //HTTPResult r = http.post(_fiap_storage,InData,&OutData);
+    HTTPResult r = http.post(_fiap_storage,InData,&stream);
+    printf("post.end \n\r");
+    if (r==HTTP_PROCESSING) {
+        if (debug_mode)printf("Processing \n");
+        return -1;
+    }
+    if (r==HTTP_PARSE) {
+        if (debug_mode) printf("URI Parse error \n");
+        return -1;
+    }
+    if (r==HTTP_DNS) {
+        if (debug_mode) printf("Could not resolve name\n");
+        return -1;
+    }
+    if (r==HTTP_PRTCL) {
+        if (debug_mode)printf("Protocol error\n");
+        return -1;
+    }
+    if (r==HTTP_NOTFOUND) {
+        if (debug_mode)printf("HTTP 404 Error\n");
+        return -1;
+    }
+    if (r==HTTP_REFUSED) {
+        if (debug_mode) printf("HTTP 403 Error \n");
+        return -1;
+    }
+    if (r==HTTP_ERROR) {
+        if (debug_mode)printf("HTTP xxx error %d  \n ",r);
+        return -1;
+    }
+    if (r==HTTP_TIMEOUT) {
+        if (debug_mode)printf("Connection timeout\n");
+        return -1;
+    }
+    if (r==HTTP_CONN) {
+        if (debug_mode)printf("Connection error\n");
+        return -1;
+    }
+    // printf("Responce(start)>>>\n");
+    // printf(OutData.gets());
+    // printf("<<< Response(end)\n");
+    // return;
+
+    SP_XmlNodeList * points;
+    if (stream.readable()) {
+        if (debug_mode)printf("\n\r data Get Ok\n\r");
+        SP_XmlDomParser parser;
+        if (debug_mode)printf("stream readlen = %d \n\r",stream.readLen());
+
+        outBuffer[stream.readLen()]=0;
+        printf("check0\n\r");
+        parser.append(outBuffer,strlen(outBuffer));
+        printf("check1\n\r");
+
+        //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];
+        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"));
+            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());
+            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 {
+            printf("ERROR\n\r");
+        }
+    } else {
+        if (debug_mode)printf("error\n\r");
+    }
+
+    return 0;
+
+}
+
+int FIAP::post(struct fiap_element* v, unsigned int esize) {
+    HTTPClient http;
+    int i;
+    char requestBuffer[50];
+ //   char _soap_text[800];
+    strcpy(_soap_header,_soap_text);
+    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 , v[i].cid);
+        strcat( _soap_text, "\">");
+        strcat(   _soap_text , "<value time=\"");
+        strcat(   _soap_text , requestBuffer);
+        strcat(   _soap_text , v[i].timezone);
+        strcat(   _soap_text , "\">");
+        strcat(   _soap_text , v[i].value);
+        strcat(   _soap_text , "</value>");
+        strcat(   _soap_text , "</point>");
+    }
+    strcat(   _soap_text , _soap_footer);
+    if (debug_mode) {
+        printf(_soap_text);
+        printf("<<< Request(end)\n");
+    }
+    http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
+    http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\"");
+//    InData=new HTTPText();
+    HTTPText InData;
+    InData.set(_soap_text);
+    HTTPText OutData;
+    HTTPResult r = http.post(_fiap_storage,InData,&OutData);
+    if (r==HTTP_PROCESSING) {
+        if (debug_mode)printf("Processing \n");
+        return -1;
+    }
+    if (r==HTTP_PARSE) {
+        if (debug_mode) printf("URI Parse error \n");
+        return -1;
+    }
+    if (r==HTTP_DNS) {
+        if (debug_mode) printf("Could not resolve name\n");
+        return -1;
+    }
+    if (r==HTTP_PRTCL) {
+        if (debug_mode)printf("Protocol error\n");
+        return -1;
+    }
+    if (r==HTTP_NOTFOUND) {
+        if (debug_mode)printf("HTTP 404 Error\n");
+        return -1;
+    }
+    if (r==HTTP_REFUSED) {
+        if (debug_mode) printf("HTTP 403 Error \n");
+        return -1;
+    }
+    if (r==HTTP_ERROR) {
+        if (debug_mode)printf("HTTP xxx error \n");
+        return -1;
+    }
+    if (r==HTTP_TIMEOUT) {
+        if (debug_mode)printf("Connection timeout\n");
+        return -1;
+    }
+    if (r==HTTP_CONN) {
+        if (debug_mode)printf("Connection error\n");
+        return -1;
+    }
+
+    return 0;
+}
\ No newline at end of file