⚡ add POST method to edit prop tree
This commit is contained in:
parent
7b05a0a244
commit
d461cbb05c
21
std/json.nas
21
std/json.nas
|
@ -11,8 +11,9 @@ var (
|
|||
_j_colon,
|
||||
_j_str,
|
||||
_j_num,
|
||||
_j_id
|
||||
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
_j_id,
|
||||
_j_bool
|
||||
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
|
||||
var _j_content = [
|
||||
"eof",
|
||||
|
@ -24,7 +25,8 @@ var _j_content = [
|
|||
"`:`",
|
||||
"string",
|
||||
"number",
|
||||
"identifier"
|
||||
"identifier",
|
||||
"boolean"
|
||||
];
|
||||
|
||||
var _parse_error = 0;
|
||||
|
@ -133,7 +135,13 @@ var parse = func() {
|
|||
} elsif (isnum(c)) {
|
||||
var s = c;
|
||||
ptr += 1;
|
||||
while(ptr<text_size and ((isnum(char(text[ptr])) or char(text[ptr])=='.'))) {
|
||||
while(ptr<text_size and ((
|
||||
isnum(char(text[ptr])) or
|
||||
char(text[ptr])=='.' or
|
||||
char(text[ptr])=='e' or
|
||||
char(text[ptr])=='-' or
|
||||
char(text[ptr])=='+'))
|
||||
) {
|
||||
s ~= char(text[ptr]);
|
||||
ptr += 1;
|
||||
}
|
||||
|
@ -150,6 +158,9 @@ var parse = func() {
|
|||
ptr -= 1;
|
||||
token.content = s;
|
||||
token.type = _j_id;
|
||||
if (s=="true" or s=="false") {
|
||||
token.type = _j_bool;
|
||||
}
|
||||
}
|
||||
ptr += 1;
|
||||
return;
|
||||
|
@ -179,7 +190,7 @@ var parse = func() {
|
|||
hash[name] = hash_gen();
|
||||
} elsif (token.type==_j_lbrkt) {
|
||||
hash[name] = vec_gen();
|
||||
} elsif (token.type==_j_str or token.type==_j_num) {
|
||||
} elsif (token.type==_j_str or token.type==_j_num or token.type==_j_bool) {
|
||||
hash[name] = token.content;
|
||||
next();
|
||||
}
|
||||
|
|
|
@ -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,13 +115,17 @@ 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 :");
|
||||
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue