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

Files at this revision

API Documentation at this revision

Comitter:
TimWoo
Date:
Tue Jan 06 22:18:27 2015 +0000
Parent:
4:8155d4d6a193
Child:
6:32e6f0048bf1
Child:
7:dd5bf173b5e6
Commit message:
Malloc call needed for picojson hangs the program silently... Don't see how to debug.; ; Going to try MbedJSONValue library instead

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show diff for this revision Revisions of this file
libMotiv/Controller.cpp Show annotated file Show diff for this revision Revisions of this file
libMotiv/Controller.h Show annotated file Show diff for this revision Revisions of this file
libMotiv/MotiVAPI.cpp Show annotated file Show diff for this revision Revisions of this file
libMotiv/MotiVAPI.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/EthernetInterface.lib	Sun Dec 07 13:18:27 2014 +0000
+++ b/EthernetInterface.lib	Tue Jan 06 22:18:27 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/TimWoo/code/EthernetInterface/#43677726a684
+http://developer.mbed.org/users/TimWoo/code/EthernetInterface/#537740f2a4c6
--- a/SDFileSystem.lib	Sun Dec 07 13:18:27 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/mbed/code/SDFileSystem/#7b35d1709458
--- a/libMotiv/Controller.cpp	Sun Dec 07 13:18:27 2014 +0000
+++ b/libMotiv/Controller.cpp	Tue Jan 06 22:18:27 2015 +0000
@@ -5,11 +5,15 @@
 #include "Device.h"
 #include "Table.h"
 #include "Flower.h"
+#include "picojson.h"
+#include "MotiVAPI.h"
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
 
 #include <exception>
-//#include <logic_error>
-//#include <runtime_error>
 #include <iostream>
+#include <sstream>
+
 //Flower commands
 //        fRESET = 0,
 //        fPETAL_SPD = 1,
@@ -84,49 +88,46 @@
 
 //#include "MotiVAPI.h"
 
+
+
+
+
 //Test endpoint: http://demo.motiv.jvanbaarsen.com/demo/test
 using namespace std;
 
-Controller::Controller(TextLCD *lcd):
-    status(WAITING)
+
+void debug_build_number()
 {
-    printf("Controller constructor\r\n");
-    this->lcd = lcd;
-    //setStatus(WAITING);
-//    _mapi = new MotiVAPI("http://demo.motiv.jvanbaarsen.com/demo/test");
-    displayStatus();
-//    devices.push_back(new <Device>Flower(1));
-//    devices.append(new Flower(2));
-//    devices.append(new Flower(3));
+    printf("\r\n\r\nBuild ");
+    printf(__DATE__);
+    printf(" - ");
+    printf(__TIME__);
+    printf("\r\n\r\n");
+}
 
-//    tables.append(new Table(1));
-//    tables.append(new Table(2));
-//    tables.append(new Table(3));
+Controller::Controller()
+{}
 
-    //tables stores Tables
-    //Tables can be updated with information
-    //Tables can be asked for commands to send
-    //Tables will check changes between new info (this API update) and old info (previous API update)
-    //Tables can use the Flower interface to build commands
+Controller::Controller(TextLCD *lcd/*, EthernetInterface *eth*/)
+    :   initialized(false),
+        lcd(lcd),
+        status(WAITING)
+{
+    debug_build_number(); //show build info
+    printf("Controller constructor\r\n");
 
-
-    //Would like to have:
-    //Sending commands in different thread than receiving api update
-    //Commands then put into shared object commandQueue (FIFO)
-
+    mAPI = new MotiVAPI("http://core.motiv.jvanbaarsen.com/v1/"); //Initialize API
 
-//    Test
-    Table table(1);
-    Table table2(2);
+    setStatus(WAITING);
+    displayStatus();
+}
 
-    vector<MCommand> insertVector;
-    insertVector = table.update(1,0,0,0);
-    printf("Updated first table\r\n");
-    commandList.insert(commandList.begin(), insertVector.begin(), insertVector.end());
-    printCommandList();
-    insertVector = table2.update(2,0,0,0);
-    printf("Updated second table\r\n");
-    commandList.insert(commandList.begin(), insertVector.begin(), insertVector.end());
+void Controller::init()
+{
+    initialized = true;
+    mAPI->init();
+    
+    initTables();
 }
 
 void Controller::printDesc(int c)
