Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
ELClientRest.cpp
Go to the documentation of this file.
1
7// Copyright (c) 2016 by B. Runnels and T. von Eicken
8
9#include "ELClientRest.h"
10
27{
28 _elc = e;
29 remote_instance = -1;
30}
31
40void ELClientRest::restCallback(void *res)
41{
42 if (!res) return;
43
45
46 resp->popArg(&_status, sizeof(_status));
47 if (_elc->_debugEn) {
48 _elc->_debug->print("REST code ");
49 _elc->_debug->println(_status);
50 }
51
52 _len = resp->popArgPtr(&_data);
53}
54
78int ELClientRest::begin(const char* host, uint16_t port, boolean security)
79{
80 uint8_t sec = !!security;
81 restCb.attach(this, &ELClientRest::restCallback);
82
83 _elc->Request(CMD_REST_SETUP, (uint32_t)&restCb, 3);
84 _elc->Request(host, strlen(host));
85 _elc->Request(&port, 2);
86 _elc->Request(&sec, 1);
87 _elc->Request();
88
89 ELClientPacket *pkt = _elc->WaitReturn();
90 if (pkt && (int32_t)pkt->value >= 0) {
91 remote_instance = pkt->value;
92 return 0;
93 }
94 return (int)pkt->value;
95}
96
112void ELClientRest::request(const char* path, const char* method, const char* data, int len)
113{
114 _status = 0;
115 if (remote_instance < 0) return;
116 if (data != 0 && len > 0) _elc->Request(CMD_REST_REQUEST, remote_instance, 3);
117 else _elc->Request(CMD_REST_REQUEST, remote_instance, 2);
118 _elc->Request(method, strlen(method));
119 _elc->Request(path, strlen(path));
120 if (data != NULL && len > 0) {
121 _elc->Request(data, len);
122 }
123
124 _elc->Request();
125}
126
141void ELClientRest::request(const char* path, const char* method, const char* data)
142{
143 request(path, method, data, strlen(data));
144}
145
159void ELClientRest::get(const char* path, const char* data) { request(path, "GET", data); }
160
192void ELClientRest::post(const char* path, const char* data) { request(path, "POST", data); }
193
206void ELClientRest::put(const char* path, const char* data) { request(path, "PUT", data); }
207
217void ELClientRest::del(const char* path) { request(path, "DELETE", 0); }
218
229void ELClientRest::setHeader(const char* value)
230{
231 uint8_t header_index = HEADER_GENERIC;
232 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2);
233 _elc->Request(&header_index, 1);
234 _elc->Request(value, strlen(value));
235 _elc->Request();
236}
237
248void ELClientRest::setContentType(const char* value)
249{
250 uint8_t header_index = HEADER_CONTENT_TYPE;
251 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2);
252 _elc->Request(&header_index, 1);
253 _elc->Request(value, strlen(value));
254 _elc->Request();
255}
256
267void ELClientRest::setUserAgent(const char* value)
268{
269 uint8_t header_index = HEADER_USER_AGENT;
270 _elc->Request(CMD_REST_SETHEADER, remote_instance, 2);
271 _elc->Request(&header_index, 1);
272 _elc->Request(value, strlen(value));
273 _elc->Request();
274}
275
321uint16_t ELClientRest::getResponse(char* data, uint16_t maxLen)
322{
323 if (_status == 0) return 0;
324 memcpy(data, _data, _len>maxLen?maxLen:_len);
325 int16_t s = _status;
326 _status = 0;
327 return s;
328}
329
374uint16_t ELClientRest::waitResponse(char* data, uint16_t maxLen, uint32_t timeout)
375{
376 uint32_t wait = millis();
377 while (_status == 0 && (millis() - wait < timeout)) {
378 _elc->Process();
379 }
380 return getResponse(data, maxLen);
381}
HEADER_TYPE
@ HEADER_CONTENT_TYPE
@ HEADER_GENERIC
@ HEADER_USER_AGENT
Definitions for ELClientRes.
@ CMD_REST_SETHEADER
Definition ELClient.h:36
@ CMD_REST_SETUP
Definition ELClient.h:34
@ CMD_REST_REQUEST
Definition ELClient.h:35
int16_t popArg(void *data, uint16_t maxLen)
Extract an argument from the response packet.
int16_t popArgPtr(void **data)
Extract pointer to an argument from the response packet.
void setUserAgent(const char *value)
Set user agent of header.
void put(const char *path, const char *data)
Send PUT request to REST server.
void del(const char *path)
Send DELETE request to REST server.
ELClientRest(ELClient *e)
Constructor for ELClientRest.
int begin(const char *host, uint16_t port=80, boolean security=false)
Initialize communication to a REST server.
void get(const char *path, const char *data=NULL)
Send GET request to REST server.
void request(const char *path, const char *method, const char *data=NULL)
Send request to REST server.
void setHeader(const char *value)
Set generic header content.
uint16_t waitResponse(char *data, uint16_t maxLen, uint32_t timeout=DEFAULT_REST_TIMEOUT)
Wait for the response.
void post(const char *path, const char *data)
Send POST request to REST server.
void setContentType(const char *value)
Set content type of header.
uint16_t getResponse(char *data, uint16_t maxLen)
Retrieve response.
ELClientPacket * Process(void)
Handle serial input.
Definition ELClient.cpp:115
void Request(uint16_t cmd, uint32_t value, uint16_t argc)
Start a request.
Definition ELClient.cpp:211
boolean _debugEn
Definition ELClient.h:125
ELClientPacket * WaitReturn(uint32_t timeout=ESP_TIMEOUT)
Wait for a response from ESP for a given timeout.
Definition ELClient.cpp:444
Stream * _debug
Definition ELClient.h:85
void attach(T *item, retT(T::*method)(argT))
Definition FP.h:147
uint32_t value
uint32_t wait