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/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