tes

Dependencies:   ASyncTicker EthernetInterface WebSocketClient mbed-rtos mbed MbedJSONValue xbee_lib

Revision:
7:7039646b7083
Parent:
6:c1bd3fadce09
--- a/main.cpp	Mon Feb 03 12:07:53 2014 +0000
+++ b/main.cpp	Wed Jun 18 17:44:49 2014 +0000
@@ -9,7 +9,9 @@
 #include <string>
 #include "btNode.h"
 #include <map>
+#include <utility>
 #define PORT 80
+const std::string HUBID = "1";
 
 // status leds
 // led 1
@@ -25,7 +27,43 @@
 EthernetInterface ethernet;
 Websocket         ws("ws://192.168.0.4:8080/");
 
+bool sync_devices()
+{
+    MbedJSONValue sync_data;
 
+    std::string sync_string;
+    
+        // fill the object
+    sync_data["TYPE"]   = "SYNC";
+    sync_data["ORIGIN"] = "HUB";
+    sync_data["ID"]     = "N/A";
+
+    //sync_data["DATA"]   = "N/A";
+    //iterate over local nodes and send node list to svr over socket
+    
+    // map<char, int>::iterator p;
+    
+    int index = 0;
+  
+for (std::map<int, btNode>::iterator it = BTnodes.begin(); it != BTnodes.end(); ++it)
+{
+  MbedJSONValue device = it->second.GetAsJSON();
+  printf("dev : %s\n\r", device.serialize());
+  sync_data["DEVICES"][index] = device.serialize();
+  index++;
+} 
+    sync_data["COUNT"] = index;
+    sync_data["STATUS"] = "0";
+    
+    sync_string = sync_data.serialize();
+    
+    ws.send((char *)sync_string.c_str());
+
+    // serialize it into a JSON string
+
+    
+return true;
+}
 
 void pull_updates()
 {
@@ -96,8 +134,10 @@
 
 int main()
 {
-    
-    btNode b(30);
+    //add a registered device to the device array
+    btNode a(1, "msp430-homer", HUBID);
+    btNode b(30, "mp430-adam", HUBID);
+    BTnodes.insert(pair<int, btNode>(1,a));
     BTnodes.insert(pair<int, btNode>(30,b));
     
     led_ethernet = 0;
@@ -128,6 +168,8 @@
     Timer timer;
 
     timer.start();
+    
+    sync_devices();
 
     // every <interval> seconds call to the server to pull updates
     while (true)