@@ -171,6 +172,7 @@
 
 void Controller::displayStatus()
 {
+    printf("Controller::displayStatus()\r\n");
     this->lcd->locate(0,0);
     this->lcd->clearLine();
     this->lcd->locate(0,0);
@@ -210,12 +212,97 @@
 
 void Controller::printCommandList()
 {
+    if(!initialized)
+    {
+        printf("Error: Controller must be initialized before calling %s!\r\n", __PRETTY_FUNCTION__);
+        return;
+    }
     printf("\r\nCommand list printing\r\n\r\n");
-    printf("The command list contains %d commands\r\n", commandList.size());
     if(commandList.size()) {
+        printf("The command list contains %d commands\r\n", commandList.size());
         for(std::vector<MCommand>::iterator it = commandList.begin(); it != commandList.end(); ++it) {
             /* std::cout << *it; ... */
             it->print();
         }
+    } else {
+        printf("Command list empty\r\n");
     }
 }
+
+void Controller::initTables()
+{
+    printf("initTables\r\n");
+    picojson::array allTables = mAPI->getTablesAll().get("tables").get<picojson::array>();
+    printf("get first table\r\n");
+    picojson::value table = (picojson::value)allTables.begin();
+    int tableId = table.get("id");
+    printf("First table id is %d\r\n", tableId);
+    //for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) {
+//        printf("menu item value =%s\r\n", (*iter).get("v").get<string>().c_str());
+//    }
+}
+
+
+void Controller::testRequest()
+{
+    mAPI->getTablesAll();
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//Controller::Controller(TextLCD *lcd):
+//    status(WAITING),
+//    lcd(lcd)
+//{
+//    debug_build_number();
+//    printf("Controller constructor\r\n");
+////    this->lcd = lcd;
+//    //setStatus(WAITING);
+////    mAPI = new MotiVAPI("http://core.motiv.jvanbaarsen.com/v1/");
+//    displayStatus();
+////    devices.push_back(new <Device>Flower(1));
+////    devices.append(new Flower(2));
+////    devices.append(new Flower(3));
+//
+////    tables.append(new Table(1));
+////    tables.append(new Table(2));
+////    tables.append(new Table(3));
+//
+//    //tables stores Tables
+//    //Tables can be updated with information
+//    //Tables can be asked for commands to send
+//    //Tables will check changes between new info (this API update) and old info (previous API update)
+//    //Tables can use the Flower interface to build commands
+//
+//
+//    //Would like to have:
+//    //Sending commands in different thread than receiving api update
+//    //Commands then put into shared object commandQueue (FIFO)
+//
+////    initTables();
+//
+////    Test
+////    Table table(1);
+////    Table table2(2);
+////    vector<MCommand> insertVector;
+////    insertVector = table.update(1,0,0,0);
+////    printf("Updated first table\r\n");
+////    commandList.insert(commandList.begin(), insertVector.begin(), insertVector.end());
+////    printCommandList();
+////    insertVector = table2.update(2,0,0,0);
+////    printf("Updated second table\r\n");
+////    commandList.insert(commandList.begin(), insertVector.begin(), insertVector.end());
+//}
\ No newline at end of file
--- a/libMotiv/Controller.h	Sun Dec 07 13:18:27 2014 +0000
+++ b/libMotiv/Controller.h	Tue Jan 06 22:18:27 2015 +0000
@@ -8,6 +8,9 @@
 #include "Device.h"
 #include "Flower.h"
 #include "Table.h"
+#include "picojson.h"
+#include "EthernetInterface.h"
+#include "HTTPClient.h"
 #include <stdlib.h>
 #include <vector>
 
@@ -20,7 +23,14 @@
         ERROR,
         INIT
     };
+    Controller();
     Controller(TextLCD *lcd);
+//    Controller(TextLCD *lcd, EthernetInterface *eth);
+    Controller& operator=(const Controller& c);
+    
+    
+    void init();
+    void testRequest();
     cStatus getStatus();
     string statusString();
     void setStatus(cStatus);
@@ -34,7 +44,6 @@
     void prevCommand();
     void sendCommandDem();
     
-    
     //DEBUG
     void printCommandList();
     
@@ -44,14 +53,21 @@
     void sendCommand();
     
     TextLCD *lcd;
-//    MotiVAPI *mapi;
+    MotiVAPI *mAPI;
+    
+    
+    
+    bool connectAPI();
     
 private:
+    bool initialized;
     cStatus status;
+    void initTables();
     
-
+    HTTPClient httpClient;
+    string endpoint;
 //    std::vector<Device*> devices;
-//    vector <Table>tables
+    vector <Table>tables;
     vector <MCommand> commandList;
 };
 
