]> git.mxchange.org Git - flightgear.git/commitdiff
Add a timestamp to JSON properties
authorTorsten Dreyer <torsten@t3r.de>
Wed, 26 Mar 2014 07:51:15 +0000 (08:51 +0100)
committerTorsten Dreyer <torsten@t3r.de>
Wed, 26 Mar 2014 07:51:15 +0000 (08:51 +0100)
add special attribute 'ts' to a JSON property reflecting
/sim/time/elapsed-sec

always add timestamp for listener properties
add timestamp for json requests if req param t=y is set

src/Network/http/JsonUriHandler.cxx
src/Network/http/PropertyChangeWebsocket.cxx
src/Network/http/jsonprops.cxx
src/Network/http/jsonprops.hxx

index 2d20923bc66abf0eb4df9c77433598c9e31c6981..6d7b08f9d2e8e4787e926d9c1b12de6d8ec03048 100644 (file)
@@ -54,6 +54,7 @@ bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse
 
   // pretty print (y) or compact print (default)
   bool indent = request.RequestVariables.get("i") == "y";
+  bool timestamp = request.RequestVariables.get("t") == "y";
 
   SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath );
   if( false == node.valid() ) {
@@ -64,7 +65,7 @@ bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse
 
   } 
 
-  response.Content = JSON::toJsonString( indent, node, depth );
+  response.Content = JSON::toJsonString( indent, node, depth, timestamp ? fgGetDouble("/sim/time/elapsed-sec") : -1.0 );
 
   return true;
 
index afa25995131b358c87c656390c93ee225d800817..c0576455f91ae592e261ba3da568f27b6692d832 100644 (file)
@@ -22,6 +22,7 @@
 #include "PropertyChangeObserver.hxx"
 #include "jsonprops.hxx"
 #include <simgear/debug/logstream.hxx>
+#include <Main/fg_props.hxx>
 
 #include <3rdparty/cjson/cJSON.h>
 
@@ -97,7 +98,7 @@ void PropertyChangeWebsocket::update(WebsocketWriter & writer)
     string newValue;
     if (_propertyChangeObserver->isChangedValue(node)) {
       SG_LOG(SG_NETWORK, SG_DEBUG, "httpd: new Value for " << node->getPath(true) << " '" << node->getStringValue() << "' #" << id);
-      writer.writeText( JSON::toJsonString( false, node, 0 ) );
+      writer.writeText( JSON::toJsonString( false, node, 0, fgGetDouble("/sim/time/elapsed-sec") ) );
     }
   }
 }
index 8457e84ae47cf451ad76a19c93cfcfa21378e505..3b159d2feb5c476f6f18188fed5c56ce6c059279 100644 (file)
@@ -69,7 +69,7 @@ static const char * getPropertyTypeString(simgear::props::Type type)
   }
 }
 
-cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth)
+cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth, double timestamp )
 {
   cJSON * json = cJSON_CreateObject();
   cJSON_AddItemToObject(json, "path", cJSON_CreateString(n->getPath(true).c_str()));
@@ -77,11 +77,14 @@ cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth)
   cJSON_AddItemToObject(json, "value", cJSON_CreateString(n->getStringValue()));
   cJSON_AddItemToObject(json, "type", cJSON_CreateString(getPropertyTypeString(n->getType())));
   cJSON_AddItemToObject(json, "index", cJSON_CreateNumber(n->getIndex()));
+  if( timestamp >= 0.0 )
+    cJSON_AddItemToObject(json, "ts", cJSON_CreateNumber(timestamp));
+
 
   if (depth > 0 && n->nChildren() > 0) {
     cJSON * jsonArray = cJSON_CreateArray();
     for (int i = 0; i < n->nChildren(); i++)
-      cJSON_AddItemToArray(jsonArray, toJson(n->getChild(i), depth - 1));
+      cJSON_AddItemToArray(jsonArray, toJson(n->getChild(i), depth - 1, timestamp ));
     cJSON_AddItemToObject(json, "children", jsonArray);
   }
   return json;
@@ -124,9 +127,9 @@ void JSON::toProp(cJSON * json, SGPropertyNode_ptr base)
   }
 }
 
-string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth)
+string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp )
 {
-  cJSON * json = toJson( n, depth );
+  cJSON * json = toJson( n, depth, timestamp );
   char * jsonString = indent ? cJSON_Print( json ) : cJSON_PrintUnformatted( json );
   string reply(jsonString);
   free( jsonString );
index 41dbcaefae3612807e88a349d7926a9f095d85ca..5c2ba1ad9ef7362aed41dd8fd3dcb7083104167c 100644 (file)
@@ -29,8 +29,8 @@ namespace flightgear {
 namespace http {
 class JSON {
 public:
-  static cJSON * toJson(SGPropertyNode_ptr n, int depth);
-  static std::string toJsonString(bool indent, SGPropertyNode_ptr n, int depth);
+  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);
 };