This commit is contained in:
Andrii Dokhniak 2025-04-11 13:30:19 +02:00
parent 782d390f30
commit 650757fe19
5 changed files with 39 additions and 33 deletions

View File

@ -1,20 +1,20 @@
import { WebSocketServer } from "ws";
import child_process from "child_process";
import fs from "fs";
import { send_notification } from "./notifications.mjs"
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.stdout.on('data', (data) => {
process.stdout.on("data", (data) => {
output += data;
});
process.stderr.on('data', (data) => {
process.stderr.on("data", (data) => {
output += data;
});
process.on("close", (code) => {
resolve({output, code});
resolve({ output, code });
});
});
}
@ -41,7 +41,11 @@ wss.on("connection", (ws) => {
await spawnPromise("bash", ["/conf/home.sh"]);
} else if (data === "install") {
const res = await spawnPromise("bash", ["/conf/install.sh"]);
send_notification(res.code === 0, "Installing the application", res.output);
send_notification(
res.code === 0,
"Installing the application",
res.output
);
} else if (data.includes("drag")) {
const dataSplit = data.split(" ");

View File

@ -4,12 +4,11 @@ import { WebSocketServer } from "ws";
const notification_proxy = new WebSocketServer({ port: 3001 });
let notification_subs = [];
notification_proxy.on('connection', (ws) => {
notification_proxy.on("connection", (ws) => {
notification_subs.push(ws);
});
export function send_notification(is_ok, context, message)
{
export function send_notification(is_ok, context, message) {
let updated_subs = [];
if (notification_subs.length === 0) {
@ -17,19 +16,25 @@ export function send_notification(is_ok, context, message)
}
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");
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
}));
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");
console.log(
"WARNING: Fail to send a notification, closing the connection"
);
}
}
}

View File

@ -6,9 +6,9 @@ import {
waitFullBoot,
} from "./screenshot.mjs";
import { execSync } from 'node:child_process';
import { execSync } from "node:child_process";
import fileUpload from 'express-fileupload';
import fileUpload from "express-fileupload";
const device_size_x = 320;
const device_size_y = 640;
@ -16,7 +16,7 @@ const device_size_y = 640;
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.static('/code/dist'))
app.use(express.static("/code/dist"));
console.log("Waiting for full boot...");
await waitFullBoot();
@ -62,23 +62,23 @@ app.post("/back", function (req, res) {
// default options
app.use(fileUpload());
app.post('/upload_apk', async function (req, res) {
app.post("/upload_apk", async function (req, res) {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
return res.status(400).send("No files were uploaded.");
}
execSync("rm -rf /shared_buffer/*");
if (Array.isArray(req.files.app)) {
for (const [idx, file] of req.files.app.entries()) {
let uploadPath = '/shared_buffer/app' + idx + '.apk';
let uploadPath = "/shared_buffer/app" + idx + ".apk";
await file.mv(uploadPath);
}
} else {
let uploadPath = '/shared_buffer/app' + 0 + '.apk';
let uploadPath = "/shared_buffer/app" + 0 + ".apk";
await req.files.app.mv(uploadPath);
}
android_websocket.send(`install`);
res.send('Files uploaded!');
})
res.send("Files uploaded!");
});
app.post("/home", function (req, res) {
android_websocket.send(`home`);
@ -110,4 +110,3 @@ app.post("/drag", function (req, res) {
});
app.listen(8080, () => console.log("Listening in port 8080"));

View File

@ -13,12 +13,11 @@ async function screenshot() {
while (android_websocket.readyState != WebSocket.OPEN) {
await sleep(15);
retries++;
if (retries > 50)
{
if (retries > 50) {
console.error("Screenshot ws timeout");
doneWrite = 0;
screenshotPromise = null;
return ;
return;
}
}
android_websocket.send("screenshot");

View File

@ -1,5 +1,4 @@
import { render, Component } from "preact";
import { useState } from "preact/hooks";
function rand_num() {
return Math.floor(Math.random() * Number.MAX_VALUE);
@ -11,12 +10,12 @@ class Notifications extends Component {
this.state = { notifications: [] };
}
remove_notification (id) {
remove_notification(id) {
const newNotifications = this.state.notifications.filter(
(notification) => notification.id !== id
);
this.setState({ notifications: newNotifications });
};
}
componentDidMount() {
// This should also be dynamic
@ -32,8 +31,8 @@ class Notifications extends Component {
});
// a 10 sec timeout
setTimeout(() => {
this.remove_notification(new_id)
}, 10000)
this.remove_notification(new_id);
}, 10000);
};
}