mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-05-02 19:00:47 +08:00
⚡ add POST method to edit prop tree
This commit is contained in:
@@ -6,7 +6,8 @@
|
||||
use module.libsock;
|
||||
use std.json;
|
||||
|
||||
var new_getter = func(hostname, port) {
|
||||
var connection = {};
|
||||
connection.new = func(hostname, port) {
|
||||
var socket = libsock.socket;
|
||||
var sd = socket.socket(
|
||||
socket.AF_INET,
|
||||
@@ -24,35 +25,88 @@ var new_getter = func(hostname, port) {
|
||||
}
|
||||
println("[", os.time(), "] connect to ", hostname, ":", port, " succeeded");
|
||||
|
||||
var raw = func(s) {
|
||||
var v = split("", s);
|
||||
var res = "";
|
||||
foreach(var i; v) {
|
||||
if (i=="\r") {
|
||||
res ~= "\\r";
|
||||
continue;
|
||||
}
|
||||
if (i=="\n") {
|
||||
res ~= "\\n\n";
|
||||
continue;
|
||||
}
|
||||
res ~= i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
var getprop = func(path) {
|
||||
var header = "GET /json"~path~" HTTP/1.1\n\r\n";
|
||||
var res = socket.send(sd, header);
|
||||
var message = socket.recv(sd, 1024);
|
||||
var head_vector = split("\r\n", message.str);
|
||||
if (size(head_vector)<11) {
|
||||
println("getprop: node \"", path, "\" not found, invalid header");
|
||||
logprint(LOG_DEBUG, raw(message.str));
|
||||
return {path: path};
|
||||
}
|
||||
var message_total_size = num("0x"~head_vector[10]);
|
||||
|
||||
var total_source = message.str;
|
||||
while(message.size!=0) {
|
||||
var total_size = message.size;
|
||||
while(total_size<=message_total_size) {
|
||||
message = socket.recv(sd, 1024);
|
||||
total_source ~= message.str;
|
||||
total_size += message.size;
|
||||
}
|
||||
if (find("0\r\n\r\n", total_source)<0) {
|
||||
message = socket.recv(sd, 1024);
|
||||
total_source ~= message.str;
|
||||
total_size += message.size;
|
||||
}
|
||||
|
||||
var begin_position = find("{", total_source);
|
||||
if (begin_position<0) {
|
||||
println("getprop: node \"", path, "\" not found, invalid begin token");
|
||||
return {path: path};
|
||||
}
|
||||
var end_position = find("0\r\n\r\n", total_source);
|
||||
var props = substr(total_source, begin_position, end_position-begin_position);
|
||||
props = json.parse(props);
|
||||
json.check_error();
|
||||
var length = end_position-begin_position;
|
||||
if (length<0) {
|
||||
println("getprop: node \"", path, "\" not found, invalid end token");
|
||||
return {path: path};
|
||||
}
|
||||
var data = substr(total_source, begin_position, length);
|
||||
var props = json.parse(data);
|
||||
if (json.get_error()) {
|
||||
println("getprop: encounter error when parsing \"", path, "\"");
|
||||
logprint(LOG_DEBUG, data);
|
||||
return {path: path};
|
||||
}
|
||||
if (size(props)==0) {
|
||||
println("getprop: node \"", path, "\" not found");
|
||||
println("getprop: node \"", path, "\" not found, empty tree node");
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
var setprop = func(path, data) {
|
||||
var header = "POST /json"~path~" HTTP/1.1\n\n";
|
||||
header ~= "{\"value\":\""~data~"\"}\n\r\n";
|
||||
|
||||
var res = socket.send(sd, header);
|
||||
var message = socket.recv(sd, 1024);
|
||||
}
|
||||
|
||||
var close = func {
|
||||
socket.closesocket(sd);
|
||||
}
|
||||
|
||||
return {
|
||||
getprop: getprop,
|
||||
setprop: setprop,
|
||||
close: close
|
||||
};
|
||||
}
|
||||
@@ -61,14 +115,18 @@ var dump = func(tree, indent = "") {
|
||||
if (size(tree)==0) {
|
||||
return;
|
||||
}
|
||||
println(indent, "---------");
|
||||
println(indent, "path : \"", tree.path, "\"");
|
||||
println(indent, "name : \"", tree.name, "\"");
|
||||
println(indent, "index : \"", tree.index, "\"");
|
||||
println(indent, "type : \"", tree.type, "\"");
|
||||
println(indent, "nChildren : \"", tree.nChildren, "\"");
|
||||
println(indent, "---------");
|
||||
|
||||
|
||||
println(indent, "-------------------");
|
||||
var tree_keys = keys(tree);
|
||||
sort(tree_keys, func(a,b) {return cmp(a, b)<0;});
|
||||
foreach(var key; tree_keys) {
|
||||
if (key == "children") {
|
||||
continue;
|
||||
}
|
||||
println(indent, key, " : \"", tree[key], "\"");
|
||||
}
|
||||
println(indent, "-------------------");
|
||||
|
||||
if (contains(tree, "children")) {
|
||||
println(indent, "children :");
|
||||
foreach(var i; tree.children) {
|
||||
@@ -86,15 +144,29 @@ if (size(arg)>2) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
var getter = new_getter(arg[0], num(arg[1]));
|
||||
var connect = connection.new(arg[0], num(arg[1]));
|
||||
var count = 0;
|
||||
var recursive_get_prop = func(path = "/") {
|
||||
count += 1;
|
||||
if (math.mod(count, 100)==0) {
|
||||
println("get ", count," nodes, now: \"", path, "\"");
|
||||
}
|
||||
var props = connect.getprop(path);
|
||||
var tree = {};
|
||||
tree.path = props.path;
|
||||
if (!contains(props, "children")) {
|
||||
return tree;
|
||||
}
|
||||
|
||||
var get_props = func(path = "/") {
|
||||
var props = getter.getprop(path);
|
||||
dump(props);
|
||||
tree.children = [];
|
||||
foreach(var child; props.children) {
|
||||
var node = {};
|
||||
node = recursive_get_prop(child.path);
|
||||
append(tree.children, node);
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
get_props();
|
||||
get_props("/e-tron/dialog/config");
|
||||
get_props("/e-tron/dialog/config/a");
|
||||
|
||||
getter.close();
|
||||
# takes about 5 min to get whole tree
|
||||
var props = recursive_get_prop("/");
|
||||
dump(props);
|
||||
|
||||
Reference in New Issue
Block a user