init
This commit is contained in:
129
front_old/renderer.js
Normal file
129
front_old/renderer.js
Normal file
@@ -0,0 +1,129 @@
|
||||
const { serialList, serialOpen } = require("./serialapi");
|
||||
|
||||
let serial;
|
||||
|
||||
async function listSerialPorts() {
|
||||
document.getElementById("port-select").innerHTML = "";
|
||||
const ports = await serialList();
|
||||
if (ports.length === 0) {
|
||||
document.getElementById("port-select").innerHTML =
|
||||
"<option value='' disabled>No ports found</option>";
|
||||
}
|
||||
|
||||
document.getElementById("port-select").innerHTML = ports
|
||||
.map(
|
||||
(port) =>
|
||||
`<option value="${port.path}">${port.path} - ${port.manufacturer} ${port.friendlyName}</option>`,
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
|
||||
async function printToConsole(text, variant) {
|
||||
let classes = "p-console";
|
||||
if (variant === "error") classes = " p-error";
|
||||
if (variant === "log") classes = " p-log";
|
||||
if (variant === "incoming") classes = " p-incoming";
|
||||
if (variant === "outgoing") classes = " p-outgoing";
|
||||
document.getElementById("inner-console").innerHTML +=
|
||||
`<p class="${classes}">${text}</p>`;
|
||||
}
|
||||
|
||||
async function printIncoming(text) {
|
||||
if (
|
||||
document
|
||||
.getElementById("inner-console")
|
||||
.lastChild.classList.contains("p-incoming")
|
||||
) {
|
||||
document.getElementById("inner-console").lastChild.innerHTML += text;
|
||||
} else {
|
||||
printToConsole(text, "incoming");
|
||||
}
|
||||
}
|
||||
|
||||
async function clearConsole() {
|
||||
document.getElementById("inner-console").innerHTML = "";
|
||||
document.getElementById("console-input").value = "";
|
||||
}
|
||||
|
||||
async function openConnection() {
|
||||
if (serial) closeConnection();
|
||||
const selector = document.getElementById("port-select");
|
||||
selector.disabled = true;
|
||||
const port = selector.options[selector.selectedIndex].value;
|
||||
console.log(port);
|
||||
serial = await serialOpen(port);
|
||||
|
||||
serial.on("data", (line) => {
|
||||
// const linebrakedData = line.split("\n");
|
||||
// if (linebrakedData.length > 1) {
|
||||
|
||||
// } else {
|
||||
// addToLastLog(data.toString());
|
||||
// }
|
||||
// console.log(line);
|
||||
// printToConsole(line.toString(), "incoming");
|
||||
printIncoming(line.toString());
|
||||
});
|
||||
|
||||
serial.on("error", (err) => {
|
||||
printToConsole(err, "error");
|
||||
closeConnection();
|
||||
});
|
||||
|
||||
serial.on("close", () => {
|
||||
printToConsole("Connection closed", "log");
|
||||
closeConnection();
|
||||
});
|
||||
|
||||
serial.on("open", () => {
|
||||
printToConsole("Connection opened", "log");
|
||||
});
|
||||
|
||||
document.getElementById("open-button").style.display = "none";
|
||||
document.getElementById("close-button").style.display = "block";
|
||||
}
|
||||
|
||||
async function closeConnection() {
|
||||
if (serial) serial.close();
|
||||
serial = null;
|
||||
document.getElementById("open-button").style.display = "block";
|
||||
document.getElementById("close-button").style.display = "none";
|
||||
document.getElementById("port-select").disabled = false;
|
||||
}
|
||||
|
||||
async function sendToSerial() {
|
||||
if (!serial) return;
|
||||
document.getElementById("console-input").disabled = true;
|
||||
const data = document.getElementById("console-input").value;
|
||||
|
||||
if (document.getElementById("message-mode").checked) {
|
||||
document.getElementById("sender").disabled = true;
|
||||
const sender = document.getElementById("sender").value;
|
||||
const sendData = "ST" + sender + "D" + data;
|
||||
serial.write(sendData);
|
||||
printToConsole("> " + sendData, "outgoing");
|
||||
document.getElementById("sender").disabled = false;
|
||||
} else {
|
||||
serial.write(data);
|
||||
printToConsole("> " + data, "outgoing");
|
||||
}
|
||||
|
||||
document.getElementById("console-input").value = "";
|
||||
document.getElementById("console-input").disabled = false;
|
||||
}
|
||||
|
||||
async function toogleMsgMode() {
|
||||
if (document.getElementById("message-mode").checked) {
|
||||
document.getElementById("sender").disabled = false;
|
||||
} else {
|
||||
document.getElementById("sender").disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById("console-input").addEventListener("keyup", (event) => {
|
||||
if (event.key === "Enter") {
|
||||
sendToSerial();
|
||||
}
|
||||
});
|
||||
|
||||
listSerialPorts();
|
||||
Reference in New Issue
Block a user