Make API communicate with modules

This commit is contained in:
Bridget
2020-04-13 01:43:13 +02:00
parent 840882898d
commit 08b858e12f
4 changed files with 52 additions and 3 deletions

26
api.lsl
View File

@@ -1,11 +1,28 @@
integer connected = 0;
key SECRET_KEY = "29731e5170353a8b235098c43cd2099a4e805c55fb4395890e81f437c17334a9";
list commands = [];
key request_id;
default
{
state_entry()
{
llRequestSecureURL();
llMessageLinked(LINK_SET, 0, "", "request_command_info");
}
link_message(integer link, integer num, string msg, key id)
{
if(id == "command_info")
{
commands += llParseString2List(msg, ["|"], []);
}
else if(id == request_id && num == 1)
{
// TODO Properly escape and serialize to JSON
llHTTPResponse(id, 200, "{\"result\": \"" + msg + "\"}");
request_id = "";
}
}
http_request(key id, string method, string body)
@@ -30,9 +47,16 @@ default
llHTTPResponse(id, 200, "{\"result\": \"disconnected\"}");
connected = 0;
}
else if(llJsonGetValue(body, ["command"]) == "get_commands")
{
llHTTPResponse(id, 200, "{\"available_commands\": " + \
llList2Json(JSON_OBJECT, commands) + "}");
}
else
{
llHTTPResponse(id, 200, "{\"result\": \"success\"}");
// Relay the message to other scripts, we handle the response later
request_id = id;
llMessageLinked(LINK_SET, 0, llJsonGetValue(body, ["command"]), id);
}
}
else

View File

@@ -67,6 +67,10 @@ class Shell(cmd.Cmd):
print("Error: Invalid response")
return
available_commands = self.send_cmd(url, "get_commands")
for key, value in available_commands.get("available_commands").items():
self.add_cmd(key, value)
print(f"Connected to {uuid}")
print(
"_______________________________________________________________________________"
@@ -99,8 +103,8 @@ class Shell(cmd.Cmd):
"""Make a new command available within the shell."""
def do_cmd(arg):
result = self.send_cmd(self.url, arg)
print(result)
result = self.send_cmd(self.url, f"{do_cmd.__name__} {arg}")
print(result.get("result"))
do_cmd.__doc__ = help_text
do_cmd.__name__ = name

19
modules/echo.lsl Normal file
View File

@@ -0,0 +1,19 @@
default
{
link_message(integer sender, integer num, string msg, key id)
{
list params = llParseString2List(msg, [" "], [""]);
string param0 = llList2String(params, 0);
string param1 = llList2String(params, 1);
if(id == "request_command_info")
{
llMessageLinked(LINK_SET, 0, "echo|echo: echo [arg ...]", "command_info");
}
else if(param0 == "echo")
{
string response = llDumpList2String(llDeleteSubList(params, 0, 0), " ");
llMessageLinked(LINK_SET, 1, response, id);
}
}
}

View File

@@ -6,3 +6,5 @@
- [ ] Add non-interactive mode to allow use in other scripts
- [ ] Add verbose mode to see all communication
- [x] Add command history
- [x] Make lsl API script discover modules
- [x] Make lsl API script communicate with modules