1 | /*
|
---|
2 | ###############################################################################
|
---|
3 | #
|
---|
4 | # Temboo Arduino library
|
---|
5 | #
|
---|
6 | # Copyright 2014, Temboo Inc.
|
---|
7 | #
|
---|
8 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
9 | # you may not use this file except in compliance with the License.
|
---|
10 | # You may obtain a copy of the License at
|
---|
11 | #
|
---|
12 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
13 | #
|
---|
14 | # Unless required by applicable law or agreed to in writing,
|
---|
15 | # software distributed under the License is distributed on an
|
---|
16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
---|
17 | # either express or implied. See the License for the specific
|
---|
18 | # language governing permissions and limitations under the License.
|
---|
19 | #
|
---|
20 | ###############################################################################
|
---|
21 | */
|
---|
22 |
|
---|
23 |
|
---|
24 | #if defined (ARDUINO_AVR_YUN) || defined (ARDUINO_AVR_TRE)
|
---|
25 |
|
---|
26 | ///////////////////////////////////////////////////////
|
---|
27 | // BEGIN ARDUINO YUN AND TRE SUPPORT
|
---|
28 | ///////////////////////////////////////////////////////
|
---|
29 |
|
---|
30 | #include <Temboo.h>
|
---|
31 |
|
---|
32 | void TembooChoreo::begin() {
|
---|
33 | Process::begin("temboo");
|
---|
34 | }
|
---|
35 |
|
---|
36 | void TembooChoreo::setAccountName(const String& accountName) {
|
---|
37 | addParameter("-a" + accountName);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void TembooChoreo::setAppKeyName(const String& appKeyName) {
|
---|
41 | addParameter("-u" + appKeyName);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void TembooChoreo::setAppKey(const String& appKey) {
|
---|
45 | addParameter("-p" + appKey);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void TembooChoreo::setChoreo(const String& choreo) {
|
---|
49 | addParameter("-c" + choreo);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void TembooChoreo::setCredential(const String& credentialName) {
|
---|
53 | addParameter("-e" + credentialName);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void TembooChoreo::setSavedInputs(const String& savedInputsName) {
|
---|
57 | addParameter("-e" + savedInputsName);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void TembooChoreo::setProfile(const String& profileName) {
|
---|
61 | addParameter("-e" + profileName);
|
---|
62 | }
|
---|
63 |
|
---|
64 | void TembooChoreo::addInput(const String& inputName, const String& inputValue) {
|
---|
65 | addParameter("-i" + inputName + ":" + inputValue);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void TembooChoreo::addOutputFilter(const String& outputName, const String& filterPath, const String& variableName) {
|
---|
69 | addParameter("-o" + outputName + ":" + filterPath + ":" + variableName);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void TembooChoreo::setSettingsFileToWrite(const String& filePath) {
|
---|
73 | addParameter("-w" + filePath);
|
---|
74 | }
|
---|
75 |
|
---|
76 | void TembooChoreo::setSettingsFileToRead(const String& filePath) {
|
---|
77 | addParameter("-r" + filePath);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | #else //ARDUINO_AVR_YUN
|
---|
82 |
|
---|
83 | ///////////////////////////////////////////////////////
|
---|
84 | // BEGIN ARDUINO NON-YUN SUPPORT
|
---|
85 | ///////////////////////////////////////////////////////
|
---|
86 |
|
---|
87 | #include <string.h>
|
---|
88 | #include <Client.h>
|
---|
89 | #include <avr/pgmspace.h>
|
---|
90 | #include <Temboo.h>
|
---|
91 | #include "utility/TembooGlobal.h"
|
---|
92 | #include "utility/TembooSession.h"
|
---|
93 |
|
---|
94 | static const char HTTP_CODE[] PROGMEM = "HTTP_CODE\x0A\x1F";
|
---|
95 |
|
---|
96 | TembooChoreo::TembooChoreo(Client& client) : m_client(client) {
|
---|
97 | m_accountName = NULL;
|
---|
98 | m_appKeyName = NULL;
|
---|
99 | m_appKeyValue = NULL;
|
---|
100 | m_path = NULL;
|
---|
101 | m_nextChar = NULL;
|
---|
102 | m_nextState = END;
|
---|
103 | }
|
---|
104 |
|
---|
105 | void TembooChoreo::setAccountName(const String& accountName) {
|
---|
106 | m_accountName = accountName.c_str();
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | void TembooChoreo::setAccountName(const char* accountName) {
|
---|
111 | m_accountName = accountName;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | void TembooChoreo::setAppKeyName(const String& appKeyName) {
|
---|
116 | m_appKeyName = appKeyName.c_str();
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void TembooChoreo::setAppKeyName(const char* appKeyName) {
|
---|
121 | m_appKeyName = appKeyName;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | void TembooChoreo::setAppKey(const String& appKeyValue) {
|
---|
126 | m_appKeyValue = appKeyValue.c_str();
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | void TembooChoreo::setAppKey(const char* appKeyValue) {
|
---|
131 | m_appKeyValue = appKeyValue;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | void TembooChoreo::setChoreo(const String& path) {
|
---|
136 | m_path = path.c_str();
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | void TembooChoreo::setChoreo(const char* path) {
|
---|
141 | m_path = path;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void TembooChoreo::setSavedInputs(const String& savedInputsName) {
|
---|
146 | m_preset.put(savedInputsName.c_str());
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | void TembooChoreo::setSavedInputs(const char* savedInputsName) {
|
---|
151 | m_preset.put(savedInputsName);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void TembooChoreo::setCredential(const String& credentialName) {
|
---|
156 | m_preset.put(credentialName.c_str());
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void TembooChoreo::setCredential(const char* credentialName) {
|
---|
161 | m_preset.put(credentialName);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void TembooChoreo::setProfile(const String& profileName) {
|
---|
165 | m_preset.put(profileName.c_str());
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void TembooChoreo::setProfile(const char* profileName) {
|
---|
170 | m_preset.put(profileName);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 | void TembooChoreo::addInput(const String& inputName, const String& inputValue) {
|
---|
176 | m_inputs.put(inputName.c_str(), inputValue.c_str());
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void TembooChoreo::addInput(const char* inputName, const char* inputValue) {
|
---|
181 | m_inputs.put(inputName, inputValue);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | void TembooChoreo::addInput(const char* inputName, const String& inputValue) {
|
---|
186 | m_inputs.put(inputName, inputValue.c_str());
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | void TembooChoreo::addInput(const String& inputName, const char* inputValue) {
|
---|
191 | m_inputs.put(inputName.c_str(), inputValue);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | void TembooChoreo::addOutputFilter(const char* outputName, const char* filterPath, const char* variableName) {
|
---|
196 | m_outputs.put(outputName, filterPath, variableName);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | void TembooChoreo::addOutputFilter(const String& outputName, const char* filterPath, const char* variableName) {
|
---|
201 | m_outputs.put(outputName.c_str(), filterPath, variableName);
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | void TembooChoreo::addOutputFilter(const char* outputName, const String& filterPath, const char* variableName) {
|
---|
206 | m_outputs.put(outputName, filterPath.c_str(), variableName);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | void TembooChoreo::addOutputFilter(const String& outputName, const String& filterPath, const char* variableName) {
|
---|
211 | m_outputs.put(outputName.c_str(), filterPath.c_str(), variableName);
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | void TembooChoreo::addOutputFilter(const char* outputName, const char* filterPath, const String& variableName) {
|
---|
216 | m_outputs.put(outputName, filterPath, variableName.c_str());
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | void TembooChoreo::addOutputFilter(const String& outputName, const char* filterPath, const String& variableName) {
|
---|
221 | m_outputs.put(outputName.c_str(), filterPath, variableName.c_str());
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | void TembooChoreo::addOutputFilter(const char* outputName, const String& filterPath, const String& variableName) {
|
---|
226 | m_outputs.put(outputName, filterPath.c_str(), variableName.c_str());
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void TembooChoreo::addOutputFilter(const String& outputName, const String& filterPath, const String& variableName) {
|
---|
231 | m_outputs.put(outputName.c_str(), filterPath.c_str(), variableName.c_str());
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | int TembooChoreo::run() {
|
---|
236 | return run(INADDR_NONE, 80);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | int TembooChoreo::run(IPAddress addr, uint16_t port) {
|
---|
241 |
|
---|
242 | m_nextChar = NULL;
|
---|
243 |
|
---|
244 | if (m_accountName == NULL || *m_accountName == '\0') {
|
---|
245 | return TEMBOO_ERROR_ACCOUNT_MISSING;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (m_path == NULL || *m_path == '\0') {
|
---|
249 | return TEMBOO_ERROR_CHOREO_MISSING;
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (m_appKeyName == NULL || *m_appKeyName == '\0') {
|
---|
253 | return TEMBOO_ERROR_APPKEY_NAME_MISSING;
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (m_appKeyValue == NULL || *m_appKeyValue == '\0') {
|
---|
257 | return TEMBOO_ERROR_APPKEY_MISSING;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | TembooSession session(m_client, addr, port);
|
---|
262 | uint16_t httpCode = 0;
|
---|
263 |
|
---|
264 | for (int i = 0; i < 2; i++) {
|
---|
265 | if (0 != session.executeChoreo(m_accountName, m_appKeyName, m_appKeyValue, m_path, m_inputs, m_outputs, m_preset)) {
|
---|
266 | httpCode = 0;
|
---|
267 | break;
|
---|
268 | }
|
---|
269 |
|
---|
270 | while(!m_client.available()) {
|
---|
271 | if (!m_client.connected()) {
|
---|
272 | TEMBOO_TRACELN("Disconnected");
|
---|
273 | return TEMBOO_ERROR_HTTP_ERROR;
|
---|
274 | }
|
---|
275 | delay(10);
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (!m_client.find("HTTP/1.1 ")) {
|
---|
279 | TEMBOO_TRACELN("No HTTP");
|
---|
280 | return TEMBOO_ERROR_HTTP_ERROR;
|
---|
281 | }
|
---|
282 |
|
---|
283 | httpCode = (uint16_t)m_client.parseInt();
|
---|
284 |
|
---|
285 | // We expect HTTP response codes to be <= 599, but
|
---|
286 | // we need to be prepared for anything.
|
---|
287 | if (httpCode >= 600) {
|
---|
288 | TEMBOO_TRACELN("Invalid HTTP");
|
---|
289 | httpCode = 0;
|
---|
290 | }
|
---|
291 |
|
---|
292 | // if we get an auth error AND there was an x-temboo-time header,
|
---|
293 | // update the session timeOffset
|
---|
294 | if ((httpCode == 401) && (i == 0)) {
|
---|
295 | if (m_client.find("x-temboo-time:")) {
|
---|
296 | TembooSession::setTime((unsigned long)m_client.parseInt());
|
---|
297 | while(m_client.available()) {
|
---|
298 | m_client.read();
|
---|
299 | }
|
---|
300 | m_client.stop();
|
---|
301 | }
|
---|
302 | } else {
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | uint16toa(httpCode, m_httpCodeStr);
|
---|
308 | strcat_P(m_httpCodeStr, PSTR("\x0A\x1E"));
|
---|
309 | m_nextState = START;
|
---|
310 | m_nextChar = HTTP_CODE;
|
---|
311 |
|
---|
312 | if (httpCode < 200 || httpCode >= 300) {
|
---|
313 | return TEMBOO_ERROR_HTTP_ERROR;
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (!m_client.find("\x0D\x0A\x0D\x0A")) {
|
---|
317 | return TEMBOO_ERROR_HTTP_ERROR;
|
---|
318 | }
|
---|
319 |
|
---|
320 | return TEMBOO_ERROR_OK;
|
---|
321 | }
|
---|
322 |
|
---|
323 | void TembooChoreo::close() {
|
---|
324 | m_client.stop();
|
---|
325 | }
|
---|
326 |
|
---|
327 | int TembooChoreo::available() {
|
---|
328 | // If we're still sending the HTTP response code,
|
---|
329 | // report at least one character available.
|
---|
330 | if (m_nextChar != NULL) {
|
---|
331 | return m_client.available() + 1;
|
---|
332 | }
|
---|
333 |
|
---|
334 | // Otherwise, return however many characters the client has.
|
---|
335 | return m_client.available();
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | int TembooChoreo::peek() {
|
---|
340 | // If we're still sending the HTTP response code,
|
---|
341 | // return the next character in that sequence.
|
---|
342 | if (m_nextChar != NULL) {
|
---|
343 | return (int)*m_nextChar;
|
---|
344 | }
|
---|
345 |
|
---|
346 | // Otherwise, return whatever is in the client buffer.
|
---|
347 | return m_client.peek();
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | int TembooChoreo::read() {
|
---|
352 |
|
---|
353 | int c = 0;
|
---|
354 | switch(m_nextState) {
|
---|
355 | case START:
|
---|
356 | m_nextChar = HTTP_CODE;
|
---|
357 | c = (int)pgm_read_byte(m_nextChar++);
|
---|
358 | m_nextState = HTTP_CODE_TAG;
|
---|
359 | break;
|
---|
360 |
|
---|
361 | case HTTP_CODE_TAG:
|
---|
362 | c = (int)pgm_read_byte(m_nextChar++);
|
---|
363 | if (pgm_read_byte(m_nextChar) == '\0') {
|
---|
364 | m_nextState = HTTP_CODE_VALUE;
|
---|
365 | m_nextChar = m_httpCodeStr;
|
---|
366 | }
|
---|
367 | break;
|
---|
368 |
|
---|
369 | case HTTP_CODE_VALUE:
|
---|
370 | c = (int)(*m_nextChar++);
|
---|
371 | if (*m_nextChar == '\0') {
|
---|
372 | m_nextState = END;
|
---|
373 | m_nextChar = NULL;
|
---|
374 | }
|
---|
375 | break;
|
---|
376 |
|
---|
377 | default:
|
---|
378 | c = m_client.read();
|
---|
379 | }
|
---|
380 | return c;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | size_t TembooChoreo::write(uint8_t data) {
|
---|
385 | return m_client.write(data);
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | void TembooChoreo::flush() {
|
---|
390 | m_nextChar = NULL;
|
---|
391 | m_nextState = END;
|
---|
392 | m_client.flush();
|
---|
393 | }
|
---|
394 |
|
---|
395 | #endif //ARDUINO_AVR_YUN
|
---|