Skip to content

Commit 4667a87

Browse files
committed
add lastValue method to Feed object
1 parent 161076b commit 4667a87

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/AdafruitIO_Feed.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,44 @@ bool AdafruitIO_Feed::create()
143143
return status == 201;
144144
}
145145

146+
AdafruitIO_Data* AdafruitIO_Feed::lastValue()
147+
{
148+
// 15 extra for api path, 12 for /data/retain, 1 for null
149+
String url = "/api/v2/";
150+
url += _io->_username;
151+
url += "/feeds/";
152+
url += name;
153+
url += "/data/retain";
154+
155+
IO_DEBUG_PRINT("lastValue get ");
156+
IO_DEBUG_PRINTLN(url);
157+
158+
_io->_http->beginRequest();
159+
_io->_http->get(url.c_str());
160+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
161+
_io->_http->endRequest();
162+
163+
int status = _io->_http->responseStatusCode();
164+
String body = _io->_http->responseBody();
165+
166+
if (status >= 200 && status <= 299) {
167+
168+
if (body.length() > 0) {
169+
return new AdafruitIO_Data(this, body.c_str());
170+
}
171+
172+
} else {
173+
174+
IO_ERROR_PRINT("error retrieving lastValue, status: ");
175+
IO_ERROR_PRINTLN(status);
176+
IO_ERROR_PRINT("response body: ");
177+
IO_ERROR_PRINTLN(_io->_http->responseBody());
178+
179+
return NULL;
180+
181+
}
182+
}
183+
146184
void AdafruitIO_Feed::setLocation(double lat, double lon, double ele)
147185
{
148186
data->setLocation(lat, lon, ele);

src/AdafruitIO_Feed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AdafruitIO_Feed : public AdafruitIO_MQTT {
4747

4848
const char *name;
4949

50+
AdafruitIO_Data *lastValue();
5051
AdafruitIO_Data *data;
5152

5253
private:

0 commit comments

Comments
 (0)