
ControllerBox directs electromechanical equipment in restaurants to display information.
Dependencies: TextLCD MbedJSONValue libMotiv picojson mbed-rtos mbed
Fork of Mbed_MotiVControllerBox by
Diff: libMotiv/MotiVAPI.cpp
- Revision:
- 7:dd5bf173b5e6
- Parent:
- 5:e22a1ae893d2
- Child:
- 8:d21d3b67fbb2
--- a/libMotiv/MotiVAPI.cpp Tue Jan 06 22:18:27 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -#include "MotiVAPI.h" -#include "EthernetInterface.h" -#include "HTTPClient.h" -#include "picojson.h" -#include "MbedJSONValue.h" -#include <string.h> -#include <sstream> - -MotiVAPI::MotiVAPI(std::string url) - : endpoint(url), - initialized(false) -{ - printf("MotiVAPI constructor\r\n"); -} - -MotiVAPI::~MotiVAPI() -{ - -} - - -MotiVAPI& MotiVAPI::operator=(const MotiVAPI& rhs) -{ - this->endpoint = rhs.endpoint; - this->httpClient = rhs.httpClient; - return *this; -} - -void MotiVAPI::init() -{ - initialized = true; -} - -bool MotiVAPI::connectAPI() -{ - return false; -} - -picojson::value MotiVAPI::getTable(int id) -{ - picojson::value jsonReply; - std::ostringstream stream; - stream << "/tables/" << id; - std::string request = stream.str(); - return getAPIData(request); -} - -picojson::value MotiVAPI::getTablesAll() -{ - printf("MotiVAPI::getTablesAll()\r\n"); - picojson::value jsonReply; - return getAPIData("/tables"); -} - -picojson::value MotiVAPI::getAPIData(string request) -{ - printf("MotiVAPI::getAPIData(string request)\r\n"); - string url = endpoint + request; - - char curl[96]; - strncpy(curl, url.c_str(), sizeof(curl)); - curl[sizeof(curl) - 1] = '\0'; - char httpResult[512]; - int ret = httpClient.get(curl, httpResult, 2048); - if (!ret) { - printf("Page fetched successfully - read %d characters\r\n", strlen(httpResult)); - printf("Result: %s\r\n", httpResult); - } else { - printf("Error - ret = %d - HTTP return code = %d\n", ret, httpClient.getHTTPResponseCode()); - } - - printf("Parse json\r\n"); - picojson::value jsonVal; - printf("no more malloccing\r\n"); - char *json = httpResult + '\0'; - printf("sum string copying"); -// strcpy(json, httpResult); - string err = picojson::parse(jsonVal, json, json + strlen(json)); - printf("res error? %s\r\n", err.c_str()); - - return jsonVal; -} - - - -// -// -// //GET data -// printf("\nTrying to fetch page...\r\n"); -// int ret = http.get("http://core.motiv.jvanbaarsen.com/v1/tables/7", str, 128); -// if (!ret) -// { -// printf("Page fetched successfully - read %d characters\r\n", strlen(str)); -// printf("Result: %s\r\n", str); -// c.lcdWriteLine(1, str); -// } -// else -// { -// printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); -// } -// -// picojson::value jsonVal; -// char *json = (char*) malloc(strlen(str)+1); -// strcpy(json, str); -// string err = picojson::parse(jsonVal, json, json + strlen(json)); -// printf("res error? %s\r\n", err.c_str()); -// -// string code = jsonVal.get("table").get("code").get<string>(); -// -// -//// int dinner_stat = json["table"]; -//// string code = table["code"]; -//// -//// printf("The status of the dinner is: %d\r\n", dinner_stat); -// printf("Code: %s\r\n", code);