Hi. This is the feed program for Cosm. (The previous name of the services is Pachube.)

Dependencies:   mbed ThermistorPack Pachube ConfigFile EthernetNetIf TextLCD HTTPClient_ToBeRemoved FatFileSystem SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PachubeV2CSV.cpp Source File

PachubeV2CSV.cpp

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