FIAP (IEEE1888) library

Dependents:   Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW

Fork of FiapV2 by Yasushi TAUCHI

Committer:
strysd
Date:
Fri Mar 01 11:56:22 2013 +0000
Revision:
16:3a54e2b235ec
Parent:
15:3bd6f70e57e2
Child:
17:e6298cbdefdc
refactoring now...

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