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
This commit is contained in:
parent
f68ae55068
commit
0dcc8aa34d
4 changed files with 13 additions and 8 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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") ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue