]> git.mxchange.org Git - flightgear.git/commitdiff
Websocket get/set props and exec commands.
authorJames Turner <zakalawe@mac.com>
Sat, 17 May 2014 08:07:25 +0000 (09:07 +0100)
committerJames Turner <zakalawe@mac.com>
Thu, 12 Jun 2014 16:57:25 +0000 (17:57 +0100)
src/Network/http/PropertyChangeWebsocket.cxx
src/Network/http/PropertyChangeWebsocket.hxx
src/Network/http/jsonprops.cxx
src/Network/http/jsonprops.hxx

index ecfdefbb4b1ec0e3bd898016165be314c039e2c4..bf4f0856cb826cd951bcf48a6e37232e865facfe 100644 (file)
 #include "PropertyChangeWebsocket.hxx"
 #include "PropertyChangeObserver.hxx"
 #include "jsonprops.hxx"
+
 #include <simgear/debug/logstream.hxx>
+#include <simgear/props/props.hxx>
+#include <simgear/structure/commands.hxx>
+
+#include <simgear/props/props_io.hxx>
+#include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 
 #include <3rdparty/cjson/cJSON.h>
@@ -33,6 +39,84 @@ using std::string;
 
 static unsigned nextid = 0;
 
+static void setPropertyFromJson(SGPropertyNode_ptr prop, cJSON * json)
+{
+  if (!prop) return;
+  if ( NULL == json ) return;
+  switch ( json->type ) {
+  case cJSON_String:
+    prop->setStringValue(json->valuestring);
+    break;
+    
+  case cJSON_Number:
+    prop->setDoubleValue(json->valuedouble);
+    break;
+    
+  case cJSON_True:
+    prop->setBoolValue(true);
+    break;
+      
+  case cJSON_False:
+    prop->setBoolValue(false);
+    break;
+      
+  default:
+    return;
+  }
+}
+  
+static void handleSetCommand(const string_list& nodes, cJSON* json, WebsocketWriter &writer)
+{
+  cJSON * value = cJSON_GetObjectItem(json, "value");
+  if ( NULL != value ) {
+    if (nodes.size() > 1) {
+      SG_LOG(SG_NETWORK, SG_WARN, "httpd: WS set: insufficent values for nodes:" << nodes.size());
+      return;
+    }
+    
+    SGPropertyNode_ptr n = fgGetNode(nodes.front());
+    if (!n) {
+      SG_LOG(SG_NETWORK, SG_WARN, "httpd: set '" << nodes.front() << "'  not found");
+      return;
+    }
+    
+    setPropertyFromJson(n, value);
+    return;
+  }
+  
+  cJSON * values = cJSON_GetObjectItem(json, "values");
+  if ( ( NULL == values ) || ( static_cast<size_t>(cJSON_GetArraySize(values)) != nodes.size()) ) {
+    SG_LOG(SG_NETWORK, SG_WARN, "httpd: WS set: mismatched nodes/values sizes:" << nodes.size());
+    return;
+  }
+  
+  string_list::const_iterator it;
+  int i=0;
+  for (it = nodes.begin(); it != nodes.end(); ++it, ++i) {
+    SGPropertyNode_ptr n = fgGetNode(*it);
+    if (!n) {
+      SG_LOG(SG_NETWORK, SG_WARN, "httpd: get '" << *it << "'  not found");
+      return;
+    }
+
+    setPropertyFromJson(n, cJSON_GetArrayItem(values, i));
+  } // of nodes iteration
+}
+  
+static void handleExecCommand(cJSON* json)
+{
+  cJSON* name = cJSON_GetObjectItem(json, "fgcommand");
+  if ((NULL == name )|| (NULL == name->valuestring)) {
+    SG_LOG(SG_NETWORK, SG_WARN, "httpd: exec: no fgcommand name");
+    return;
+  }
+  
+  SGPropertyNode_ptr arg(new SGPropertyNode);
+  JSON::addChildrenToProp( json, arg );
+  
+  globals->get_commands()->execute(name->valuestring, arg);
+}
+  
 PropertyChangeWebsocket::PropertyChangeWebsocket(PropertyChangeObserver * propertyChangeObserver)
     : id(++nextid), _propertyChangeObserver(propertyChangeObserver)
 {
@@ -48,7 +132,22 @@ void PropertyChangeWebsocket::close()
   _watchedNodes.clear();
 }
 
