http://http://diytec.web.fc2.com/mark2r2/

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Committer:
mark2r2
Date:
Tue Sep 20 12:46:26 2011 +0000
Revision:
0:08a4d61cd84c
V1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mark2r2 0:08a4d61cd84c 1 /*******************************************************************************
mark2r2 0:08a4d61cd84c 2 modify
mark2r2 0:08a4d61cd84c 3 2011/08
mark2r2 0:08a4d61cd84c 4 - Use commonClient.
mark2r2 0:08a4d61cd84c 5
mark2r2 0:08a4d61cd84c 6 *******************************************************************************/
mark2r2 0:08a4d61cd84c 7
mark2r2 0:08a4d61cd84c 8 /**
mark2r2 0:08a4d61cd84c 9 * Pachube API interface driver. (Version 0.0.1)
mark2r2 0:08a4d61cd84c 10 *
mark2r2 0:08a4d61cd84c 11 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
mark2r2 0:08a4d61cd84c 12 * http://shinta.main.jp/
mark2r2 0:08a4d61cd84c 13 */
mark2r2 0:08a4d61cd84c 14
mark2r2 0:08a4d61cd84c 15 #include "PachubeV2CSV.h"
mark2r2 0:08a4d61cd84c 16 #include "EthernetNetIf.h"
mark2r2 0:08a4d61cd84c 17 #include "HTTPClient.h"
mark2r2 0:08a4d61cd84c 18
mark2r2 0:08a4d61cd84c 19 PachubeV2CSV::PachubeV2CSV(std::string APIKEY) : Pachube(APIKEY) {
mark2r2 0:08a4d61cd84c 20 }
mark2r2 0:08a4d61cd84c 21
mark2r2 0:08a4d61cd84c 22 PachubeV2CSV::~PachubeV2CSV() {
mark2r2 0:08a4d61cd84c 23 }
mark2r2 0:08a4d61cd84c 24
mark2r2 0:08a4d61cd84c 25 /*
mark2r2 0:08a4d61cd84c 26 * =================================================================
mark2r2 0:08a4d61cd84c 27 * Environments (feeds)
mark2r2 0:08a4d61cd84c 28 * =================================================================
mark2r2 0:08a4d61cd84c 29 */
mark2r2 0:08a4d61cd84c 30
mark2r2 0:08a4d61cd84c 31 /**
mark2r2 0:08a4d61cd84c 32 * List all available feeds: GET /v2/feeds
mark2r2 0:08a4d61cd84c 33 */
mark2r2 0:08a4d61cd84c 34 int PachubeV2CSV::listAllAvailableFeeds(
mark2r2 0:08a4d61cd84c 35 int page,
mark2r2 0:08a4d61cd84c 36 int per_page,
mark2r2 0:08a4d61cd84c 37 std::string content,
mark2r2 0:08a4d61cd84c 38 std::string q,
mark2r2 0:08a4d61cd84c 39 std::string tag,
mark2r2 0:08a4d61cd84c 40 std::string user,
mark2r2 0:08a4d61cd84c 41 std::string units,
mark2r2 0:08a4d61cd84c 42 std::string status,
mark2r2 0:08a4d61cd84c 43 std::string order,
mark2r2 0:08a4d61cd84c 44 std::string &datatext) {
mark2r2 0:08a4d61cd84c 45 HTTPClient client;
mark2r2 0:08a4d61cd84c 46 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 47
mark2r2 0:08a4d61cd84c 48 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 49 text.set("");
mark2r2 0:08a4d61cd84c 50 HTTPText data;
mark2r2 0:08a4d61cd84c 51
mark2r2 0:08a4d61cd84c 52 /*
mark2r2 0:08a4d61cd84c 53 * Example: http://api.pachube.com/v2/feeds
mark2r2 0:08a4d61cd84c 54 */
mark2r2 0:08a4d61cd84c 55 char tmp[32];
mark2r2 0:08a4d61cd84c 56 sprintf(tmp, "?page=%d&per_page=%d", page, per_page);
mark2r2 0:08a4d61cd84c 57 string URL = Pachube::URLBASE_V2 + "/feeds.csv" + std::string(tmp);
mark2r2 0:08a4d61cd84c 58
mark2r2 0:08a4d61cd84c 59 if (content.length() > 0) {
mark2r2 0:08a4d61cd84c 60 URL = URL + "&content=" + content;
mark2r2 0:08a4d61cd84c 61 }
mark2r2 0:08a4d61cd84c 62 if (q.length() > 0) {
mark2r2 0:08a4d61cd84c 63 URL = URL + "&q=" + q;
mark2r2 0:08a4d61cd84c 64 }
mark2r2 0:08a4d61cd84c 65 if (tag.length() > 0) {
mark2r2 0:08a4d61cd84c 66 URL = URL + "&tag=" + tag;
mark2r2 0:08a4d61cd84c 67 }
mark2r2 0:08a4d61cd84c 68 if (user.length() > 0) {
mark2r2 0:08a4d61cd84c 69 URL = URL + "&user=" + user;
mark2r2 0:08a4d61cd84c 70 }
mark2r2 0:08a4d61cd84c 71 if (units.length() > 0) {
mark2r2 0:08a4d61cd84c 72 URL = URL + "&units=" + units;
mark2r2 0:08a4d61cd84c 73 }
mark2r2 0:08a4d61cd84c 74 if (status.length() > 0) {
mark2r2 0:08a4d61cd84c 75 URL = URL + "&status=" + status;
mark2r2 0:08a4d61cd84c 76 }
mark2r2 0:08a4d61cd84c 77 if (order.length() > 0) {
mark2r2 0:08a4d61cd84c 78 URL = URL + "&order=" + order;
mark2r2 0:08a4d61cd84c 79 }
mark2r2 0:08a4d61cd84c 80
mark2r2 0:08a4d61cd84c 81 HTTPResult result = client.get(URL.c_str(), &data);
mark2r2 0:08a4d61cd84c 82 datatext = data.get();
mark2r2 0:08a4d61cd84c 83
mark2r2 0:08a4d61cd84c 84 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 85 }
mark2r2 0:08a4d61cd84c 86
mark2r2 0:08a4d61cd84c 87 /**
mark2r2 0:08a4d61cd84c 88 * Create new feed: POST /v2/feeds
mark2r2 0:08a4d61cd84c 89 */
mark2r2 0:08a4d61cd84c 90 int PachubeV2CSV::createNewFeed(void) {
mark2r2 0:08a4d61cd84c 91 error("CSV is not supported for creating Environments because it cannot represent the required parameters due to its very simple format.");
mark2r2 0:08a4d61cd84c 92 return 404;
mark2r2 0:08a4d61cd84c 93 }
mark2r2 0:08a4d61cd84c 94
mark2r2 0:08a4d61cd84c 95 /**
mark2r2 0:08a4d61cd84c 96 * Read feed: GET /v2/feeds/<feed_id>
mark2r2 0:08a4d61cd84c 97 */
mark2r2 0:08a4d61cd84c 98 int PachubeV2CSV::readFeed(int feed_id, std::string &datatext) {
mark2r2 0:08a4d61cd84c 99 HTTPClient client;
mark2r2 0:08a4d61cd84c 100 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 101
mark2r2 0:08a4d61cd84c 102 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 103 text.set("");
mark2r2 0:08a4d61cd84c 104 HTTPText data;
mark2r2 0:08a4d61cd84c 105
mark2r2 0:08a4d61cd84c 106 /*
mark2r2 0:08a4d61cd84c 107 * Example: http://api.pachube.com/v2/feeds/1977
mark2r2 0:08a4d61cd84c 108 */
mark2r2 0:08a4d61cd84c 109 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 110 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 111 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + ".csv";
mark2r2 0:08a4d61cd84c 112
mark2r2 0:08a4d61cd84c 113 HTTPResult result = client.get(URL.c_str(), &data);
mark2r2 0:08a4d61cd84c 114 datatext = data.get();
mark2r2 0:08a4d61cd84c 115 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 116 }
mark2r2 0:08a4d61cd84c 117
mark2r2 0:08a4d61cd84c 118 /**
mark2r2 0:08a4d61cd84c 119 * Update feed: PUT /v2/feeds/<feed_id>
mark2r2 0:08a4d61cd84c 120 */
mark2r2 0:08a4d61cd84c 121 int PachubeV2CSV::updateFeed(int feed_id) {
mark2r2 0:08a4d61cd84c 122 HTTPClient client;
mark2r2 0:08a4d61cd84c 123 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 124
mark2r2 0:08a4d61cd84c 125 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 126 text.set("");
mark2r2 0:08a4d61cd84c 127
mark2r2 0:08a4d61cd84c 128 /*
mark2r2 0:08a4d61cd84c 129 * Example: http://api.pachube.com/v2/feeds/1977?_method=put
mark2r2 0:08a4d61cd84c 130 */
mark2r2 0:08a4d61cd84c 131 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 132 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 133 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=put";
mark2r2 0:08a4d61cd84c 134
mark2r2 0:08a4d61cd84c 135 HTTPResult result = client.post(URL.c_str(), text, NULL);
mark2r2 0:08a4d61cd84c 136 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 137 }
mark2r2 0:08a4d61cd84c 138
mark2r2 0:08a4d61cd84c 139 /**
mark2r2 0:08a4d61cd84c 140 * Delete feed: DELETE /v2/feeds/<feed_id>
mark2r2 0:08a4d61cd84c 141 */
mark2r2 0:08a4d61cd84c 142 int PachubeV2CSV::deleteFeed(int feed_id) {
mark2r2 0:08a4d61cd84c 143 HTTPClient client;
mark2r2 0:08a4d61cd84c 144 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 145
mark2r2 0:08a4d61cd84c 146 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 147 text.set("");
mark2r2 0:08a4d61cd84c 148
mark2r2 0:08a4d61cd84c 149 /*
mark2r2 0:08a4d61cd84c 150 * Example: http://api.pachube.com/v2/feeds/1977?_method=delete
mark2r2 0:08a4d61cd84c 151 */
mark2r2 0:08a4d61cd84c 152 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 153 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 154 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "?_method=delete";
mark2r2 0:08a4d61cd84c 155
mark2r2 0:08a4d61cd84c 156 HTTPResult result = client.post(URL.c_str(), text, NULL);
mark2r2 0:08a4d61cd84c 157 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 158 }
mark2r2 0:08a4d61cd84c 159
mark2r2 0:08a4d61cd84c 160 /*
mark2r2 0:08a4d61cd84c 161 * =================================================================
mark2r2 0:08a4d61cd84c 162 * Datastreams
mark2r2 0:08a4d61cd84c 163 * =================================================================
mark2r2 0:08a4d61cd84c 164 */
mark2r2 0:08a4d61cd84c 165
mark2r2 0:08a4d61cd84c 166 /**
mark2r2 0:08a4d61cd84c 167 * Create new datastream: POST /v2/feeds/<feed_id>/datastreams
mark2r2 0:08a4d61cd84c 168 *
mark2r2 0:08a4d61cd84c 169 * @param feed_id Feed ID.
mark2r2 0:08a4d61cd84c 170 * @param stream_id Stream ID text.
mark2r2 0:08a4d61cd84c 171 * @param value value.
mark2r2 0:08a4d61cd84c 172 *
mark2r2 0:08a4d61cd84c 173 * @return Return code from a web server.
mark2r2 0:08a4d61cd84c 174 */
mark2r2 0:08a4d61cd84c 175 int PachubeV2CSV::createNewDataStream(int feed_id, std::string stream_id, std::string value) {
mark2r2 0:08a4d61cd84c 176 HTTPClient client;
mark2r2 0:08a4d61cd84c 177 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 178
mark2r2 0:08a4d61cd84c 179 std::string data = stream_id + "," + value;
mark2r2 0:08a4d61cd84c 180 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 181 text.set(data);
mark2r2 0:08a4d61cd84c 182
mark2r2 0:08a4d61cd84c 183 /*
mark2r2 0:08a4d61cd84c 184 * Example: http://api.pachube.com/v2/feeds/1977/datastreams
mark2r2 0:08a4d61cd84c 185 */
mark2r2 0:08a4d61cd84c 186 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 187 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 188 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams";
mark2r2 0:08a4d61cd84c 189
mark2r2 0:08a4d61cd84c 190 HTTPResult result = client.post(URL.c_str(), text, NULL);
mark2r2 0:08a4d61cd84c 191 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 192 }
mark2r2 0:08a4d61cd84c 193
mark2r2 0:08a4d61cd84c 194 /**
mark2r2 0:08a4d61cd84c 195 * Read datastream - GET /v2/feeds/<feed_id>/datastreams/<datastream_id>
mark2r2 0:08a4d61cd84c 196 *
mark2r2 0:08a4d61cd84c 197 * @param feed_id Feed ID.
mark2r2 0:08a4d61cd84c 198 * @param stream_id Stream ID text.
mark2r2 0:08a4d61cd84c 199 *
mark2r2 0:08a4d61cd84c 200 * @return Return code from a web server.
mark2r2 0:08a4d61cd84c 201 */
mark2r2 0:08a4d61cd84c 202 int PachubeV2CSV::readDataStream(int feed_id, std::string stream_id, std::string &datatext) {
mark2r2 0:08a4d61cd84c 203 HTTPClient client;
mark2r2 0:08a4d61cd84c 204 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 205
mark2r2 0:08a4d61cd84c 206 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 207 text.set("");
mark2r2 0:08a4d61cd84c 208 HTTPText data;
mark2r2 0:08a4d61cd84c 209
mark2r2 0:08a4d61cd84c 210 /*
mark2r2 0:08a4d61cd84c 211 * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy
mark2r2 0:08a4d61cd84c 212 */
mark2r2 0:08a4d61cd84c 213 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 214 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 215 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + ".csv";
mark2r2 0:08a4d61cd84c 216
mark2r2 0:08a4d61cd84c 217 HTTPResult result = client.get(URL.c_str(), &data);
mark2r2 0:08a4d61cd84c 218 datatext = data.get();
mark2r2 0:08a4d61cd84c 219 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 220 }
mark2r2 0:08a4d61cd84c 221
mark2r2 0:08a4d61cd84c 222 /**
mark2r2 0:08a4d61cd84c 223 * Update datastream: PUT /v2/feeds/<feed_id>/datastreams/<datastream_id>
mark2r2 0:08a4d61cd84c 224 *
mark2r2 0:08a4d61cd84c 225 * @param feed_id Feed ID.
mark2r2 0:08a4d61cd84c 226 * @param stream_id Stream ID text.
mark2r2 0:08a4d61cd84c 227 * @param value value.
mark2r2 0:08a4d61cd84c 228 *
mark2r2 0:08a4d61cd84c 229 * @return Return code from a web server.
mark2r2 0:08a4d61cd84c 230 */
mark2r2 0:08a4d61cd84c 231 //int PachubeV2CSV::updateDataStream(int feed_id, std::string stream_id, std::string value) {
mark2r2 0:08a4d61cd84c 232 int PachubeV2CSV::updateDataStream(int feed_id, std::string stream_id, std::string value, HTTPClient *client) { // 2011/08
mark2r2 0:08a4d61cd84c 233 //HTTPClient client;
mark2r2 0:08a4d61cd84c 234 client->setRequestHeader(Pachube::REQUESTHEADER, APIKEY); // 2011/08
mark2r2 0:08a4d61cd84c 235
mark2r2 0:08a4d61cd84c 236 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 237 text.set(value);
mark2r2 0:08a4d61cd84c 238
mark2r2 0:08a4d61cd84c 239 /*
mark2r2 0:08a4d61cd84c 240 * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=put
mark2r2 0:08a4d61cd84c 241 */
mark2r2 0:08a4d61cd84c 242 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 243 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 244 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=put";
mark2r2 0:08a4d61cd84c 245 HTTPResult result = client->post(URL.c_str(), text, NULL);
mark2r2 0:08a4d61cd84c 246 return client->getHTTPResponseCode(); // 2011/08
mark2r2 0:08a4d61cd84c 247 }
mark2r2 0:08a4d61cd84c 248
mark2r2 0:08a4d61cd84c 249 /**
mark2r2 0:08a4d61cd84c 250 * Delete datastream: DELETE /v2/feeds/<feed_id>/datastreams/<datastream_id>
mark2r2 0:08a4d61cd84c 251 *
mark2r2 0:08a4d61cd84c 252 * @param feed_id Feed ID.
mark2r2 0:08a4d61cd84c 253 * @param stream_id Stream ID text.
mark2r2 0:08a4d61cd84c 254 *
mark2r2 0:08a4d61cd84c 255 * @return Return code from a web server.
mark2r2 0:08a4d61cd84c 256 */
mark2r2 0:08a4d61cd84c 257 int PachubeV2CSV::deleteDataStream(int feed_id, std::string stream_id) {
mark2r2 0:08a4d61cd84c 258 HTTPClient client;
mark2r2 0:08a4d61cd84c 259 client.setRequestHeader(Pachube::REQUESTHEADER, APIKEY);
mark2r2 0:08a4d61cd84c 260
mark2r2 0:08a4d61cd84c 261 HTTPText text("text/csv");
mark2r2 0:08a4d61cd84c 262 text.set("");
mark2r2 0:08a4d61cd84c 263
mark2r2 0:08a4d61cd84c 264 /*
mark2r2 0:08a4d61cd84c 265 * Example: http://api.pachube.com/v2/feeds/1977/datastreams/energy?_method=delete
mark2r2 0:08a4d61cd84c 266 */
mark2r2 0:08a4d61cd84c 267 char feed_id_char[32];
mark2r2 0:08a4d61cd84c 268 sprintf(feed_id_char, "%d", feed_id);
mark2r2 0:08a4d61cd84c 269 const string URL = Pachube::URLBASE_V2 + "/feeds/" + std::string(feed_id_char) + "/datastreams/" + stream_id + "?_method=delete";
mark2r2 0:08a4d61cd84c 270
mark2r2 0:08a4d61cd84c 271 HTTPResult result = client.post(URL.c_str(), text, NULL);
mark2r2 0:08a4d61cd84c 272 return client.getHTTPResponseCode();
mark2r2 0:08a4d61cd84c 273 }