Skip to content

Commit 0d75d6f

Browse files
committed
working feed exists call
1 parent c63d3e2 commit 0d75d6f

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

AdafruitIO_Feed.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ AdafruitIO_Feed::~AdafruitIO_Feed()
4747

4848
if(_topic)
4949
free(_topic);
50+
51+
if(_feed_url)
52+
free(_feed_url);
53+
54+
if(_create_url)
55+
free(_create_url);
5056
}
5157

5258
void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb)
@@ -108,6 +114,19 @@ bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele, int
108114
return _pub->publish(data->toCSV());
109115
}
110116

117+
bool AdafruitIO_Feed::exists()
118+
{
119+
_io->_http->get(_feed_url);
120+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
121+
int status = _io->_http->responseStatusCode();
122+
return status == 200;
123+
}
124+
125+
bool AdafruitIO_Feed::create()
126+
{
127+
128+
}
129+
111130
void AdafruitIO_Feed::setLocation(double lat, double lon, double ele)
112131
{
113132
data->setLocation(lat, lon, ele);
@@ -125,20 +144,33 @@ void AdafruitIO_Feed::subCallback(char *val, uint16_t len)
125144
void AdafruitIO_Feed::_init()
126145
{
127146

128-
// dynamically allocate memory for topic
147+
// dynamically allocate memory for mqtt topic and REST URLs
129148
_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 8)); // 8 extra chars for /f/, /csv & null termination
149+
_feed_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 16)); // 16 extra for api path & null term
150+
_create_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + 15)); // 15 extra for api path & null term
130151

131152
// init feed data
132153
data = new AdafruitIO_Data(this);
133154

134-
if(_topic) {
155+
if(_topic && _create_url && _feed_url) {
135156

136157
// build topic string
137158
strcpy(_topic, _io->_username);
138159
strcat(_topic, "/f/");
139160
strcat(_topic, name);
140161
strcat(_topic, "/csv");
141162

163+
// build feed url string
164+
strcpy(_feed_url, "/api/v2/");
165+
strcat(_feed_url, _io->_username);
166+
strcat(_feed_url, "/feeds/");
167+
strcat(_feed_url, name);
168+
169+
// build create url string
170+
strcpy(_create_url, "/api/v2/");
171+
strcat(_create_url, _io->_username);
172+
strcat(_create_url, "/feeds");
173+
142174
// setup subscription
143175
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic);
144176
_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _topic);
@@ -150,6 +182,8 @@ void AdafruitIO_Feed::_init()
150182

151183
// malloc failed
152184
_topic = 0;
185+
_create_url = 0;
186+
_feed_url = 0;
153187
_sub = 0;
154188
_pub = 0;
155189
data = 0;

AdafruitIO_Feed.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class AdafruitIO_Feed {
3939
bool save(float value, double lat=0, double lon=0, double ele=0, int precision=6);
4040
bool save(double value, double lat=0, double lon=0, double ele=0, int precision=6);
4141

42+
bool exists();
43+
bool create();
44+
4245
void setLocation(double lat, double lon, double ele=0);
4346

4447
void subCallback(char *val, uint16_t len);
@@ -50,6 +53,8 @@ class AdafruitIO_Feed {
5053
void _init();
5154

5255
char *_topic;
56+
char *_create_url;
57+
char *_feed_url;
5358

5459
Adafruit_MQTT_Subscribe *_sub;
5560
Adafruit_MQTT_Publish *_pub;

0 commit comments

Comments
 (0)