Add Karen v2 demo

This commit is contained in:
AaronDewes 2022-05-16 07:19:50 +00:00
parent 56946a9812
commit 611d4a166b
3 changed files with 82 additions and 0 deletions

1
.gitignore vendored
View File

@ -52,3 +52,4 @@ db/citadel-seed/*
services/installed.json services/installed.json
services/installed.yml services/installed.yml
events/karen

51
karen-client.js Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env node
const net = require("net");
let connection;
function initConnection() {
return new Promise((resolve) => {
connection = net.createConnection("events/karen", () => resolve());
});
}
async function runCommand(command) {
await initConnection();
return new Promise((resolve, reject) => {
const errorListener = (error) => {
reject(error);
};
connection.on("error", errorListener);
connection.write(command, () => {
connection.off("error", errorListener);
resolve();
});
});
}
const readline = require("readline/promises");
const { stdin, stdout } = require("process");
async function main() {
const rl = readline.createInterface({ input: stdin, output: stdout });
while (true) {
const userInput = await rl.question("> ");
if (userInput === "exit" || userInput === "quit") {
rl.close();
break;
} else if (userInput === "help") {
console.log(`Available commands:
trigger: Execute a certain trigger
help: Print this message
quit: Exit this client
exit: Same as quit`);
}
await runCommand(userInput);
console.log("Command executed");
}
}
main();

30
karen-v2 Executable file
View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
import socket
import os
import time
from collections import deque
rootDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(rootDir)
if os.path.exists("events/karen"):
os.remove("events/karen")
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind("events/karen")
while True:
server.listen(1)
conn, addr = server.accept()
datagram = conn.recv(1024)
if datagram:
instructions = datagram.decode("utf-8").strip().split()
cmd = instructions[0]
if(cmd == "trigger"):
trigger = instructions[1]
instructions.pop(0)
instructions.pop(0)
print("Executing trigger {} with arguments {}".format(trigger, " ".join(instructions)))
os.system("events/triggers/{} {}".format(trigger, " ".join(instructions)))
conn.close()