IEEE1888(FIAP)のライブラリです まだWRITEプロトコルしかサポートしていません。 EthernetNetIf,HTTPClientライブラリが必要です。

Files at this revision

API Documentation at this revision

Comitter:
yueee_yt
Date:
Sun Jan 01 14:31:16 2012 +0000
Commit message:

Changed in this revision

fiap.cpp Show annotated file Show diff for this revision Revisions of this file
fiap.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fiap.cpp	Sun Jan 01 14:31:16 2012 +0000
@@ -0,0 +1,94 @@
+#include "fiap.h"
+#include "mbed.h"
+FIAP::FIAP(string Storage,string PointSetId) {
+    debug_mode=false;
+    _fiap_storage=Storage;
+    _fiap_id_prefix=PointSetId;
+
+    _soap_header ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
+    _soap_header += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">";
+    _soap_header += "<soapenv:Body>";
+    _soap_header += "<ns2:dataRQ xmlns:ns2=\"http://soap.fiap.org/\">";
+    _soap_header += "<transport xmlns=\"http://gutp.jp/fiap/2009/11/\">";
+    _soap_header += "<body>";
+    _soap_header += "<pointSet id=\"" + _fiap_id_prefix +"\">";
+
+    _soap_footer ="</pointSet>";
+    _soap_footer+="</body>";
+    _soap_footer+="</transport>";
+    _soap_footer+="</ns2:dataRQ>";
+    _soap_footer+="</soapenv:Body>";
+    _soap_footer+="</soapenv:Envelope>";
+    _soap_footer+="\r\n\r\n";
+}
+
+int FIAP::post(struct fiap_element* v, unsigned int esize) {
+    HTTPClient http;
+    int i;
+    char requestBuffer[50];
+    string _date_time;
+    string _soap_text=_soap_header;
+    for (i=0; i<esize; i++) {
+        strftime(&requestBuffer[0],50,"%Y-%m-%dT%H:%M:%S.0000000",v[i].t);
+        _soap_text += "<point id=\"";
+        _soap_text += _fiap_id_prefix;
+        _soap_text += v[i].cid;
+        _soap_text += "\">";
+        _soap_text += "<value time=\"";
+        _soap_text += requestBuffer;
+        _soap_text += v[i].timezone;
+        _soap_text += "\">";
+        _soap_text += v[i].value;
+        _soap_text += "</value>";
+        _soap_text += "</point>";
+    }
+    _soap_text += _soap_footer;
+if(debug_mode){
+printf(_soap_text.c_str());
+printf("<<< Request(end)\n");
+}
+    http.setRequestHeader("Content-Type","text/xml; charset=UTF-8");
+    http.setRequestHeader("SOAPAction","\"http://soap.fiap.org/data\"");
+    HTTPText InData;
+    InData.set(_soap_text.c_str());
+    HTTPText OutData;
+    HTTPResult r = http.post(_fiap_storage.c_str(),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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fiap.h	Sun Jan 01 14:31:16 2012 +0000
@@ -0,0 +1,143 @@
+//  IEEE1888 / FIAP Uploader Library for Arduino
+//
+//  2011/09/19 ver.1   H.Inoue & H.Ochiai
+//  2011/12/31 Ver.1 for mbed Y.Tauchi
+
+// --------- FIAP.h  ---------
+#ifndef MBED_FIAP_H
+#define MBED_FIAP_H
+
+#include "mbed.h"
+#include "HTTPClient.h"
+
+// return code of post method
+#define FIAP_UPLOAD_OK       0  // Succeeded
+#define FIAP_UPLOAD_CONNFAIL 1  // Connection faild (Socket I/O error)
+#define FIAP_UPLOAD_DNSERR   2  // DNS error
+#define FIAP_UPLOAD_HTTPERR  3  // HTTP Server error (The response was not "200 OK")
+#define FIAP_UPLOAD_FIAPERR  4  // FIAP Server error
+
+// point element
+
+/** fiap_element 
+ *
+ *@param cid Point ID
+ *@param value Data
+ *@param t DateTime
+ *@param timezone Timezone ie JST="+09:00"
+ */
+struct fiap_element {
+  const char* cid;       // Set PointID
+  string value;           // Set Value
+  struct tm *t;           // Set DateTime
+  char* timezone;        // Set Timezon JST="+09:00"
+};
+
+/** FIAP Class
+ *
+ *  for mbed
+ *
+ *Example:
+ *@code
+ * #include "mbed.h"
+ * #include "EthernetNetIf.h"
+ * #include "NTPClient.h"
+ * #include "fiap.h"
+ *  
+ * #define ntp_server "ntp server address"
+ * 
+ * DigitalOut led1(LED1);
+ * DigitalOut led2(LED2);
+ * DigitalOut led3(LED3);
+ * DigitalOut led4(LED4);
+ * 
+ * EthernetNetIf eth;
+ * NTPClient ntp;
+ * FIAP ieee1888("http://192.168.1.5/axis2/services/FIAPStorage","http://test.fiap.org/mbed_hello/");
+ * 
+ * char timezone[] = "+09:00";  // JST
+ *
+ * struct fiap_element element[]={
+ *     {"P1",NULL,NULL,timezone},
+ *     {"P2",NULL,NULL,timezone},
+ * };
+ * 
+ * int main() {
+ *     led1=led2=led3=led4=0;
+ *     ieee1888.debug_mode=true;
+ *     //EthernetNetIf initialize
+ *     EthernetErr ethErr = eth.setup();
+ *     if (ethErr) {
+ *         //   lcd.locate(0,1);
+ *         //   lcd.printf("Error %d in setup.\n", ethErr);
+ *         return -1;
+ *     }
+ *     led1=1;
+ *     //NTPClient initia,lize
+ *     Host server(IpAddr(), 123, ntp_server);
+ *     NTPResult Ntpr=ntp.setTime(server);
+ *     //UTC-->JST +9Hour(32400Sec)
+ *     time_t ctTime;
+ *     ctTime = time(NULL);
+ *     ctTime+=32400;
+ *     set_time(ctTime);
+ *     led2=1;
+ * 
+ *     //post
+ *     int i,j;
+ *     for (j=0; j<10; j++) {
+ *         //data initialize
+ *         time_t seconds = time(NULL);
+ *         for (i=0; i<2; i++) {
+ *         char data[5];
+ *             int s=j*pow((double)10,i);
+ *             sprintf(data,"%03d",s);
+ *             element[i].value=data;
+ *             element[i].t=localtime(&seconds);
+ *         }
+ *         //do post
+ *         int ret=ieee1888.post(element,2);
+ *         if(ret!=0){
+ *         while (1) {
+ *         led3=!led3;
+ *         wait(0.5);
+ *     }
+ *         return 0;
+ *         }
+ *         wait(1);
+ *     }
+ *     //finish
+ *     while (1) {
+ *         led4=!led4;
+ *         wait(0.5);
+ *     }
+ * }
+ *@endcode 
+ */
+class FIAP {
+public: 
+/** Create fiap object 
+ * @param Storage FIAP Storage Addrees (ie. "http://fiap.org/axis2/services/FIAPStorage")
+ * @param PointSetId Point Set ID(ie. http://test.fiap.jp/bldg_1/3F/)
+ */
+ FIAP(string Storage,string PointSetId);
+
+/** post
+ * @param v data vaule
+ * @param esize  number of data
+ */
+  int post(struct fiap_element* v, unsigned int esize);
+
+/** debug_mode
+ *  Output XML and Error
+ */
+bool debug_mode;
+  
+private: 
+  string _fiap_storage;
+  string _fiap_id_prefix;
+  string _soap_header;
+  string _soap_footer;
+};
+
+#endif