@@ -47,6 +47,12 @@ AdafruitIO_Feed::~AdafruitIO_Feed()
47
47
48
48
if (_topic)
49
49
free (_topic);
50
+
51
+ if (_feed_url)
52
+ free (_feed_url);
53
+
54
+ if (_create_url)
55
+ free (_create_url);
50
56
}
51
57
52
58
void AdafruitIO_Feed::onMessage (AdafruitIODataCallbackType cb)
@@ -108,6 +114,19 @@ bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele, int
108
114
return _pub->publish (data->toCSV ());
109
115
}
110
116
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
+
111
130
void AdafruitIO_Feed::setLocation (double lat, double lon, double ele)
112
131
{
113
132
data->setLocation (lat, lon, ele);
@@ -125,20 +144,33 @@ void AdafruitIO_Feed::subCallback(char *val, uint16_t len)
125
144
void AdafruitIO_Feed::_init ()
126
145
{
127
146
128
- // dynamically allocate memory for topic
147
+ // dynamically allocate memory for mqtt topic and REST URLs
129
148
_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
130
151
131
152
// init feed data
132
153
data = new AdafruitIO_Data (this );
133
154
134
- if (_topic) {
155
+ if (_topic && _create_url && _feed_url ) {
135
156
136
157
// build topic string
137
158
strcpy (_topic, _io->_username );
138
159
strcat (_topic, " /f/" );
139
160
strcat (_topic, name);
140
161
strcat (_topic, " /csv" );
141
162
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
+
142
174
// setup subscription
143
175
_sub = new Adafruit_MQTT_Subscribe (_io->_mqtt , _topic);
144
176
_pub = new Adafruit_MQTT_Publish (_io->_mqtt , _topic);
@@ -150,6 +182,8 @@ void AdafruitIO_Feed::_init()
150
182
151
183
// malloc failed
152
184
_topic = 0 ;
185
+ _create_url = 0 ;
186
+ _feed_url = 0 ;
153
187
_sub = 0 ;
154
188
_pub = 0 ;
155
189
data = 0 ;
0 commit comments