1
0
Fork 0

Websocket get/set props and exec commands.

This commit is contained in:
James Turner 2014-05-17 09:07:25 +01:00
parent ee627b8652
commit d1a477324e
4 changed files with 137 additions and 6 deletions

View file

@ -21,7 +21,13 @@
#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);
}
}

View 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);

View 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 );

View 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