57 lines
2.6 KiB
QML
57 lines
2.6 KiB
QML
pragma ComponentBehavior: Bound
|
|
import QtQuick
|
|
import Quickshell
|
|
import Quickshell.Io
|
|
|
|
QtObject {
|
|
id: root
|
|
property var backendCommand: ["python3", Quickshell.shellPath("backend/bluetooth.py")]
|
|
property bool busy: false
|
|
property bool timedOut: false
|
|
property int timeoutMs: 50000
|
|
property string operation: ""
|
|
property string error: ""
|
|
property string readiness: "unknown"
|
|
property int adapterCount: 0
|
|
signal succeeded(string action)
|
|
function start(action, path) {
|
|
if (busy || worker.running) return false;
|
|
if (!["status", "power-on", "power-off", "connect", "disconnect", "forget"].includes(action)) { error = "Неизвестное действие Bluetooth"; return false; }
|
|
if (action !== "status" && typeof path !== "string") { error = "Некорректный путь Bluetooth-устройства"; return false; }
|
|
operation = action; error = ""; timedOut = false; busy = true;
|
|
worker.command = backendCommand.concat(action === "status" ? [action] : [action, path]);
|
|
worker.running = true;
|
|
return true;
|
|
}
|
|
function refresh() { return start("status", ""); }
|
|
function finish(code) {
|
|
busy = false;
|
|
if (timedOut) return;
|
|
try {
|
|
if (!reply.text.trim()) throw new Error(errors.text.trim().slice(0, 2000) || "Не удалось запустить Bluetooth backend");
|
|
const result = JSON.parse(reply.text);
|
|
if (code !== 0 || result.ok !== true) throw new Error(result.error || "Ошибка Bluetooth");
|
|
if (operation === "status") {
|
|
if (!Number.isInteger(result.adapterCount) || result.adapterCount < 0) throw new Error("Некорректный ответ Bluetooth backend");
|
|
adapterCount = result.adapterCount; readiness = "ready";
|
|
} else succeeded(operation);
|
|
} catch(e) { error = String(e); if (operation === "status") readiness = "unavailable"; }
|
|
}
|
|
property Process worker: Process {
|
|
stdout: StdioCollector { id: reply }
|
|
stderr: StdioCollector { id: errors }
|
|
onExited: (code, status) => root.finish(code)
|
|
}
|
|
property Timer watchdog: Timer {
|
|
running: root.busy
|
|
interval: root.timeoutMs
|
|
onTriggered: {
|
|
root.timedOut = true;
|
|
root.error = "Время ожидания истекло. Результат действия Bluetooth не подтверждён.";
|
|
if (root.operation === "status") root.readiness = "unavailable";
|
|
root.worker.signal(9);
|
|
if (!root.worker.running) root.busy = false;
|
|
}
|
|
}
|
|
}
|