Make API communicate with modules
This commit is contained in:
26
api.lsl
26
api.lsl
@@ -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
|
||||
|
||||
8
lslsh.py
8
lslsh.py
@@ -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
19
modules/echo.lsl
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user