-void PropertyChangeWebsocket::handleRequest(const HTTPRequest & request, WebsocketWriter &)
+void PropertyChangeWebsocket::handleGetCommand(const string_list& nodes, WebsocketWriter &writer)
+{
+  double t = fgGetDouble("/sim/time/elapsed-sec");
+  string_list::const_iterator it;
+  for (it = nodes.begin(); it != nodes.end(); ++it) {
+    SGPropertyNode_ptr n = fgGetNode(*it);
+    if (!n) {
+      SG_LOG(SG_NETWORK, SG_WARN, "httpd: get '" << *it << "'  not found");
+      return;
+    }
+    
+    writer.writeText( JSON::toJsonString( false, n, 0, t ) );
+  } // of nodes iteration
+}
+  
+void PropertyChangeWebsocket::handleRequest(const HTTPRequest & request, WebsocketWriter &writer)
 {
   if (request.Content.empty()) return;
 
@@ -71,10 +170,12 @@ void PropertyChangeWebsocket::handleRequest(const HTTPRequest & request, Websock
       command = j->valuestring;
     }
 
-    string nodeName;
+    // handle a single node name, or an array of them
+    string_list nodeNames;
     j = cJSON_GetObjectItem(json, "node");
-    if ( NULL != j && NULL != j->valuestring) nodeName = j->valuestring;
-    _watchedNodes.handleCommand(command, nodeName, _propertyChangeObserver);
+    if ( NULL != j && NULL != j->valuestring) {
+      nodeNames.push_back(j->valuestring);
+    }
 
     cJSON * nodes = cJSON_GetObjectItem(json, "nodes");
     if ( NULL != nodes) {
@@ -82,10 +183,23 @@ void PropertyChangeWebsocket::handleRequest(const HTTPRequest & request, Websock
         cJSON * node = cJSON_GetArrayItem(nodes, i);
         if ( NULL == node) continue;
         if ( NULL == node->valuestring) continue;
-        nodeName = node->valuestring;
-        _watchedNodes.handleCommand(command, nodeName, _propertyChangeObserver);
+        nodeNames.push_back(node->valuestring);
+      }
+    }
+    
+    if (command == "get") {
+      handleGetCommand(nodeNames, writer);
+    } else if (command == "set") {
+      handleSetCommand(nodeNames, json, writer);
+    } else if (command == "exec") {
+      handleExecCommand(json);
+    } else {
+      string_list::const_iterator it;
+      for (it = nodeNames.begin(); it != nodeNames.end(); ++it) {
+        _watchedNodes.handleCommand(command, *it, _propertyChangeObserver);
       }
     }
+    
     cJSON_Delete(json);
   }
 }
index f195c4917ed45ed68622c5a50e757a1b96b9b517..3f5af11f52ab3ad6e4b97010fd1292ec4039fc7b 100644 (file)
@@ -43,6 +43,8 @@ private:
   unsigned id;
   PropertyChangeObserver * _propertyChangeObserver;
 
+  void handleGetCommand(const string_list& nodes, WebsocketWriter &writer);
+  
   class WatchedNodesList: public std::vector<SGPropertyNode_ptr> {
   public:
     void handleCommand(const std::string & command, const std::string & node, PropertyChangeObserver * propertyChangeObserver);
index 5e7a4b54d12c59d4b83eb8966654bb9a55b9c304..3bb3633fb070e54c3cbaff7bedc335395ed3dfa5 100644 (file)
@@ -128,6 +128,19 @@ void JSON::toProp(cJSON * json, SGPropertyNode_ptr base)
   }
 }
 
+void JSON::addChildrenToProp(cJSON * json, SGPropertyNode_ptr n)
+{
+  if (NULL == json) return;
+  if (!n) return;
+  
+  cJSON * children = cJSON_GetObjectItem(json, "children");
+  if (NULL != children) {
+    for (int i = 0; i < cJSON_GetArraySize(children); i++) {
+      toProp(cJSON_GetArrayItem(children, i), n);
+    }
+  }
+}
+
 string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp )
 {
   cJSON * json = toJson( n, depth, timestamp );
index 5c2ba1ad9ef7362aed41dd8fd3dcb7083104167c..4f2fcf67febd1547c2540147521ec709b3c4bee3 100644 (file)
@@ -31,7 +31,9 @@ class JSON {
 public:
   static cJSON * toJson(SGPropertyNode_ptr n, int depth, double timestamp = -1.0 );
   static std::string toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp = -1.0 );
+  
   static void toProp(cJSON * json, SGPropertyNode_ptr base);
+  static void addChildrenToProp(cJSON * json, SGPropertyNode_ptr base);
 };
 
 }  // namespace http