--- 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);
--- a/libMotiv/MotiVAPI.h	Sun Dec 07 13:18:27 2014 +0000
+++ b/libMotiv/MotiVAPI.h	Tue Jan 06 22:18:27 2015 +0000
@@ -1,28 +1,38 @@
-//#ifndef MOTIV_API_H
-//#define MOTIV_API_H
-//
-//#include "EthernetInterface.h"
-//#include <string>
-//
-//using namespace std;
-//
-//class MotiVAPI
-//{
-//    public:
-//    MotiVAPI();
-//    MotiVAPI(string url);
-//    ~MotiVAPI();
-//    
-//    MotiVAPI(const MotiVAPI&);
-//    MotiVAPI& operator=(const MotiVAPI&);
-//    
-//    EthernetInterface eth;
-//    bool connectAPI();
-//    
-//    private:
-//    //Add network connection
-//    string _url;
-//
-//};
-//
-//#endif
\ No newline at end of file
+#ifndef MOTIV_API_H
+#define MOTIV_API_H
+#include "HTTPClient.h"
+#include "picojson.h"
+#include "EthernetInterface.h"
+#include <string>
+
+using namespace std;
+
+class MotiVAPI
+{
+    public:
+    MotiVAPI();
+    MotiVAPI(string url);
+    ~MotiVAPI();
+    
+    MotiVAPI(const MotiVAPI&);
+    MotiVAPI& operator=(const MotiVAPI&);
+    
+    void init();
+    
+    picojson::value getTable(int id);
+    picojson::value getTablesAll();
+    
+    
+    
+    bool connectAPI();
+    
+    private:
+    //Add network connection
+    
+    string endpoint;
+    bool initialized;
+    picojson::value getAPIData(string request);
+    HTTPClient httpClient;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Sun Dec 07 13:18:27 2014 +0000
+++ b/main.cpp	Tue Jan 06 22:18:27 2015 +0000
@@ -2,9 +2,9 @@
 //the library "TextLCD.h" was slightly altered to work with the GDM2004D LCD
 #include "TextLCD/TextLCD.h"
 #include "Controller.h"
+#include "picojson.h"
 #include "EthernetInterface.h"
-#include "HTTPClient.h"
-#include "picojson.h"
+//#include "HTTPClient.h"
 //#include "SDFileSystem.h" Needed if config files are going to be implemented
 #include <string>
 
@@ -27,9 +27,9 @@
 Serial rfd(p9, p10); //tx, rx
 
 //TODO: Should be the responsibility of Controller
-EthernetInterface eth;
+EthernetInterface eth; //Doeesn't want to initialize in the objects Controller->mAPI
 HTTPClient http;
-char str[512];
+char str[1024];
 
 //Storage
 //SD card
@@ -42,7 +42,7 @@
 
 
 //Controller mainly directs the program. It directs data and also controls the lcd output.
-Controller c(&lcd);
+Controller c(&lcd/*, &eth*/);
 
 /**********************
     Declare functions
@@ -57,20 +57,17 @@
 *****************/
 int main()
 {
-    printf("\r\n\r\nBuild  ");
-    printf(__DATE__);
-    printf(" - ");
-    printf(__TIME__);
-    printf("\r\n\r\n");
-    setup();
-    c.lcd->cls();
-    //the LCD is cleared using function .cls()
-    lcd.cls();
-
+    eth.init();
+    eth.connect();
+    printf("int main()\r\n");
+    c.init();
     c.printCommandList();
-    pc.printf("Entering loop\r\n");
-
-    //the endless loop keeps mbed in low power mode
+    c.testRequest();
+//    setup();
+//
+//    pc.printf("Entering loop\r\n");
+//
+//    //the endless loop keeps mbed in low power mode
 //    runProgram();
 }
 
@@ -84,28 +81,18 @@
     c.setStatus(Controller::INIT);
     rfd.baud(9600);
 
-    //Initialize Ethernet
-//    eth.init();
-//    eth.connect();
     wait_ms(200);
 }
 
 void runProgram()
 {
 
-    c.lcd->locate(0,0);
-    c.displayStatus();
-//    c.printDesc(1);
-//    string ip = eth.getIPAddress();
-//    string mStr = "IP: " + ip + "\r\n";
-//    c.lcdWriteLine(3, mStr);
-//    pc.printf("%s", ip);
-////
 //
-//
+
+
 //        //GET data
 //    printf("\nTrying to fetch page...\r\n");
-//    int ret = http.get("http://core.motiv.jvanbaarsen.com/v1/tables/7", str, 128);
+//    int ret = http.get("http://core.motiv.jvanbaarsen.com/v1/tables", str, 128);
 //    if (!ret)
 //    {
 //      printf("Page fetched successfully - read %d characters\r\n", strlen(str));
@@ -126,17 +113,16 @@
 //    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);
+//    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); 
     
     c.setStatus(Controller::READY);
     pc.printf("Controller ready\r\n");
     
     c.printCommandList();
-
     //Reset flower
     rfd.putc(2);
     rfd.putc(0);
--- a/mbed-rtos.lib	Sun Dec 07 13:18:27 2014 +0000
+++ b/mbed-rtos.lib	Tue Jan 06 22:18:27 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#f1ef95efa5ad
+http://mbed.org/users/mbed_official/code/mbed-rtos/#13a25134ac60
--- a/mbed.bld	Sun Dec 07 13:18:27 2014 +0000
+++ b/mbed.bld	Tue Jan 06 22:18:27 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file