From: Torsten Dreyer Date: Wed, 26 Mar 2014 07:51:15 +0000 (+0100) Subject: Add a timestamp to JSON properties X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=0dcc8aa34ddd0384bde1d86669c6ce3f2a159e6f;p=flightgear.git Add a timestamp to JSON properties 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 --- diff --git a/src/Network/http/JsonUriHandler.cxx b/src/Network/http/JsonUriHandler.cxx index 2d20923bc..6d7b08f9d 100644 --- a/src/Network/http/JsonUriHandler.cxx +++ b/src/Network/http/JsonUriHandler.cxx @@ -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; diff --git a/src/Network/http/PropertyChangeWebsocket.cxx b/src/Network/http/PropertyChangeWebsocket.cxx index afa259951..c0576455f 100644 --- a/src/Network/http/PropertyChangeWebsocket.cxx +++ b/src/Network/http/PropertyChangeWebsocket.cxx @@ -22,6 +22,7 @@ #include "PropertyChangeObserver.hxx" #include "jsonprops.hxx" #include +#include
#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") ) ); } } } diff --git a/src/Network/http/jsonprops.cxx b/src/Network/http/jsonprops.cxx index 8457e84ae..3b159d2fe 100644 --- a/src/Network/http/jsonprops.cxx +++ b/src/Network/http/jsonprops.cxx @@ -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 ); diff --git a/src/Network/http/jsonprops.hxx b/src/Network/http/jsonprops.hxx index 41dbcaefa..5c2ba1ad9 100644 --- a/src/Network/http/jsonprops.hxx +++ b/src/Network/http/jsonprops.hxx @@ -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); };