#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>
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)
{
_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;
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) {
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);
}
}