ControllerBox directs electromechanical equipment in restaurants to display information.

Dependencies:   TextLCD MbedJSONValue libMotiv picojson mbed-rtos mbed

Fork of Mbed_MotiVControllerBox by Tim Wöstemeier

Revision:
5:e22a1ae893d2
Parent:
3:21c8adb97c8f
Child:
6:32e6f0048bf1
--- a/libMotiv/MotiVAPI.cpp	Sun Dec 07 13:18:27 2014 +0000
+++ b/libMotiv/MotiVAPI.cpp	Tue Jan 06 22:18:27 2015 +0000
@@ -1,21 +1,115 @@
-//#include "MotiVAPI.h"
-//#include "EthernetInterface.h"
-//#include <string.h>
-//
-//MotiVAPI::MotiVAPI(std::string url)
-//    ://_eni(),
-//     _url(url)
-//{
-//    _eth.init();
-//}
+#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;
+}
+
+
+
 //
-//MotiVAPI::~MotiVAPI()
-//{
 //
-//}
+//        //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());
+//    }
 //
-//bool MotiVAPI::connectAPI()
-//{
+//    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());
 //
-//    return false;
-//}
\ No newline at end of file
+//    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);