eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PachubeV2CSV.cpp Source File

PachubeV2CSV.cpp

00001 /*******************************************************************************
00002 modify 
00003 2011/08
00004         - Use commonClient.
00005 
00006 *******************************************************************************/
00007 
00008 /**
00009  * Pachube API interface driver. (Version 0.0.1)
00010  *
00011  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00012  * http://shinta.main.jp/
00013  */
00014 
00015 #include "PachubeV2CSV.h"
00016 #include "EthernetNetIf.h"
00017 #include "HTTPClient.h"
00018 
00019 PachubeV2CSV::PachubeV2CSV(std::string APIKEY) : Pachube(APIKEY) {
00020 }
00021 
00022 PachubeV2CSV::~PachubeV2CSV() {
00023 }
00024 
00025 /*
00026  * =================================================================
00027  *  Environments (feeds)
00028  * =================================================================
00029  */
00030 
00031 /**
00032  * List all available feeds: GET /v2/feeds
00033  */
00034 int PachubeV2CSV::listAllAvailableFeeds(
00035         int page,
00036         int per_page,
00037         std::string content,
00038         std::string q,
00039         std::string tag,
00040         std::string user,
00041         std::string units,
00042         std::string status,
00043         std::string order,
00044         std::string &datatext) {
00045     HTTPClient client;
00046     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00047 
00048     HTTPText text("text/csv");
00049     text.set("");
00050     HTTPText data;
00051     
00052     /*
00053      * Example: http://api.pachube.com/v2/feeds
00054      */
00055     char tmp[32];
00056     sprintf(tmp, "?page=%d&per_page=%d", page, per_page);
00057     string URL = Pachube::URLBASE_V2 + "/feeds.csv" + std::string(tmp);
00058     
00059     if (content.length() > 0) {
00060         URL = URL + "&content=" + content;
00061     }
00062     if (q.length() > 0) {
00063         URL = URL + "&q=" + q;
00064     }
00065     if (tag.length() > 0) {
00066         URL = URL + "&tag=" + tag;
00067     }
00068     if (user.length() > 0) {
00069         URL = URL + "&user=" + user;
00070     }
00071     if (units.length() > 0) {
00072         URL = URL + "&units=" + units;
00073     }
00074     if (status.length() > 0) {
00075         URL = URL + "&status=" + status;
00076     }
00077     if (order.length() > 0) {
00078         URL = URL + "&order=" + order;
00079     }
00080 
00081     HTTPResult result = client.get(URL.c_str(), &data);
00082     datatext = data.get();
00083     
00084     return client.getHTTPResponseCode();
00085 }
00086 
00087 /**
00088  * Create new feed: POST /v2/feeds
00089  */
00090 int PachubeV2CSV::createNewFeed(void) {
00091     error("CSV is not supported for creating Environments because it cannot represent the required parameters due to its very simple format.");
00092     return 404;
00093 }
00094 
00095 /**
00096  * Read feed: GET /v2/feeds/<feed_id>
00097  */
00098 int PachubeV2CSV::readFeed(int feed_id, std::string &datatext) {
00099     HTTPClient client;
00100     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00101 
00102     HTTPText text("text/csv");
00103     text.set("");
00104     HTTPText data;
00105 
00106     /*
00107      * Example: http://api.pachube.com/v2/feeds/1977
00108      */
00109     char feed_id_char[32];
00110     sprintf(feed_id_char, "%d", feed_id);
00111     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + ".csv";
00112 
00113     HTTPResult result = client.get(URL.c_str(), &data);
00114     datatext = data.get();
00115     return client.getHTTPResponseCode();
00116 }
00117 
00118 /**
00119  * Update feed: PUT /v2/feeds/<feed_id>
00120  */
00121 int PachubeV2CSV::updateFeed(int feed_id) {
00122     HTTPClient client;
00123     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00124 
00125     HTTPText text("text/csv");
00126     text.set("");
00127 
00128     /*
00129      * Example: http://api.pachube.com/v2/feeds/1977?_method=put
00130      */
00131     char feed_id_char[32];
00132     sprintf(feed_id_char, "%d", feed_id);
00133     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=put";
00134 
00135     HTTPResult result = client.post(URL.c_str(), text, NULL);
00136     return client.getHTTPResponseCode();
00137 }
00138 
00139 /**
00140  * Delete feed: DELETE /v2/feeds/<feed_id>
00141  */
00142 int PachubeV2CSV::deleteFeed(int feed_id) {
00143     HTTPClient client;
00144     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00145 
00146     HTTPText text("text/csv");
00147     text.set("");
00148 
00149     /*
00150      * Example: http://api.pachube.com/v2/feeds/1977?_method=delete
00151      */
00152     char feed_id_char[32];
00153     sprintf(feed_id_char, "%d", feed_id);
00154     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=delete";
00155 
00156     HTTPResult result = client.post(URL.c_str(), text, NULL);
00157     return client.getHTTPResponseCode();
00158 }
00159 
00160 /*
00161  * =================================================================
00162  *  Datastreams
00163  * =================================================================
00164  */
00165 
00166 /**
00167  * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
00168  *
00169  * @param feed_id Feed ID.
00170  * @param stream_id Stream ID text.
00171  * @param value value.
00172  *
00173  * @return Return code from a web server.
00174  */
00175 int PachubeV2CSV::createNewDataStream(int feed_id, std::string stream_id, std::string value) {
00176     HTTPClient client;
00177     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00178 
00179     std::string data = stream_id + "," + value;
00180     HTTPText text("text/csv");
00181     text.set(data);
00182 
00183     /*
00184      * Example: http://api.pachube.com/v2/feeds/1977/datastreams
00185      */
00186     char feed_id_char[32];
00187     sprintf(feed_id_char, "%d", feed_id);
00188     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams";
00189 
00190     HTTPResult result = client.post(URL.c_str(), text, NULL);
00191     return client.getHTTPResponseCode();
00192 }
00193 
00194 /**
00195  * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
00196  *
00197  * @param feed_id Feed ID.
00198  * @param stream_id Stream ID text.
00199  *
00200  * @return Return code from a web server.
00201  */
00202 int PachubeV2CSV::readDataStream(int feed_id, std::string stream_id, std::string &datatext) {
00203     HTTPClient client;
00204     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00205 
00206     HTTPText text("text/csv");
00207     text.set("");
00208     HTTPText data;
00209 
00210     /*
00211      * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy
00212      */
00213     char feed_id_char[32];
00214     sprintf(feed_id_char, "%d", feed_id);
00215     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + ".csv";
00216 
00217     HTTPResult result = client.get(URL.c_str(), &data);
00218     datatext = data.get();
00219     return client.getHTTPResponseCode();
00220 }
00221 
00222 /**
00223  * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
00224  *
00225  * @param feed_id Feed ID.
00226  * @param stream_id Stream ID text.
00227  * @param value value.
00228  *
00229  * @return Return code from a web server.
00230  */
00231 //int PachubeV2CSV::updateDataStream(int feed_id, std::string stream_id, std::string value) {
00232 int PachubeV2CSV::updateDataStream(int feed_id, std::string stream_id, std::string value, HTTPClient *client) { //  2011/08
00233     //HTTPClient client;
00234     client->setRequestHeader(Pachube::REQUESTHEADER, APIKEY);   //  2011/08
00235 
00236     HTTPText text("text/csv");
00237     text.set(value);
00238 
00239     /*
00240      * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=put
00241      */
00242     char feed_id_char[32];
00243     sprintf(feed_id_char, "%d", feed_id);
00244     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=put";
00245     HTTPResult result = client->post(URL.c_str(), text, NULL);
00246     return client->getHTTPResponseCode();   // 2011/08
00247 }
00248 
00249 /**
00250  * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
00251  *
00252  * @param feed_id Feed ID.
00253  * @param stream_id Stream ID text.
00254  *
00255  * @return Return code from a web server.
00256  */
00257 int PachubeV2CSV::deleteDataStream(int feed_id, std::string stream_id) {
00258     HTTPClient client;
00259     client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
00260 
00261     HTTPText text("text/csv");
00262     text.set("");
00263 
00264     /*
00265      * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=delete
00266      */
00267     char feed_id_char[32];
00268     sprintf(feed_id_char, "%d", feed_id);
00269     const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=delete";
00270 
00271     HTTPResult result = client.post(URL.c_str(), text, NULL);
00272     return client.getHTTPResponseCode();
00273 }