FIAP (IEEE1888) library

Dependents:   Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW

Fork of FiapV2 by Yasushi TAUCHI

Committer:
strysd
Date:
Sat Mar 02 02:37:40 2013 +0000
Revision:
19:101d8775b33d
Parent:
18:274f2cbdc18c
comment element size from fetch_last_data.  now trouble if the size is bigger than 1.

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 19:101d8775b33d 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 19:101d8775b33d 125 char zone[10];//timezone
strysd 18:274f2cbdc18c 126 char p_id[100];
strysd 17:e6298cbdefdc 127 char p_val[50];
strysd 18:274f2cbdc18c 128 char p_time[50];
strysd 15:3bd6f70e57e2 129 char uuid[37];
strysd 15:3bd6f70e57e2 130
yueee_yt 0:d34a9148b19e 131 strcpy( _soap_text, _soap_header);
yueee_yt 0:d34a9148b19e 132 strcat( _soap_text , "<query id=\"");
strysd 15:3bd6f70e57e2 133 sprintf(uuid,"%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
strysd 15:3bd6f70e57e2 134 rand()%0x10000, rand()%0x10000, rand()%0x10000,
strysd 15:3bd6f70e57e2 135 rand()%1000|0x4000, rand()%0x1000|0x8000, rand()%0x10000,
strysd 15:3bd6f70e57e2 136 rand()%10000, (rand()+1)%0x10000);
strysd 15:3bd6f70e57e2 137 strcat( _soap_text , uuid);
yueee_yt 0:d34a9148b19e 138 strcat( _soap_text , "\" type=\"storage\">");
strysd 19:101d8775b33d 139 /* for (i=0; i < esize; i++) {*/
strysd 19:101d8775b33d 140 i = 0;
yueee_yt 0:d34a9148b19e 141 strcat(_soap_text , "<key id=\"");
yueee_yt 0:d34a9148b19e 142 strcat(_soap_text , v[i].cid);
yueee_yt 0:d34a9148b19e 143 strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>");
strysd 19:101d8775b33d 144 /* }*/
yueee_yt 0:d34a9148b19e 145 strcat(_soap_text , "</query>");
yueee_yt 0:d34a9148b19e 146 strcat(_soap_text , _soap_footer);
yueee_yt 0:d34a9148b19e 147 if (debug_mode) {
yueee_yt 0:d34a9148b19e 148 printf(_fiap_storage);
yueee_yt 0:d34a9148b19e 149 printf("\r\n");
yueee_yt 0:d34a9148b19e 150 printf(_soap_text);
yueee_yt 0:d34a9148b19e 151 printf("<<< Request(end)\n");
yueee_yt 0:d34a9148b19e 152 }
yueee_yt 2:2ba00be13585 153 //HTTPText InData("text/html", 800);
yueee_yt 2:2ba00be13585 154 //InData.set(_soap_text);
yueee_yt 2:2ba00be13585 155 HTTPText InData(_soap_text);
yueee_yt 3:7b144e1a52db 156 HTTPText stream(rstr,800);
yueee_yt 2:2ba00be13585 157 //stream.readNext((byte*)outBuffer,1500);
strysd 11:db42339d6cce 158 myprintf("post.start \n\r");
strysd 11:db42339d6cce 159 HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream);
strysd 11:db42339d6cce 160 myprintf("post.end \n\r");
yueee_yt 6:52e95ea60ec1 161 if (debug_mode) {
yueee_yt 6:52e95ea60ec1 162 printf("****\n\r");
yueee_yt 6:52e95ea60ec1 163 printf(rstr);
yueee_yt 6:52e95ea60ec1 164 printf("\n\r****\n\r");
yueee_yt 6:52e95ea60ec1 165 }
strysd 10:b355957e01f4 166
strysd 11:db42339d6cce 167 myprintf(HTTPStatusText(r));
strysd 10:b355957e01f4 168 if (r != HTTP_OK) {
yueee_yt 6:52e95ea60ec1 169 return -1;
yueee_yt 0:d34a9148b19e 170 }
strysd 10:b355957e01f4 171
yueee_yt 0:d34a9148b19e 172 SP_XmlNodeList * points;
yueee_yt 6:52e95ea60ec1 173 SP_XmlNodeList * trans_point;
strysd 15:3bd6f70e57e2 174 if (strlen(rstr) <= 0) {
strysd 15:3bd6f70e57e2 175 myprintf("can not get data\n\r");
strysd 15:3bd6f70e57e2 176 return -1;
strysd 15:3bd6f70e57e2 177 }
strysd 15:3bd6f70e57e2 178
strysd 15:3bd6f70e57e2 179 myprintf("\n\r data Get Ok\n\r");
strysd 15:3bd6f70e57e2 180 SP_XmlDomParser parser;
strysd 15:3bd6f70e57e2 181 parser.append(rstr,strlen(rstr));
strysd 15:3bd6f70e57e2 182 //stream.readNext((byte*)outBuffer,4096);
strysd 15:3bd6f70e57e2 183 SP_XmlHandle rootHandle(parser.getDocument()->getRootElement());
strysd 15:3bd6f70e57e2 184 SP_XmlHandle transport = rootHandle.getChild(0).getChild(0).getChild(0);
strysd 15:3bd6f70e57e2 185 SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement();
strysd 15:3bd6f70e57e2 186 if(debug_mode)printf ("%s \n",transport.toElement()->getName());
strysd 15:3bd6f70e57e2 187 if(fiapError != NULL) {
strysd 15:3bd6f70e57e2 188 myprintf("XmlElementNode error\n\r");
strysd 15:3bd6f70e57e2 189 return -1;
strysd 15:3bd6f70e57e2 190 }
strysd 15:3bd6f70e57e2 191
strysd 15:3bd6f70e57e2 192 SP_XmlElementNode * trans_element = transport.toElement();
strysd 15:3bd6f70e57e2 193 trans_point=(SP_XmlNodeList *)trans_element->getChildren() ;
strysd 15:3bd6f70e57e2 194 k1=trans_point->getLength();
strysd 15:3bd6f70e57e2 195 if(debug_mode)printf("Trans Child has Element of No %d \n\r",k1);
strysd 15:3bd6f70e57e2 196 SP_XmlElementNode * values;
strysd 15:3bd6f70e57e2 197 for(k2=0; k2<k1; k2++) {
strysd 15:3bd6f70e57e2 198 values =transport.getChild(k2).toElement();
strysd 15:3bd6f70e57e2 199 if(debug_mode){
strysd 15:3bd6f70e57e2 200 printf(values->getName());
strysd 15:3bd6f70e57e2 201 printf("\n\r");
strysd 15:3bd6f70e57e2 202 }
strysd 15:3bd6f70e57e2 203 if(strstr((values->getName()),"body") !=NULL) {
strysd 15:3bd6f70e57e2 204 myprintf("find body \n\r");
strysd 15:3bd6f70e57e2 205 points=(SP_XmlNodeList *)values->getChildren() ;
strysd 15:3bd6f70e57e2 206 j=points->getLength();
strysd 15:3bd6f70e57e2 207 if(debug_mode)printf("GetValues of No %d \n\r",j);
strysd 15:3bd6f70e57e2 208 SP_XmlElementNode *data_point_node,*data_value_node;
strysd 15:3bd6f70e57e2 209 SP_XmlCDataNode *data_value_cnode;
strysd 15:3bd6f70e57e2 210 for (i=0; i<j; i++) {
strysd 15:3bd6f70e57e2 211 SP_XmlHandle data_handle(points->get(i));//Point
strysd 15:3bd6f70e57e2 212 data_point_node=data_handle.toElement();
strysd 15:3bd6f70e57e2 213 //data_value_node=data_handle.getChild("value").toElement();
strysd 15:3bd6f70e57e2 214 //data_value_cnode=data_handle.getChild("value").getChild(0).toCData();
strysd 15:3bd6f70e57e2 215 data_value_node=data_handle.getChild(0).toElement();
strysd 15:3bd6f70e57e2 216 data_value_cnode=data_handle.getChild(0).getChild(0).toCData();
strysd 17:e6298cbdefdc 217
strysd 18:274f2cbdc18c 218 sprintf(p_id ,"%s",data_point_node->getAttrValue("id"));
strysd 17:e6298cbdefdc 219 sprintf(p_val,"%s",data_value_cnode->getText());
strysd 18:274f2cbdc18c 220 sprintf(p_time,"%s",data_value_node->getAttrValue("time"));
strysd 17:e6298cbdefdc 221
strysd 11:db42339d6cce 222 if(debug_mode){
strysd 18:274f2cbdc18c 223 printf("PointID=%s \r\n",p_id);
strysd 18:274f2cbdc18c 224 printf("date=%s \r\n",p_time);
strysd 17:e6298cbdefdc 225 printf("data=%s \r\n",p_val);
strysd 11:db42339d6cce 226 }
strysd 19:101d8775b33d 227 /* for (ii=0; ii<esize; ii++) {*/
strysd 19:101d8775b33d 228 ii = 0;
strysd 18:274f2cbdc18c 229 if (strcmp(v[ii].cid, p_id)==0) {
strysd 18:274f2cbdc18c 230 ll=sscanf(p_time,"%d-%d-%dT%d:%d:%d.000%s",
strysd 18:274f2cbdc18c 231 &yy,&mo,&dd,&hh,&mm,&ss,zone);
strysd 18:274f2cbdc18c 232 if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d %s) \r\n",
strysd 18:274f2cbdc18c 233 ll,yy,mo,dd,hh,mm,ss,zone);
strysd 18:274f2cbdc18c 234
strysd 16:3a54e2b235ec 235 //TODO timezone
strysd 17:e6298cbdefdc 236 struct fiap_element my_element = {
strysd 17:e6298cbdefdc 237 v[ii].cid, p_val, yy, mo, dd,
strysd 18:274f2cbdc18c 238 hh, mm, ss, NULL};
strysd 17:e6298cbdefdc 239
strysd 16:3a54e2b235ec 240 v[ii] = my_element;
yueee_yt 2:2ba00be13585 241 }
strysd 19:101d8775b33d 242 /* }*/
yueee_yt 2:2ba00be13585 243 }
yueee_yt 2:2ba00be13585 244 }
yueee_yt 2:2ba00be13585 245 }
yueee_yt 2:2ba00be13585 246 return 0;
strysd 14:a9ec0e6e22c8 247 }
strysd 15:3bd6f70e57e2 248 #endif //USE_FETCH
yueee_yt 0:d34a9148b19e 249
yueee_yt 2:2ba00be13585 250 int FIAP::post(struct fiap_element* v, unsigned int esize)
yueee_yt 2:2ba00be13585 251 {
yueee_yt 0:d34a9148b19e 252 HTTPClient http;
yueee_yt 0:d34a9148b19e 253 int i;
yueee_yt 7:cc924e783dff 254 char rstr[1200];
strysd 19:101d8775b33d 255 char p_time[50];
yueee_yt 2:2ba00be13585 256 strcpy(_soap_text,_soap_header);
strysd 11:db42339d6cce 257 for (i=0; i < esize; i++) {
strysd 19:101d8775b33d 258 sprintf(p_time,"%04d-%02d-%02dT%02d:%02d:%02d.0000000",
strysd 15:3bd6f70e57e2 259 v[i].year,v[i].month,v[i].day,
strysd 15:3bd6f70e57e2 260 v[i].hour,v[i].minute,v[i].second);
yueee_yt 0:d34a9148b19e 261 strcat(_soap_text , "<point id=\"");
yueee_yt 2:2ba00be13585 262 strcat(_soap_text , v[i].cid);
strysd 11:db42339d6cce 263 strcat(_soap_text , "\">");
yueee_yt 2:2ba00be13585 264 strcat(_soap_text , "<value time=\"");
strysd 19:101d8775b33d 265 strcat(_soap_text , p_time);
yueee_yt 2:2ba00be13585 266 strcat(_soap_text , v[i].timezone);
yueee_yt 2:2ba00be13585 267 strcat(_soap_text , "\">");
yueee_yt 2:2ba00be13585 268 strcat(_soap_text , v[i].value);
yueee_yt 2:2ba00be13585 269 strcat(_soap_text , "</value>");
yueee_yt 2:2ba00be13585 270 strcat(_soap_text , "</point>");
yueee_yt 0:d34a9148b19e 271 }
yueee_yt 2:2ba00be13585 272 strcat(_soap_text , _soap_footer);
strysd 11:db42339d6cce 273
strysd 11:db42339d6cce 274 if(debug_mode){
yueee_yt 0:d34a9148b19e 275 printf(_soap_text);
yueee_yt 0:d34a9148b19e 276 printf("<<< Request(end)\n");
yueee_yt 0:d34a9148b19e 277 }
yueee_yt 2:2ba00be13585 278 // http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
yueee_yt 2:2ba00be13585 279 // http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\"");
yueee_yt 0:d34a9148b19e 280 // InData=new HTTPText();
yueee_yt 2:2ba00be13585 281 HTTPText InData(_soap_text);
yueee_yt 7:cc924e783dff 282 HTTPText OutData(rstr,1200);
strysd 11:db42339d6cce 283 HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&OutData);
strysd 10:b355957e01f4 284
strysd 11:db42339d6cce 285 myprintf(HTTPStatusText(r));
strysd 10:b355957e01f4 286 if (r != HTTP_OK) {
yueee_yt 6:52e95ea60ec1 287 return -1;
yueee_yt 0:d34a9148b19e 288 }
strysd 11:db42339d6cce 289
strysd 11:db42339d6cce 290 if(debug_mode){
strysd 10:b355957e01f4 291 printf("****\n\r");
strysd 10:b355957e01f4 292 printf(rstr);
strysd 10:b355957e01f4 293 printf("\n\r****\n\r");
strysd 10:b355957e01f4 294 }
yueee_yt 0:d34a9148b19e 295 return 0;
strysd 11:db42339d6cce 296 }