fix: reduce file system read cycle #19
This commit is contained in:
+32
-11
@@ -1,7 +1,8 @@
|
|||||||
const os = require("os");
|
const os = require("os");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const process = require("child_process");
|
const fsp = require("fs/promises");
|
||||||
|
const cpr = require("child_process");
|
||||||
|
|
||||||
global.HARDWARE = global.HARDWARE || {
|
global.HARDWARE = global.HARDWARE || {
|
||||||
initialized: false,
|
initialized: false,
|
||||||
@@ -90,7 +91,7 @@ const init = async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Check for display changes
|
// Check for display changes
|
||||||
setInterval(update, 500);
|
interval(update, 1000);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -98,14 +99,14 @@ const init = async () => {
|
|||||||
/**
|
/**
|
||||||
* Updates the shared hardware properties.
|
* Updates the shared hardware properties.
|
||||||
*/
|
*/
|
||||||
const update = () => {
|
const update = async () => {
|
||||||
if (!HARDWARE.initialized) {
|
if (!HARDWARE.initialized) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display status has changed
|
// Display status has changed
|
||||||
if (HARDWARE.support.displayStatus) {
|
if (HARDWARE.support.displayStatus) {
|
||||||
const status = fs.readFileSync(`${HARDWARE.display.status.path}/dpms`, "utf8").trim();
|
const status = (await fsp.readFile(`${HARDWARE.display.status.path}/dpms`, "utf8")).trim();
|
||||||
if (status !== HARDWARE.display.status.value) {
|
if (status !== HARDWARE.display.status.value) {
|
||||||
console.log("Update Display Status:", getDisplayStatus());
|
console.log("Update Display Status:", getDisplayStatus());
|
||||||
HARDWARE.display.status.value = status;
|
HARDWARE.display.status.value = status;
|
||||||
@@ -117,7 +118,7 @@ const update = () => {
|
|||||||
|
|
||||||
// Display brightness has changed
|
// Display brightness has changed
|
||||||
if (HARDWARE.support.displayBrightness) {
|
if (HARDWARE.support.displayBrightness) {
|
||||||
const brightness = fs.readFileSync(`${HARDWARE.display.brightness.path}/brightness`, "utf8").trim();
|
const brightness = (await fsp.readFile(`${HARDWARE.display.brightness.path}/brightness`, "utf8")).trim();
|
||||||
if (brightness !== HARDWARE.display.brightness.value) {
|
if (brightness !== HARDWARE.display.brightness.value) {
|
||||||
console.log("Update Display Brightness:", getDisplayBrightness());
|
console.log("Update Display Brightness:", getDisplayBrightness());
|
||||||
HARDWARE.display.brightness.value = brightness;
|
HARDWARE.display.brightness.value = brightness;
|
||||||
@@ -532,7 +533,7 @@ const rebootSystem = (callback = null) => {
|
|||||||
*/
|
*/
|
||||||
const processRuns = (name) => {
|
const processRuns = (name) => {
|
||||||
try {
|
try {
|
||||||
return !!process.execSync(`pidof ${name}`, { encoding: "utf8" });
|
return !!cpr.execSync(`pidof ${name}`, { encoding: "utf8" });
|
||||||
} catch {}
|
} catch {}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
@@ -545,7 +546,7 @@ const processRuns = (name) => {
|
|||||||
*/
|
*/
|
||||||
const commandExists = (name) => {
|
const commandExists = (name) => {
|
||||||
try {
|
try {
|
||||||
return !!process.execSync(`which ${name}`, { encoding: "utf8" });
|
return !!cpr.execSync(`which ${name}`, { encoding: "utf8" });
|
||||||
} catch {}
|
} catch {}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
@@ -559,7 +560,7 @@ const commandExists = (name) => {
|
|||||||
*/
|
*/
|
||||||
const execSyncCommand = (cmd, args = []) => {
|
const execSyncCommand = (cmd, args = []) => {
|
||||||
try {
|
try {
|
||||||
const output = process.execSync([cmd, ...args].join(" "), { encoding: "utf8" });
|
const output = cpr.execSync([cmd, ...args].join(" "), { encoding: "utf8" });
|
||||||
return output.trim().replace(/\0/g, "");
|
return output.trim().replace(/\0/g, "");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Execute Sync:", error.message);
|
console.error("Execute Sync:", error.message);
|
||||||
@@ -578,7 +579,7 @@ const execSyncCommand = (cmd, args = []) => {
|
|||||||
const execAsyncCommand = (cmd, args = [], callback = null) => {
|
const execAsyncCommand = (cmd, args = [], callback = null) => {
|
||||||
let errorOutput = "";
|
let errorOutput = "";
|
||||||
let successOutput = "";
|
let successOutput = "";
|
||||||
let proc = process.spawn(cmd, args);
|
let proc = cpr.spawn(cmd, args);
|
||||||
proc.stderr.on("data", (data) => {
|
proc.stderr.on("data", (data) => {
|
||||||
if (data) {
|
if (data) {
|
||||||
errorOutput += data.toString();
|
errorOutput += data.toString();
|
||||||
@@ -622,7 +623,7 @@ const dbusCall = (path, method, values, callback = null) => {
|
|||||||
const dest = `${iface} ${path} ${iface}.${method} ${values.join(" ")}`;
|
const dest = `${iface} ${path} ${iface}.${method} ${values.join(" ")}`;
|
||||||
const args = ["--print-reply", "--type=method_call", `--dest=${dest}`];
|
const args = ["--print-reply", "--type=method_call", `--dest=${dest}`];
|
||||||
try {
|
try {
|
||||||
const output = process.execSync([cmd, ...args].join(" "), { encoding: "utf8" });
|
const output = cpr.execSync([cmd, ...args].join(" "), { encoding: "utf8" });
|
||||||
if (typeof callback === "function") {
|
if (typeof callback === "function") {
|
||||||
callback(output.trim().replace(/\0/g, ""), null);
|
callback(output.trim().replace(/\0/g, ""), null);
|
||||||
}
|
}
|
||||||
@@ -644,7 +645,7 @@ const dbusCall = (path, method, values, callback = null) => {
|
|||||||
const dbusMonitor = (path, callback) => {
|
const dbusMonitor = (path, callback) => {
|
||||||
const cmd = "dbus-monitor";
|
const cmd = "dbus-monitor";
|
||||||
const args = [`interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='${path}'`];
|
const args = [`interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='${path}'`];
|
||||||
const proc = process.spawn(cmd, args);
|
const proc = cpr.spawn(cmd, args);
|
||||||
proc.stdout.on("data", (data) => {
|
proc.stdout.on("data", (data) => {
|
||||||
try {
|
try {
|
||||||
const signal = data.toString();
|
const signal = data.toString();
|
||||||
@@ -680,6 +681,26 @@ const dbusMonitor = (path, callback) => {
|
|||||||
return proc;
|
return proc;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function for asynchronous interval calls.
|
||||||
|
*
|
||||||
|
* @param {function} callback - An async callback function.
|
||||||
|
* @param {number} ms - Sleep time in milliseconds.
|
||||||
|
*/
|
||||||
|
const interval = (callback, ms) => {
|
||||||
|
const run = () => {
|
||||||
|
setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
await callback();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Interval Callback:", error.message);
|
||||||
|
}
|
||||||
|
run();
|
||||||
|
}, ms);
|
||||||
|
};
|
||||||
|
run();
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init,
|
init,
|
||||||
update,
|
update,
|
||||||
|
|||||||
@@ -307,10 +307,12 @@ const initDisplay = (client) => {
|
|||||||
const status = message.toString();
|
const status = message.toString();
|
||||||
console.log("Set Display Status:", status);
|
console.log("Set Display Status:", status);
|
||||||
hardware.setDisplayStatus(status);
|
hardware.setDisplayStatus(status);
|
||||||
|
hardware.update();
|
||||||
} else if (topic === `${root}/brightness/set`) {
|
} else if (topic === `${root}/brightness/set`) {
|
||||||
const brightness = parseInt(message, 10);
|
const brightness = parseInt(message, 10);
|
||||||
console.log("Set Display Brightness:", brightness);
|
console.log("Set Display Brightness:", brightness);
|
||||||
hardware.setDisplayBrightness(brightness);
|
hardware.setDisplayBrightness(brightness);
|
||||||
|
hardware.update();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.subscribe(`${root}/set`)
|
.subscribe(`${root}/set`)
|
||||||
|
|||||||
Reference in New Issue
Block a user