FIAP (IEEE1888) library
Dependents: Fetch_IEEE1888_Storage IEEE1888_MULTI_SENSOR_GW
Fork of FiapV2 by
fiap.cpp@12:f4e59ab2bab0, 2013-02-22 (annotated)
- Committer:
- strysd
- Date:
- Fri Feb 22 16:41:51 2013 +0000
- Revision:
- 12:f4e59ab2bab0
- Parent:
- 11:db42339d6cce
- Child:
- 13:13cceccf6bfb
refactoring constructors and methods
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:db42339d6cce | 8 | void FIAP::xml_initialize(void) |
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 | 11:db42339d6cce | 18 | } else { |
strysd | 11:db42339d6cce | 19 | strcat(_soap_header,"dataRQ"); |
strysd | 11:db42339d6cce | 20 | strcat(_soap_action,"data"); |
strysd | 11:db42339d6cce | 21 | } |
strysd | 11:db42339d6cce | 22 | |
strysd | 11:db42339d6cce | 23 | strcat(_soap_header," xmlns:ns2=\"http://soap.fiap.org/\">"); |
yueee_yt | 2:2ba00be13585 | 24 | strcat(_soap_header,"<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">"); |
strysd | 11:db42339d6cce | 25 | |
strysd | 12:f4e59ab2bab0 | 26 | if (_use_fetch) { |
strysd | 11:db42339d6cce | 27 | strcat(_soap_header,"<header>"); |
strysd | 11:db42339d6cce | 28 | strcpy(_soap_footer,"</header>"); |
strysd | 11:db42339d6cce | 29 | } else { |
strysd | 11:db42339d6cce | 30 | strcat(_soap_header,"<body>"); |
strysd | 11:db42339d6cce | 31 | strcpy(_soap_footer,"</body>"); |
strysd | 11:db42339d6cce | 32 | } |
strysd | 11:db42339d6cce | 33 | |
strysd | 11:db42339d6cce | 34 | strcat(_soap_footer,"</transport></ns2:"); |
strysd | 11:db42339d6cce | 35 | |
strysd | 12:f4e59ab2bab0 | 36 | if (_use_fetch) { |
strysd | 11:db42339d6cce | 37 | strcat(_soap_footer,"queryRQ>"); |
strysd | 11:db42339d6cce | 38 | } else { |
strysd | 11:db42339d6cce | 39 | strcat(_soap_footer,"dataRQ>"); |
strysd | 11:db42339d6cce | 40 | } |
strysd | 11:db42339d6cce | 41 | |
yueee_yt | 0:d34a9148b19e | 42 | strcat(_soap_footer,"</soapenv:Body>"); |
yueee_yt | 0:d34a9148b19e | 43 | strcat(_soap_footer,"</soapenv:Envelope>"); |
yueee_yt | 0:d34a9148b19e | 44 | strcat(_soap_footer,"\r\n\r\n"); |
yueee_yt | 0:d34a9148b19e | 45 | } |
strysd | 11:db42339d6cce | 46 | |
strysd | 11:db42339d6cce | 47 | void FIAP::generateUUID() |
yueee_yt | 2:2ba00be13585 | 48 | { |
strysd | 11:db42339d6cce | 49 | 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); |
yueee_yt | 0:d34a9148b19e | 50 | } |
yueee_yt | 0:d34a9148b19e | 51 | |
strysd | 10:b355957e01f4 | 52 | char* FIAP::HTTPStatusText(HTTPResult r) |
strysd | 10:b355957e01f4 | 53 | { |
strysd | 10:b355957e01f4 | 54 | if (r==HTTP_OK) { |
strysd | 10:b355957e01f4 | 55 | return "Success \n"; |
strysd | 10:b355957e01f4 | 56 | } |
strysd | 10:b355957e01f4 | 57 | if (r==HTTP_PROCESSING) { |
strysd | 10:b355957e01f4 | 58 | return "Processing \n"; |
strysd | 10:b355957e01f4 | 59 | } |
strysd | 10:b355957e01f4 | 60 | if (r==HTTP_PARSE) { |
strysd | 10:b355957e01f4 | 61 | return "URI Parse error \n"; |
strysd | 10:b355957e01f4 | 62 | } |
strysd | 10:b355957e01f4 | 63 | if (r==HTTP_DNS) { |
strysd | 10:b355957e01f4 | 64 | return "Could not resolve name\n"; |
strysd | 10:b355957e01f4 | 65 | } |
strysd | 10:b355957e01f4 | 66 | if (r==HTTP_PRTCL) { |
strysd | 10:b355957e01f4 | 67 | return "Protocol error\n"; |
strysd | 10:b355957e01f4 | 68 | } |
strysd | 10:b355957e01f4 | 69 | if (r==HTTP_NOTFOUND) { |
strysd | 10:b355957e01f4 | 70 | return "HTTP 404 Error\n"; |
strysd | 10:b355957e01f4 | 71 | } |
strysd | 10:b355957e01f4 | 72 | if (r==HTTP_REFUSED) { |
strysd | 10:b355957e01f4 | 73 | return "HTTP 403 Error \n"; |
strysd | 10:b355957e01f4 | 74 | } |
strysd | 10:b355957e01f4 | 75 | if (r==HTTP_ERROR) { |
strysd | 10:b355957e01f4 | 76 | return "HTTP xxx error \n "; |
strysd | 10:b355957e01f4 | 77 | } |
strysd | 10:b355957e01f4 | 78 | if (r==HTTP_TIMEOUT) { |
strysd | 10:b355957e01f4 | 79 | return "Connection timeout\n"; |
strysd | 10:b355957e01f4 | 80 | } |
strysd | 10:b355957e01f4 | 81 | if (r==HTTP_CONN) { |
strysd | 10:b355957e01f4 | 82 | return "Connection error\n"; |
strysd | 10:b355957e01f4 | 83 | } |
strysd | 10:b355957e01f4 | 84 | if (r==HTTP_CLOSED) { |
strysd | 10:b355957e01f4 | 85 | return "Connection error\n"; |
strysd | 10:b355957e01f4 | 86 | } |
strysd | 10:b355957e01f4 | 87 | |
strysd | 10:b355957e01f4 | 88 | return "Unknown Network Error\n"; |
strysd | 10:b355957e01f4 | 89 | } |
strysd | 10:b355957e01f4 | 90 | |
strysd | 11:db42339d6cce | 91 | void FIAP::myprintf(char text[2000]) |
strysd | 11:db42339d6cce | 92 | { |
strysd | 11:db42339d6cce | 93 | if (debug_mode)printf(text); |
strysd | 11:db42339d6cce | 94 | } |
strysd | 11:db42339d6cce | 95 | |
strysd | 9:02ae92c06e52 | 96 | FIAP::FIAP() |
yueee_yt | 2:2ba00be13585 | 97 | { |
yueee_yt | 2:2ba00be13585 | 98 | debug_mode=false; |
strysd | 12:f4e59ab2bab0 | 99 | _use_fetch =false; |
strysd | 12:f4e59ab2bab0 | 100 | xml_initialize(); |
strysd | 12:f4e59ab2bab0 | 101 | } |
strysd | 11:db42339d6cce | 102 | |
strysd | 12:f4e59ab2bab0 | 103 | FIAP::FIAP(char Storage[]) |
strysd | 12:f4e59ab2bab0 | 104 | { |
strysd | 12:f4e59ab2bab0 | 105 | debug_mode=false; |
strysd | 12:f4e59ab2bab0 | 106 | _use_fetch =false; |
strysd | 11:db42339d6cce | 107 | xml_initialize(); |
strysd | 12:f4e59ab2bab0 | 108 | strcpy(_fiap_storage,Storage); |
strysd | 12:f4e59ab2bab0 | 109 | } |
strysd | 12:f4e59ab2bab0 | 110 | |
strysd | 12:f4e59ab2bab0 | 111 | FIAP::FIAP(char Storage[], bool _use_fetch) |
strysd | 12:f4e59ab2bab0 | 112 | { |
strysd | 12:f4e59ab2bab0 | 113 | debug_mode=false; |
strysd | 12:f4e59ab2bab0 | 114 | xml_initialize(); |
strysd | 12:f4e59ab2bab0 | 115 | strcpy(_fiap_storage,Storage); |
strysd | 9:02ae92c06e52 | 116 | } |
strysd | 9:02ae92c06e52 | 117 | |
strysd | 9:02ae92c06e52 | 118 | void FIAP::setStorage(char Storage[]) |
strysd | 9:02ae92c06e52 | 119 | { |
yueee_yt | 2:2ba00be13585 | 120 | strcpy(_fiap_storage,Storage); |
strysd | 9:02ae92c06e52 | 121 | return; |
yueee_yt | 2:2ba00be13585 | 122 | } |
yueee_yt | 2:2ba00be13585 | 123 | |
strysd | 12:f4e59ab2bab0 | 124 | int FIAP::fetch_last_data(struct fiap_element* v, unsigned int esize) |
yueee_yt | 2:2ba00be13585 | 125 | { |
yueee_yt | 0:d34a9148b19e | 126 | HTTPClient http; |
yueee_yt | 6:52e95ea60ec1 | 127 | int i,ii,ll,k1,k2; |
yueee_yt | 0:d34a9148b19e | 128 | strcpy( _soap_text, _soap_header); |
yueee_yt | 0:d34a9148b19e | 129 | strcat( _soap_text , "<query id=\""); |
strysd | 11:db42339d6cce | 130 | generateUUID(); |
strysd | 11:db42339d6cce | 131 | strcat( _soap_text , _uuid); |
yueee_yt | 0:d34a9148b19e | 132 | strcat( _soap_text , "\" type=\"storage\">"); |
strysd | 11:db42339d6cce | 133 | for (i=0; i < esize; i++) { |
yueee_yt | 0:d34a9148b19e | 134 | strcat(_soap_text , "<key id=\""); |
yueee_yt | 0:d34a9148b19e | 135 | strcat(_soap_text , v[i].cid); |
yueee_yt | 0:d34a9148b19e | 136 | strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>"); |
yueee_yt | 0:d34a9148b19e | 137 | } |
yueee_yt | 0:d34a9148b19e | 138 | strcat(_soap_text , "</query>"); |
yueee_yt | 0:d34a9148b19e | 139 | strcat(_soap_text , _soap_footer); |
yueee_yt | 0:d34a9148b19e | 140 | if (debug_mode) { |
yueee_yt | 0:d34a9148b19e | 141 | printf(_fiap_storage); |
yueee_yt | 0:d34a9148b19e | 142 | printf("\r\n"); |
yueee_yt | 0:d34a9148b19e | 143 | printf(_soap_text); |
yueee_yt | 0:d34a9148b19e | 144 | printf("<<< Request(end)\n"); |
yueee_yt | 0:d34a9148b19e | 145 | } |
yueee_yt | 2:2ba00be13585 | 146 | //HTTPText InData("text/html", 800); |
yueee_yt | 2:2ba00be13585 | 147 | //InData.set(_soap_text); |
yueee_yt | 2:2ba00be13585 | 148 | HTTPText InData(_soap_text); |
yueee_yt | 3:7b144e1a52db | 149 | char rstr[800]; |
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; |
yueee_yt | 3:7b144e1a52db | 168 | if (strlen(rstr)>0) { |
strysd | 11:db42339d6cce | 169 | myprintf("\n\r data Get Ok\n\r"); |
yueee_yt | 0:d34a9148b19e | 170 | SP_XmlDomParser parser; |
yueee_yt | 3:7b144e1a52db | 171 | parser.append(rstr,strlen(rstr)); |
yueee_yt | 0:d34a9148b19e | 172 | //stream.readNext((byte*)outBuffer,4096); |
yueee_yt | 0:d34a9148b19e | 173 | SP_XmlHandle rootHandle(parser.getDocument()->getRootElement()); |
yueee_yt | 0:d34a9148b19e | 174 | SP_XmlHandle transport = rootHandle.getChild(0).getChild(0).getChild(0); |
yueee_yt | 0:d34a9148b19e | 175 | SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement(); |
yueee_yt | 2:2ba00be13585 | 176 | int yy,mo,dd,hh,mm,ss; |
yueee_yt | 2:2ba00be13585 | 177 | char str[50]; |
strysd | 11:db42339d6cce | 178 | if(debug_mode)printf ("%s \n",transport.toElement()->getName()); |
yueee_yt | 2:2ba00be13585 | 179 | if (fiapError==NULL) { |
yueee_yt | 6:52e95ea60ec1 | 180 | SP_XmlElementNode * trans_element = transport.toElement(); |
yueee_yt | 6:52e95ea60ec1 | 181 | trans_point=(SP_XmlNodeList *)trans_element->getChildren() ; |
yueee_yt | 6:52e95ea60ec1 | 182 | k1=trans_point->getLength(); |
yueee_yt | 6:52e95ea60ec1 | 183 | if(debug_mode)printf("Trans Child has Element of No %d \n\r",k1); |
yueee_yt | 6:52e95ea60ec1 | 184 | SP_XmlElementNode * values; |
yueee_yt | 6:52e95ea60ec1 | 185 | for(k2=0; k2<k1; k2++) { |
yueee_yt | 6:52e95ea60ec1 | 186 | values =transport.getChild(k2).toElement(); |
strysd | 11:db42339d6cce | 187 | if(debug_mode){ |
strysd | 11:db42339d6cce | 188 | printf(values->getName()); |
strysd | 11:db42339d6cce | 189 | printf("\n\r"); |
strysd | 11:db42339d6cce | 190 | } |
yueee_yt | 6:52e95ea60ec1 | 191 | if(strstr((values->getName()),"body") !=NULL) { |
strysd | 11:db42339d6cce | 192 | myprintf("find body \n\r"); |
yueee_yt | 6:52e95ea60ec1 | 193 | points=(SP_XmlNodeList *)values->getChildren() ; |
yueee_yt | 6:52e95ea60ec1 | 194 | int j=points->getLength(); |
strysd | 11:db42339d6cce | 195 | if(debug_mode)printf("GetValues of No %d \n\r",j); |
yueee_yt | 6:52e95ea60ec1 | 196 | SP_XmlElementNode *data_point_node,*data_value_node; |
yueee_yt | 6:52e95ea60ec1 | 197 | SP_XmlCDataNode *data_value_cnode; |
yueee_yt | 6:52e95ea60ec1 | 198 | for (i=0; i<j; i++) { |
yueee_yt | 6:52e95ea60ec1 | 199 | SP_XmlHandle data_handle(points->get(i));//Point |
yueee_yt | 6:52e95ea60ec1 | 200 | data_point_node=data_handle.toElement(); |
yueee_yt | 6:52e95ea60ec1 | 201 | //data_value_node=data_handle.getChild("value").toElement(); |
yueee_yt | 6:52e95ea60ec1 | 202 | //data_value_cnode=data_handle.getChild("value").getChild(0).toCData(); |
yueee_yt | 6:52e95ea60ec1 | 203 | data_value_node=data_handle.getChild(0).toElement(); |
yueee_yt | 6:52e95ea60ec1 | 204 | data_value_cnode=data_handle.getChild(0).getChild(0).toCData(); |
strysd | 11:db42339d6cce | 205 | if(debug_mode){ |
strysd | 11:db42339d6cce | 206 | printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); |
strysd | 11:db42339d6cce | 207 | printf("date=%s \r\n",data_value_node->getAttrValue("time")); |
strysd | 11:db42339d6cce | 208 | printf("data=%s \r\n",data_value_cnode->getText()); |
strysd | 11:db42339d6cce | 209 | } |
yueee_yt | 6:52e95ea60ec1 | 210 | for (ii=0; ii<esize; ii++) { |
yueee_yt | 6:52e95ea60ec1 | 211 | if (strcmp(v[ii].cid,data_point_node->getAttrValue("id"))==0) { |
yueee_yt | 6:52e95ea60ec1 | 212 | sprintf(str,"%s",data_value_node->getAttrValue("time")); |
yueee_yt | 6:52e95ea60ec1 | 213 | ll=sscanf(str,"%d-%d-%dT%d:%d:%d.",&yy,&mo,&dd,&hh,&mm,&ss); |
strysd | 11:db42339d6cce | 214 | if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); |
yueee_yt | 6:52e95ea60ec1 | 215 | v[ii].year=yy; |
yueee_yt | 6:52e95ea60ec1 | 216 | v[ii].month=mo; |
yueee_yt | 6:52e95ea60ec1 | 217 | v[ii].day=dd; |
yueee_yt | 6:52e95ea60ec1 | 218 | v[ii].hour=hh; |
yueee_yt | 6:52e95ea60ec1 | 219 | v[ii].minute=mm; |
yueee_yt | 6:52e95ea60ec1 | 220 | v[ii].second=ss; |
yueee_yt | 6:52e95ea60ec1 | 221 | sprintf(v[ii].value,"%s",data_value_cnode->getText()); |
yueee_yt | 6:52e95ea60ec1 | 222 | } |
yueee_yt | 6:52e95ea60ec1 | 223 | } |
yueee_yt | 2:2ba00be13585 | 224 | } |
yueee_yt | 6:52e95ea60ec1 | 225 | |
yueee_yt | 2:2ba00be13585 | 226 | } |
yueee_yt | 6:52e95ea60ec1 | 227 | |
yueee_yt | 6:52e95ea60ec1 | 228 | |
yueee_yt | 2:2ba00be13585 | 229 | } |
yueee_yt | 6:52e95ea60ec1 | 230 | |
yueee_yt | 2:2ba00be13585 | 231 | } else { |
strysd | 11:db42339d6cce | 232 | myprintf("ERROR\n\r"); |
yueee_yt | 2:2ba00be13585 | 233 | } |
yueee_yt | 2:2ba00be13585 | 234 | } else { |
strysd | 11:db42339d6cce | 235 | myprintf("error\n\r"); |
yueee_yt | 2:2ba00be13585 | 236 | } |
yueee_yt | 2:2ba00be13585 | 237 | return 0; |
yueee_yt | 2:2ba00be13585 | 238 | } |
yueee_yt | 0:d34a9148b19e | 239 | |
strysd | 12:f4e59ab2bab0 | 240 | #if USE_FETCH_ONLY_1 |
strysd | 12:f4e59ab2bab0 | 241 | |
strysd | 12:f4e59ab2bab0 | 242 | int FIAP::fetch_last_data(struct fiap_element* v) |
yueee_yt | 2:2ba00be13585 | 243 | { |
yueee_yt | 2:2ba00be13585 | 244 | HTTPClient http; |
yueee_yt | 2:2ba00be13585 | 245 | int ll; |
yueee_yt | 3:7b144e1a52db | 246 | char rstr[800]; |
yueee_yt | 2:2ba00be13585 | 247 | strcpy(_soap_text, _soap_header); |
yueee_yt | 2:2ba00be13585 | 248 | strcat(_soap_text , "<query id=\""); |
strysd | 11:db42339d6cce | 249 | generateUUID(); |
strysd | 11:db42339d6cce | 250 | strcat(_soap_text , _uuid); |
yueee_yt | 2:2ba00be13585 | 251 | strcat(_soap_text , "\" type=\"storage\">"); |
yueee_yt | 2:2ba00be13585 | 252 | strcat(_soap_text , "<key id=\""); |
yueee_yt | 2:2ba00be13585 | 253 | strcat(_soap_text , v->cid); |
yueee_yt | 2:2ba00be13585 | 254 | strcat(_soap_text , "\" attrName=\"time\" select=\"maximum\"/>"); |
yueee_yt | 2:2ba00be13585 | 255 | strcat(_soap_text , "</query>"); |
yueee_yt | 2:2ba00be13585 | 256 | strcat(_soap_text , _soap_footer); |
yueee_yt | 2:2ba00be13585 | 257 | if (debug_mode) { |
yueee_yt | 2:2ba00be13585 | 258 | printf("\r\n"); |
yueee_yt | 2:2ba00be13585 | 259 | printf(_fiap_storage); |
yueee_yt | 2:2ba00be13585 | 260 | printf("\r\n"); |
yueee_yt | 2:2ba00be13585 | 261 | printf(_soap_text); |
yueee_yt | 2:2ba00be13585 | 262 | printf("<<< Request(end)\n"); |
yueee_yt | 2:2ba00be13585 | 263 | } |
yueee_yt | 2:2ba00be13585 | 264 | // http.setRequestHeader("Content-Type","text/xml; charset=UTF-8"); |
yueee_yt | 2:2ba00be13585 | 265 | // http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/query\""); |
yueee_yt | 2:2ba00be13585 | 266 | HTTPText InData(_soap_text); |
yueee_yt | 3:7b144e1a52db | 267 | HTTPText stream(rstr,800); |
yueee_yt | 2:2ba00be13585 | 268 | // stream.readNext((byte*)outBuffer,strlen(outBuffer)); |
strysd | 11:db42339d6cce | 269 | myprintf("post.start \n\r"); |
strysd | 11:db42339d6cce | 270 | HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&stream); |
strysd | 11:db42339d6cce | 271 | myprintf("post.end \n\r"); |
strysd | 10:b355957e01f4 | 272 | |
strysd | 11:db42339d6cce | 273 | myprintf(HTTPStatusText(r)); |
strysd | 10:b355957e01f4 | 274 | if (r != HTTP_OK) { |
yueee_yt | 6:52e95ea60ec1 | 275 | return -1; |
yueee_yt | 2:2ba00be13585 | 276 | } |
strysd | 10:b355957e01f4 | 277 | |
yueee_yt | 2:2ba00be13585 | 278 | SP_XmlNodeList * points; |
yueee_yt | 3:7b144e1a52db | 279 | if (strlen(rstr)>0) { |
strysd | 11:db42339d6cce | 280 | myprintf("\n\r data Get Ok\n\r"); |
yueee_yt | 6:52e95ea60ec1 | 281 | |
yueee_yt | 2:2ba00be13585 | 282 | SP_XmlDomParser parser; |
yueee_yt | 6:52e95ea60ec1 | 283 | |
strysd | 11:db42339d6cce | 284 | // myprintf("stream readlen = %d \n\r",stream.readLen()); |
yueee_yt | 3:7b144e1a52db | 285 | // outBuffer[stream.readLen()]=0; |
strysd | 11:db42339d6cce | 286 | myprintf("check0\n\r"); |
yueee_yt | 3:7b144e1a52db | 287 | parser.append(rstr,strlen(rstr)); |
strysd | 11:db42339d6cce | 288 | myprintf("check1\n\r"); |
yueee_yt | 2:2ba00be13585 | 289 | SP_XmlHandle rootHandle(parser.getDocument()->getRootElement()); |
yueee_yt | 2:2ba00be13585 | 290 | SP_XmlHandle transport = rootHandle.getChild(0).getChild(0).getChild(0); |
yueee_yt | 2:2ba00be13585 | 291 | SP_XmlElementNode * fiapError = transport.getChild("header").getChild("error").toElement(); |
yueee_yt | 2:2ba00be13585 | 292 | int yy,mo,dd,hh,mm,ss; |
yueee_yt | 0:d34a9148b19e | 293 | char str[50]; |
strysd | 11:db42339d6cce | 294 | if(debug_mode)printf ("%s \n\r",transport.toElement()->getName()); |
yueee_yt | 0:d34a9148b19e | 295 | if (fiapError==NULL) { |
yueee_yt | 0:d34a9148b19e | 296 | SP_XmlElementNode * values =transport.getChild("body").toElement(); |
yueee_yt | 0:d34a9148b19e | 297 | points=(SP_XmlNodeList *)values->getChildren() ; |
yueee_yt | 0:d34a9148b19e | 298 | // points=values->getChildren(); |
yueee_yt | 0:d34a9148b19e | 299 | int j=points->getLength(); |
strysd | 11:db42339d6cce | 300 | if(debug_mode)printf("GetValues of No %d \n\r",j); |
yueee_yt | 0:d34a9148b19e | 301 | SP_XmlElementNode *data_point_node,*data_value_node; |
yueee_yt | 0:d34a9148b19e | 302 | SP_XmlCDataNode *data_value_cnode; |
yueee_yt | 0:d34a9148b19e | 303 | SP_XmlHandle data_handle(points->get(0));//Point |
yueee_yt | 0:d34a9148b19e | 304 | data_point_node=data_handle.toElement(); |
yueee_yt | 0:d34a9148b19e | 305 | data_value_node=data_handle.getChild("value").toElement(); |
yueee_yt | 0:d34a9148b19e | 306 | data_value_cnode=data_handle.getChild("value").getChild(0).toCData(); |
strysd | 11:db42339d6cce | 307 | if(debug_mode){ |
strysd | 11:db42339d6cce | 308 | printf("PointID=%s \r\n",data_point_node->getAttrValue("id")); |
strysd | 11:db42339d6cce | 309 | printf("date=%s \r\n",data_value_node->getAttrValue("time")); |
strysd | 11:db42339d6cce | 310 | printf("data=%s \r\n",data_value_cnode->getText()); |
strysd | 11:db42339d6cce | 311 | } |
yueee_yt | 0:d34a9148b19e | 312 | sprintf(str,"%s",data_value_node->getAttrValue("time")); |
yueee_yt | 0:d34a9148b19e | 313 | ll=sscanf(str,"%d-%d-%dT%d:%d:%d.",&yy,&mo,&dd,&hh,&mm,&ss); |
strysd | 11:db42339d6cce | 314 | if(debug_mode)printf("date convert no %d (%d/%d/%d %d:%d:%d) \r\n",ll,yy,mo,dd,hh,mm,ss); |
yueee_yt | 0:d34a9148b19e | 315 | v->year=yy; |
yueee_yt | 0:d34a9148b19e | 316 | v->month=mo; |
yueee_yt | 0:d34a9148b19e | 317 | v->day=dd; |
yueee_yt | 0:d34a9148b19e | 318 | v->hour=hh; |
yueee_yt | 0:d34a9148b19e | 319 | v->minute=mm; |
yueee_yt | 0:d34a9148b19e | 320 | v->second=ss; |
yueee_yt | 0:d34a9148b19e | 321 | sprintf(v->value,"%s", data_value_cnode->getText()); |
yueee_yt | 0:d34a9148b19e | 322 | } else { |
strysd | 11:db42339d6cce | 323 | myprintf("ERROR\n\r"); |
yueee_yt | 0:d34a9148b19e | 324 | } |
yueee_yt | 0:d34a9148b19e | 325 | } else { |
strysd | 11:db42339d6cce | 326 | myprintf("error\n\r"); |
yueee_yt | 0:d34a9148b19e | 327 | } |
yueee_yt | 0:d34a9148b19e | 328 | return 0; |
yueee_yt | 0:d34a9148b19e | 329 | } |
yueee_yt | 0:d34a9148b19e | 330 | |
strysd | 12:f4e59ab2bab0 | 331 | #endif //USE_FETCH_ONLY_1 |
yueee_yt | 2:2ba00be13585 | 332 | |
yueee_yt | 2:2ba00be13585 | 333 | int FIAP::post(struct fiap_element* v, unsigned int esize) |
yueee_yt | 2:2ba00be13585 | 334 | { |
yueee_yt | 0:d34a9148b19e | 335 | HTTPClient http; |
yueee_yt | 0:d34a9148b19e | 336 | int i; |
yueee_yt | 7:cc924e783dff | 337 | char rstr[1200]; |
yueee_yt | 0:d34a9148b19e | 338 | char requestBuffer[50]; |
yueee_yt | 2:2ba00be13585 | 339 | strcpy(_soap_text,_soap_header); |
strysd | 11:db42339d6cce | 340 | for (i=0; i < esize; i++) { |
yueee_yt | 0:d34a9148b19e | 341 | 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); |
yueee_yt | 0:d34a9148b19e | 342 | strcat(_soap_text , "<point id=\""); |
strysd | 11:db42339d6cce | 343 | //!!strcat(_soap_text , _fiap_id_prefix); |
yueee_yt | 2:2ba00be13585 | 344 | strcat(_soap_text , v[i].cid); |
strysd | 11:db42339d6cce | 345 | strcat(_soap_text , "\">"); |
yueee_yt | 2:2ba00be13585 | 346 | strcat(_soap_text , "<value time=\""); |
yueee_yt | 2:2ba00be13585 | 347 | strcat(_soap_text , requestBuffer); |
yueee_yt | 2:2ba00be13585 | 348 | strcat(_soap_text , v[i].timezone); |
yueee_yt | 2:2ba00be13585 | 349 | strcat(_soap_text , "\">"); |
yueee_yt | 2:2ba00be13585 | 350 | strcat(_soap_text , v[i].value); |
yueee_yt | 2:2ba00be13585 | 351 | strcat(_soap_text , "</value>"); |
yueee_yt | 2:2ba00be13585 | 352 | strcat(_soap_text , "</point>"); |
yueee_yt | 0:d34a9148b19e | 353 | } |
yueee_yt | 2:2ba00be13585 | 354 | strcat(_soap_text , _soap_footer); |
strysd | 11:db42339d6cce | 355 | |
strysd | 11:db42339d6cce | 356 | if(debug_mode){ |
yueee_yt | 0:d34a9148b19e | 357 | printf(_soap_text); |
yueee_yt | 0:d34a9148b19e | 358 | printf("<<< Request(end)\n"); |
yueee_yt | 0:d34a9148b19e | 359 | } |
yueee_yt | 2:2ba00be13585 | 360 | // http.setRequestHeader("Content-Type","text/xml; charset=UTF-8"); |
yueee_yt | 2:2ba00be13585 | 361 | // http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\""); |
yueee_yt | 0:d34a9148b19e | 362 | // InData=new HTTPText(); |
yueee_yt | 2:2ba00be13585 | 363 | HTTPText InData(_soap_text); |
yueee_yt | 7:cc924e783dff | 364 | HTTPText OutData(rstr,1200); |
strysd | 11:db42339d6cce | 365 | HTTPResult r = http.postXML(_fiap_storage,_soap_action,InData,&OutData); |
strysd | 10:b355957e01f4 | 366 | |
strysd | 11:db42339d6cce | 367 | myprintf(HTTPStatusText(r)); |
strysd | 10:b355957e01f4 | 368 | if (r != HTTP_OK) { |
yueee_yt | 6:52e95ea60ec1 | 369 | return -1; |
yueee_yt | 0:d34a9148b19e | 370 | } |
strysd | 11:db42339d6cce | 371 | |
strysd | 11:db42339d6cce | 372 | if(debug_mode){ |
strysd | 10:b355957e01f4 | 373 | printf("****\n\r"); |
strysd | 10:b355957e01f4 | 374 | printf(rstr); |
strysd | 10:b355957e01f4 | 375 | printf("\n\r****\n\r"); |
strysd | 10:b355957e01f4 | 376 | } |
yueee_yt | 0:d34a9148b19e | 377 | return 0; |
strysd | 11:db42339d6cce | 378 | } |