diff --git a/android/code/index.mjs b/android/code/index.mjs index 7ae0ad8..11531b5 100644 --- a/android/code/index.mjs +++ b/android/code/index.mjs @@ -1,12 +1,20 @@ import { WebSocketServer } from "ws"; import child_process from "child_process"; import fs from "fs"; +import { send_notification } from "./notifications.mjs" async function spawnPromise(program, args) { return new Promise((resolve, reject) => { + let output = ""; const process = child_process.spawn(program, args); - process.on("close", (_) => { - resolve(); + process.stdout.on('data', (data) => { + output += data; + }); + process.stderr.on('data', (data) => { + output += data; + }); + process.on("close", (code) => { + resolve({output, code}); }); }); } @@ -32,7 +40,8 @@ wss.on("connection", (ws) => { } else if (data === "home") { await spawnPromise("bash", ["/conf/home.sh"]); } else if (data === "install") { - await spawnPromise("bash", ["/conf/install.sh"]); + const res = await spawnPromise("bash", ["/conf/install.sh"]); + send_notification(res.code === 0, "Installing the application", res.output); } else if (data.includes("drag")) { const dataSplit = data.split(" "); diff --git a/android/code/notifications.mjs b/android/code/notifications.mjs new file mode 100644 index 0000000..200d5b2 --- /dev/null +++ b/android/code/notifications.mjs @@ -0,0 +1,36 @@ +import { WebSocket } from "ws"; +import { WebSocketServer } from "ws"; + +const notification_proxy = new WebSocketServer({ port: 3001 }); +let notification_subs = []; + +notification_proxy.on('connection', (ws) => { + notification_subs.push(ws); +}); + +export function send_notification(is_ok, context, message) +{ + let updated_subs = []; + + if (notification_subs.length === 0) { + console.log("WARNING: Got a notification, but nobody is subscribed"); + } + for (const sub of notification_subs) { + if (sub.readyState == WebSocket.CONNECTING) { + console.log("WARNING: Unable to forward a notification to client that is still connecting"); + updated_subs.push(sub); + } else { + try { + sub.send(JSON.stringify({ + is_ok, + context, + message + })); + updated_subs.push(sub); + } catch { + sub.close(); + console.log("WARNING: Fail to send a notification, closing the connection"); + } + } + } +} diff --git a/docker-compose.yaml b/docker-compose.yaml index 661ca4b..1505ce7 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -22,6 +22,7 @@ services: - 45457:45459 # This cannot change - 10001:10001 # api port - 3000:3000 # android server port + - 3001:3001 # Notifications server volumes: - $PWD/shared_buffer:/shared_buffer - $PWD/android/conf:/conf diff --git a/http_server/code/index.html b/http_server/code/index.html index 0780381..83ab631 100644 --- a/http_server/code/index.html +++ b/http_server/code/index.html @@ -128,10 +128,16 @@ #upload_form button:hover, #upload_form label:hover { background-color: #ddd; } + #notifications { + width: 40%; + margin-left: 60%; + position: absolute; + }
- + +