]> git.mxchange.org Git - flightgear.git/commitdiff
allow binding of arguments for fg-commands thru run.cgi
authorTorsten Dreyer <torsten@t3r.de>
Mon, 10 Mar 2014 21:58:52 +0000 (22:58 +0100)
committerTorsten Dreyer <torsten@t3r.de>
Mon, 10 Mar 2014 21:58:52 +0000 (22:58 +0100)
to run a fg-command with args, POST run.cgi?value=my-command
and post the args node as JSON content like this:
{
   name: '',
   children: [
     {
       name: 'property',
       index: 0,
       value: 'first-value is here'
     },
     {
       name: 'property',
       index: 1,
       value: 'first-value is here'
     }
   ]
}
or whatever arguments the requested command takes

src/Network/http/RunUriHandler.cxx

index 798a81874c96a7c377f72db6b195070ce8624709..c0e5bac48bae897aae8f684916cd85cb00f49b94 100644 (file)
 #include <simgear/props/props.hxx>
 #include <simgear/structure/commands.hxx>
 #include <Main/globals.hxx>
+#include <3rdparty/cjson/cJSON.h>
+
 
 using std::string;
 
 namespace flightgear {
 namespace http {
 
+static void JsonToProp( cJSON * json, SGPropertyNode_ptr base )
+{
+  if( NULL == json ) return;
+
+  cJSON * cj = cJSON_GetObjectItem( json, "name" );
+  if( NULL == cj ) return; // a node with no name?
+  char * name = cj->valuestring;
+  if( NULL == name ) return; // still no name?
+
+  //TODO: check valid name
+
+  int index = 0;
+  cj = cJSON_GetObjectItem( json, "index" );
+  if( NULL != cj ) index = cj->valueint;
+  if( index < 0 ) return;
+
+  SGPropertyNode_ptr n = base->getNode( name, index, true );
+  cJSON * children = cJSON_GetObjectItem( json, "children" );
+  if( NULL != children ) {
+    for( int i = 0; i < cJSON_GetArraySize( children ); i++ ) {
+      JsonToProp( cJSON_GetArrayItem( children, i ), n );
+    }
+  } else {
+    //TODO: set correct type
+/*
+    char * type = "";
+    cj = cJSON_GetObjectItem( json, "type" );
+    if( NULL != cj ) type = cj->valuestring;
+*/
+    char * value = NULL;
+    cj = cJSON_GetObjectItem( json, "value" );
+    if( NULL != cj ) value = cj->valuestring;
+
+    if( NULL != value )
+      n->setUnspecifiedValue( value );
+  }
+}
+
 bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & response )
 {
   response.Header["Content-Type"] = "text/plain";
@@ -39,8 +79,12 @@ bool RunUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse & r
     return true;
   }
 
-  SGPropertyNode args;
-  if ( globals->get_commands()->execute(command.c_str(), &args) ) {
+  SGPropertyNode_ptr args = new SGPropertyNode();
+  cJSON * json = cJSON_Parse( request.Content.c_str() );
+  JsonToProp( json, args );
+
+  cJSON_Delete( json );
+  if ( globals->get_commands()->execute(command.c_str(), args) ) {
     response.Content = "ok.";
     return true;
   }