FIAP (IEEE1888) library

Dependents:   Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW

Fork of FiapV2 by Yasushi TAUCHI

Committer:
strysd
Date:
Fri Mar 01 12:40:18 2013 +0000
Revision:
17:e6298cbdefdc
Parent:
16:3a54e2b235ec
Child:
18:274f2cbdc18c
can confirm multi fetch is not supported

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:d34a9148b19e 1 #include "fiap.h"
yueee_yt 0:d34a9148b19e 2 #include "mbed.h"
yueee_yt 0:d34a9148b19e 3 #include "stdio.h"
strysd 17:e6298cbdefdc 4 #if USE_FETCH
yueee_yt 0:d34a9148b19e 5 #include "spdomparser.hpp"
yueee_yt 0:d34a9148b19e 6 #include "spxmlnode.hpp"
yueee_yt 0:d34a9148b19e 7 #include "spxmlhandle.hpp"
strysd 17:e6298cbdefdc 8 #endif
yueee_yt 0:d34a9148b19e 9
strysd 15:3bd6f70e57e2 10 void FIAP::setMode(bool _use_fetch)
yueee_yt 2:2ba00be13585 11 {
yueee_yt 0:d34a9148b19e 12 strcpy(_soap_header,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
yueee_yt 2:2ba00be13585 13 strcat(_soap_header,"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">");
strysd 11:db42339d6cce 14 strcat(_soap_header,"<soapenv:Body><ns2:");
strysd 11:db42339d6cce 15 strcpy(_soap_action,"http://soap.fiap.org/");
strysd 11:db42339d6cce 16
strysd 12:f4e59ab2bab0 17 if (_use_fetch) {
strysd 11:db42339d6cce 18 strcat(_soap_header,"queryRQ");
strysd 11:db42339d6cce 19 strcat(_soap_action,"query");
strysd 15:3bd6f70e57e2 20 strcpy(_soap_footer,"</header>");
strysd 11:db42339d6cce 21 } else {
strysd 11:db42339d6cce 22 strcat(_soap_header,"dataRQ");
strysd 11:db42339d6cce 23 strcat(_soap_action,"data");
strysd 15:3bd6f70e57e2 24 strcpy(_soap_footer,"</body>");
strysd 11:db42339d6cce 25 }
strysd 11:db42339d6cce 26
strysd 11:db42339d6cce 27 strcat(_soap_header," xmlns:ns2=\"http://soap.fiap.org/\">");
yueee_yt 2:2ba00be13585 28 strcat(_soap_header,"<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">");
strysd 15:3bd6f70e57e2 29 strcat(_soap_footer,"</transport></ns2:");
strysd 11:db42339d6cce 30
strysd 12:f4e59ab2bab0 31 if (_use_fetch) {
strysd 11:db42339d6cce 32 strcat(_soap_header,"<header>");
strysd 15:3bd6f70e57e2 33 strcat(_soap_footer,"queryRQ>");
strysd 11:db42339d6cce 34 } else {
strysd 11:db42339d6cce 35 strcat(_soap_header,"<body>");
strysd 11:db42339d6cce 36 strcat(_soap_footer,"dataRQ>");
strysd 11:db42339d6cce 37 }
strysd 11:db42339d6cce 38
yueee_yt 0:d34a9148b19e 39 strcat(_soap_footer,"</soapenv:Body>");
yueee_yt 0:d34a9148b19e 40 strcat(_soap_footer,"</soapenv:Envelope>");
yueee_yt 0:d34a9148b19e 41 strcat(_soap_footer,"\r\n\r\n");
strysd 11:db42339d6cce 42
strysd 15:3bd6f70e57e2 43 return;
yueee_yt 0:d34a9148b19e 44 }
yueee_yt 0:d34a9148b19e 45
strysd 10:b355957e01f4 46 char* FIAP::HTTPStatusText(HTTPResult r)
strysd 10:b355957e01f4 47 {
strysd 10:b355957e01f4 48 if (r==HTTP_OK) {
strysd 10:b355957e01f4 49 return "Success \n";
strysd 10:b355957e01f4 50 }
strysd 10:b355957e01f4 51 if (r==HTTP_PROCESSING) {
strysd 10:b355957e01f4 52 return "Processing \n";
strysd 10:b355957e01f4 53 }
strysd 10:b355957e01f4 54 if (r==HTTP_PARSE) {
strysd 10:b355957e01f4 55 return "URI Parse error \n";
strysd 10:b355957e01f4 56 }
strysd 10:b355957e01f4 57 if (r==HTTP_DNS) {
strysd 10:b355957e01f4 58 return "Could not resolve name\n";
strysd 10:b355957e01f4 59 }
strysd 10:b355957e01f4 60 if (r==HTTP_PRTCL) {
strysd 10:b355957e01f4 61 return "Protocol error\n";
strysd 10:b355957e01f4 62 }
strysd 10:b355957e01f4 63 if (r==HTTP_NOTFOUND) {
strysd 10:b355957e01f4 64 return "HTTP 404 Error\n";
strysd 10:b355957e01f4 65 }
strysd 10:b355957e01f4 66 if (r==HTTP_REFUSED) {
strysd 10:b355957e01f4 67 return "HTTP 403 Error \n";
strysd 10:b355957e01f4 68 }
strysd 10:b355957e01f4 69 if (r==HTTP_ERROR) {
strysd 10:b355957e01f4 70 return "HTTP xxx error \n ";
strysd 10:b355957e01f4 71 }
strysd 10:b355957e01f4 72 if (r==HTTP_TIMEOUT) {
strysd 10:b355957e01f4 73 return "Connection timeout\n";
strysd 10:b355957e01f4 74 }
strysd 10:b355957e01f4 75 if (r==HTTP_CONN) {
strysd 10:b355957e01f4 76 return "Connection error\n";
strysd 10:b355957e01f4 77 }
strysd 10:b355957e01f4 78 if (r==HTTP_CLOSED) {
strysd 10:b355957e01f4 79 return "Connection error\n";
strysd 10:b355957e01f4 80 }
strysd 10:b355957e01f4 81
strysd 10:b355957e01f4 82 return "Unknown Network Error\n";
strysd 10:b355957e01f4 83 }
strysd 10:b355957e01f4 84
strysd 11:db42339d6cce 85 void FIAP::myprintf(char text[2000])
strysd 11:db42339d6cce 86 {
strysd 11:db42339d6cce 87 if (debug_mode)printf(text);
strysd 11:db42339d6cce 88 }
strysd 11:db42339d6cce 89
strysd 9:02ae92c06e52 90 FIAP::FIAP()
yueee_yt 2:2ba00be13585 91 {
strysd 13:13cceccf6bfb 92 debug_mode = false;
strysd 13:13cceccf6bfb 93 _use_fetch = false;
strysd 15:3bd6f70e57e2 94 setMode(_use_fetch);
strysd 12:f4e59ab2bab0 95 }
strysd 11:db42339d6cce 96
strysd 12:f4e59ab2bab0 97 FIAP::FIAP(char Storage[])
strysd 12:f4e59ab2bab0 98 {
strysd 13:13cceccf6bfb 99 debug_mode = false;
strysd 13:13cceccf6bfb 100 _use_fetch = false;
strysd 15:3bd6f70e57e2 101 setMode(_use_fetch);
strysd 12:f4e59ab2bab0 102 strcpy(_fiap_storage,Storage);
strysd 12:f4e59ab2bab0 103 }
strysd 12:f4e59ab2bab0 104
strysd 12:f4e59ab2bab0 105 FIAP::FIAP(char Storage[], bool _use_fetch)
strysd 12:f4e59ab2bab0 106 {
strysd 13:13cceccf6bfb 107 debug_mode = false;
strysd 15:3bd6f70e57e2 108 setMode(_use_fetch);
strysd 12:f4e59ab2bab0 109 strcpy(_fiap_storage,Storage);
strysd 9:02ae92c06e52 110 }
strysd 9:02ae92c06e52 111
strysd 9:02ae92c06e52 112 void FIAP::setStorage(char Storage[])
strysd 9:02ae92c06e52 113 {
yueee_yt 2:2ba00be13585 114 strcpy(_fiap_storage,Storage);
strysd 9:02ae92c06e52 115 return;
yueee_yt 2:2ba00be13585 116 }
yueee_yt 2:2ba00be13585 117
strysd 15:3bd6f70e57e2 118 #if USE_FETCH
strysd 12:f4e59ab2bab0 119 int FIAP::fetch_last_data(struct fiap_element* v, unsigned int esize)
yueee_yt 2:2ba00be13585 120 {
yueee_yt 0:d34a9148b19e 121 HTTPClient http;
strysd 15:3bd6f70e57e2 122 int i,ii,j,ll,k1,k2;
strysd 15:3bd6f70e57e2 123 char rstr[800];
strysd 15:3bd6f70e57e2 124 int yy,mo,dd,hh,mm,ss;//date, time
strysd 15:3bd6f70e57e2 125 char str[50];
strysd 17:e6298cbdefdc 126 char p_val[50];
strysd 15:3bd6f70e57e2 127 char uuid[37];
strysd 15:3bd6f70e57e2 128
yueee_yt 0:d34a9148b19e 129 strcpy( _soap_text, _soap_header);
yueee_yt 0:d34a9148b19e 130 strcat( _soap_text , "<query id=\"");
strysd 15:3bd6f70e57e2 131 sprintf(uuid,"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
strysd 15:3bd6f70e57e2 132 rand()%0x10000, rand()%0x10000, rand()%0x10000,
strysd 15:3bd6f70e57e2 133 rand()%1000|0x4000, rand()%0x1000|0x8000, rand()%0x10000,
strysd 15:3bd6f70e57e2 134 rand()%10000, (rand()+1)%0x10000);
strysd 15:3bd6f70e57e2 135 strcat( _soap_text , uuid);
yueee_yt 0:d34a9148b19e 136 strcat( _soap_text , "\" type=\"storage\">");
strysd 11:db42339d6cce 137 for (i=0; i < esize; i++) {
yueee_yt 0:d34a9148b19e 138 strcat(_soap_text , "<key id=\"");
yueee_yt 0:d34a9148b19e 139 strcat(_soap_text , v[i].cid);
yueee_yt 0:d34a9148b19e 140 strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>");
yueee_yt 0:d34a9148b19e 141 }
yueee_yt 0:d34a9148b19e 142 strcat(_soap_text , "</query>");
yueee_yt 0:d34a9148b19e 143 strcat(_soap_text , _soap_footer);
yueee_yt 0:d34a9148b19e 144 if (debug_mode) {
yueee_yt 0:d34a9148b19e 145 printf(_fiap_storage);
yueee_yt 0:d34a9148b19e 146 printf("\r\n");
yueee_yt 0:d34a9148b19e 147 printf(_soap_text);
yueee_yt 0:d34a9148b19e 148 printf("<<< Request(end)\n");
yueee_yt 0:d34a9148b19e 149 }
yueee_yt 2:2ba00be13585 150 //HTTPText InData("text/html", 800);
yueee_yt 2:2ba00be13585 151 //InData.set(_soap_text);
yueee_yt 2:2ba00be13585 152 HTTPText InData(_soap_text);
yueee_yt 3:7b144e1a52db 153 HTTPText stream(rstr,800);
yueee_yt 2:2ba00be13585 154 //stream.readNext((byte*)outBuffer,1500);
strysd 11:db42339d6cce 155 myprintf("post.start \n\r");
strysd 11:db42339d6cce 156 HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream);
strysd 11:db42339d6cce 157 myprintf("post.end \n\r");
yueee_yt 6:52e95ea60ec1 158 if (debug_mode) {
yueee_yt 6:52e95ea60ec1 159 printf("****\n\r");
yueee_yt 6:52e95ea60ec1 160 printf(rstr);
yueee_yt 6:52e95ea60ec1 161 printf("\n\r****\n\r");
yueee_yt 6:52e95ea60ec1 162 }
strysd 10:b355957e01f4 163
strysd 11:db42339d6cce 164 myprintf(HTTPStatusText(r));
strysd 10:b355957e01f4 165 if (r != HTTP_OK) {
yueee_yt 6:52e95ea60ec1 166 return -1;
yueee_yt 0:d34a9148b19e 167 }
strysd 10:b355957e01f4 168
yueee_yt 0:d34a9148b19e 169 SP_XmlNodeList * points;
yueee_yt 6:52e95ea60ec1 170 SP_XmlNodeList * trans_point;
strysd 15:3bd6f70e57e2 171 if (strlen(rstr) <= 0) {
strysd 15:3bd6f70e57e2 172 myprintf("can not get data\n\r");
strysd 15:3bd6f70e57e2 173 return -1;
strysd 15:3bd6f70e57e2 174 }
strysd 15:3bd6f70e57e2 175
strysd 15:3bd6f70e57e2 176 myprintf("\n\r data Get Ok\n\r");
strysd 15:3bd6f70e57e2 177 SP_XmlDomParser parser;
strysd 15:3bd6f70e57e2 178 parser.append(rstr,strlen(rstr));
strysd 15:3bd6f70e57e2 179 //stream.readNext((byte*)outBuffer,4096);
strysd 15:3bd6f70e57e2 180 SP_XmlHandle rootHandle(parser.getDocument()->getRootElement());
strysd 15:3bd6f70e57e2 181 SP_XmlHandle transport = rootHandle.getChild(0).getChild(0).getChild(0);
strysd 15:3bd6f70e57e2 182 SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement();
strysd 15:3bd6f70e57e2 183 if(debug_mode)printf ("%s \n",transport.toElement()->getName());
strysd 15:3bd6f70e57e2 184 if(fiapError != NULL) {
strysd 15:3bd6f70e57e2 185 myprintf("XmlElementNode error\n\r");
strysd 15:3bd6f70e57e2 186 return -1;
strysd 15:3bd6f70e57e2 187 }
strysd 15:3bd6f70e57e2 188
strysd 15:3bd6f70e57e2 189 SP_XmlElementNode * trans_element = transport.toElement();
strysd 15:3bd6f70e57e2 190 trans_point=(SP_XmlNodeList *)trans_element->getChildren() ;
strysd 15:3bd6f70e57e2 191 k1=trans_point->getLength();
strysd 15:3bd6f70e57e2 192 if(debug_mode)printf("Trans Child has Element of No %d \n\r",k1);
strysd 15:3bd6f70e57e2 193 SP_XmlElementNode * values;
strysd 15:3bd6f70e57e2 194 for(k2=0; k2<k1; k2++) {
strysd 15:3bd6f70e57e2 195 values =transport.getChild(k2).toElement();
strysd 15:3bd6f70e57e2 196 if(debug_mode){
strysd 15:3bd6f70e57e2 197 printf(values->getName());
strysd 15:3bd6f70e57e2 198 printf("\n\r");
strysd 15:3bd6f70e57e2 199 }
strysd 15:3bd6f70e57e2 200 if(strstr((values->getName()),"body") !=NULL) {
strysd 15:3bd6f70e57e2 201 myprintf("find body \n\r");
strysd 15:3bd6f70e57e2 202 points=(SP_XmlNodeList *)values->getChildren() ;
strysd 15:3bd6f70e57e2 203 j=points->getLength();
strysd 15:3bd6f70e57e2 204 if(debug_mode)printf("GetValues of No %d \n\r",j);
strysd 15:3bd6f70e57e2 205 SP_XmlElementNode *data_point_node,*data_value_node;
strysd 15:3bd6f70e57e2 206 SP_XmlCDataNode *data_value_cnode;
strysd 15:3bd6f70e57e2 207 for (i=0; i<j; i++) {
strysd 15:3bd6f70e57e2 208 SP_XmlHandle data_handle(points->get(i));//Point
strysd 15:3bd6f70e57e2 209 data_point_node=data_handle.toElement();
strysd 15:3bd6f70e57e2 210 //data_value_node=data_handle.getChild("value").toElement();
strysd 15:3bd6f70e57e2 211 //data_value_cnode=data_handle.getChild("value").getChild(0).toCData();
strysd 15:3bd6f70e57e2 212 data_value_node=data_handle.getChild(0).toElement();
strysd 15:3bd6f70e57e2 213 data_value_cnode=data_handle.getChild(0).getChild(0).toCData();
strysd 17:e6298cbdefdc 214
strysd 17:e6298cbdefdc 215 sprintf(p_val,"%s",data_value_cnode->getText());
strysd 17:e6298cbdefdc 216
strysd 11:db42339d6cce 217 if(debug_mode){
strysd 15:3bd6f70e57e2 218 printf("PointID=%s \r\n",data_point_node->getAttrValue("id"));
strysd 15:3bd6f70e57e2 219 printf("date=%s \r\n",data_value_node->getAttrValue("time"));
strysd 17:e6298cbdefdc 220 printf("data=%s \r\n",p_val);
strysd 11:db42339d6cce 221 }
strysd 15:3bd6f70e57e2 222 for (ii=0; ii<esize; ii++) {
strysd 15:3bd6f70e57e2 223 if (strcmp(v[ii].cid,data_point_node->getAttrValue("id"))==0) {
strysd 16:3a54e2b235ec 224 //TODO timezone
strysd 15:3bd6f70e57e2 225 sprintf(str,"%s",data_value_node->getAttrValue("time"));
strysd 15:3bd6f70e57e2 226 ll=sscanf(str,"%d-%d-%dT%d:%d:%d.",
strysd 15:3bd6f70e57e2 227 &yy,&mo,&dd,&hh,&mm,&ss);
strysd 15:3bd6f70e57e2 228 if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",
strysd 15:3bd6f70e57e2 229 ll,yy,mo,dd,hh,mm,ss);
strysd 16:3a54e2b235ec 230 //TODO timezone
strysd 17:e6298cbdefdc 231 struct fiap_element my_element = {
strysd 17:e6298cbdefdc 232 v[ii].cid, p_val, yy, mo, dd,
strysd 17:e6298cbdefdc 233 hh, mm, ss, v[ii].timezone};
strysd 17:e6298cbdefdc 234
strysd 16:3a54e2b235ec 235 v[ii] = my_element;
yueee_yt 2:2ba00be13585 236 }
yueee_yt 2:2ba00be13585 237 }
yueee_yt 2:2ba00be13585 238 }
yueee_yt 2:2ba00be13585 239 }
yueee_yt 2:2ba00be13585 240 }
yueee_yt 2:2ba00be13585 241 return 0;
strysd 14:a9ec0e6e22c8 242 }
strysd 15:3bd6f70e57e2 243 #endif //USE_FETCH
yueee_yt 0:d34a9148b19e 244
yueee_yt 2:2ba00be13585 245 int FIAP::post(struct fiap_element* v, unsigned int esize)
yueee_yt 2:2ba00be13585 246 {
yueee_yt 0:d34a9148b19e 247 HTTPClient http;
yueee_yt 0:d34a9148b19e 248 int i;
yueee_yt 7:cc924e783dff 249 char rstr[1200];
yueee_yt 0:d34a9148b19e 250 char requestBuffer[50];
yueee_yt 2:2ba00be13585 251 strcpy(_soap_text,_soap_header);
strysd 11:db42339d6cce 252 for (i=0; i < esize; i++) {
strysd 15:3bd6f70e57e2 253 sprintf(requestBuffer,"%04d-%02d-%02dT%02d:%02d:%02d.0000000",
strysd 15:3bd6f70e57e2 254 v[i].year,v[i].month,v[i].day,
strysd 15:3bd6f70e57e2 255 v[i].hour,v[i].minute,v[i].second);
yueee_yt 0:d34a9148b19e 256 strcat(_soap_text , "<point id=\"");
yueee_yt 2:2ba00be13585 257 strcat(_soap_text , v[i].cid);
strysd 11:db42339d6cce 258 strcat(_soap_text , "\">");
yueee_yt 2:2ba00be13585 259 strcat(_soap_text , "<value time=\"");
yueee_yt 2:2ba00be13585 260 strcat(_soap_text , requestBuffer);
yueee_yt 2:2ba00be13585 261 strcat(_soap_text , v[i].timezone);
yueee_yt 2:2ba00be13585 262 strcat(_soap_text , "\">");
yueee_yt 2:2ba00be13585 263 strcat(_soap_text , v[i].value);
yueee_yt 2:2ba00be13585 264 strcat(_soap_text , "</value>");
yueee_yt 2:2ba00be13585 265 strcat(_soap_text , "</point>");
yueee_yt 0:d34a9148b19e 266 }
yueee_yt 2:2ba00be13585 267 strcat(_soap_text , _soap_footer);
strysd 11:db42339d6cce 268
strysd 11:db42339d6cce 269 if(debug_mode){
yueee_yt 0:d34a9148b19e 270 printf(_soap_text);
yueee_yt 0:d34a9148b19e 271 printf("<<< Request(end)\n");
yueee_yt 0:d34a9148b19e 272 }
yueee_yt 2:2ba00be13585 273 // http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
yueee_yt 2:2ba00be13585 274 // http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\"");
yueee_yt 0:d34a9148b19e 275 // InData=new HTTPText();
yueee_yt 2:2ba00be13585 276 HTTPText InData(_soap_text);
yueee_yt 7:cc924e783dff 277 HTTPText OutData(rstr,1200);
strysd 11:db42339d6cce 278 HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&OutData);
strysd 10:b355957e01f4 279
strysd 11:db42339d6cce 280 myprintf(HTTPStatusText(r));
strysd 10:b355957e01f4 281 if (r != HTTP_OK) {
yueee_yt 6:52e95ea60ec1 282 return -1;
yueee_yt 0:d34a9148b19e 283 }
strysd 11:db42339d6cce 284
strysd 11:db42339d6cce 285 if(debug_mode){
strysd 10:b355957e01f4 286 printf("****\n\r");
strysd 10:b355957e01f4 287 printf(rstr);
strysd 10:b355957e01f4 288 printf("\n\r****\n\r");
strysd 10:b355957e01f4 289 }
yueee_yt 0:d34a9148b19e 290 return 0;
strysd 11:db42339d6cce 291 }