ref: verify hardware

This commit is contained in:
leukipp
2024-12-15 19:51:11 +01:00
parent 7f54996960
commit 8ac11731c8
3 changed files with 58 additions and 34 deletions
+54 -30
View File
@@ -18,34 +18,43 @@ global.HARDWARE = global.HARDWARE || {
* @param {Object} args - The command-line arguments to customize the initialization process.
* @returns {boolean} Returns true if the initialization was successful.
*/
const init = (args) => {
const model = getModel();
const valid = model && model.includes("Raspberry Pi 5");
// Check supported hardware
if (valid) {
console.log("\nModel:", getModel());
console.log("Serial Number:", getSerialNumber());
console.log("Host Name:", getHostName());
console.log("Up Time:", getUpTime());
console.log("Memory Size:", getMemorySize());
console.log("Memory Usage:", getMemoryUsage());
console.log("Processor Usage:", getProcessorUsage());
console.log("Processor Temperature:", getProcessorTemperature());
console.log("Display Status:", getDisplayStatus());
console.log("Display Brightness:", getDisplayBrightness(), "\n");
// Init globals
HARDWARE.initialized = true;
HARDWARE.status = "valid";
// Check for display changes
setInterval(update, 500);
} else {
console.warn(`Device ${model} not supported`);
const init = async (args) => {
if (!args.mqtt_url) {
return false;
}
return valid;
// Check supported hardware
if (!hardwareExists()) {
console.warn("Hardware not supported");
return false;
}
// Check supported model
const model = getModel();
if (!model || !model.includes("Raspberry Pi 5")) {
console.warn(`Device ${model} not supported`);
return false;
}
console.log("\nModel:", getModel());
console.log("Serial Number:", getSerialNumber());
console.log("Host Name:", getHostName());
console.log("Up Time:", getUpTime());
console.log("Memory Size:", getMemorySize());
console.log("Memory Usage:", getMemoryUsage());
console.log("Processor Usage:", getProcessorUsage());
console.log("Processor Temperature:", getProcessorTemperature());
console.log("Display Status:", getDisplayStatus());
console.log("Display Brightness:", getDisplayBrightness(), "\n");
// Init globals
HARDWARE.initialized = true;
HARDWARE.status = "valid";
// Check for display changes
setInterval(update, 500);
return true;
};
/**
@@ -78,6 +87,21 @@ const update = () => {
}
};
/**
* Verifies hardware compatibility by checking the presence of necessary sys files.
*
* @returns {bool} Returns true if all files exists.
*/
const hardwareExists = () => {
const files = [
"/sys/firmware/devicetree/base/model",
"/sys/firmware/devicetree/base/serial-number",
"/sys/class/backlight/10-0045/brightness",
"/sys/class/backlight/10-0045/bl_power",
];
return files.every((file) => fs.existsSync(file));
};
/**
* Gets the Raspberry Pi model name using `/sys/firmware/devicetree/base/model`.
*
@@ -192,8 +216,8 @@ const getDisplayStatus = () => {
*/
const setDisplayStatus = (status, callback = null) => {
if (status !== "ON" && status !== "OFF") {
console.error("Status must be 'ON' or 'OFF'.");
if (typeof callback === "function") callback(null, "Invalid status.");
console.error("Status must be 'ON' or 'OFF'");
if (typeof callback === "function") callback(null, "Invalid status");
return;
}
const args = status === "ON" ? ["--on", "*"] : ["--off", "*"];
@@ -240,8 +264,8 @@ const getDisplayBrightness = () => {
*/
const setDisplayBrightness = (brightness, callback = null) => {
if (typeof brightness !== "number" || brightness < 1 || brightness > 100) {
console.error("Brightness must be a number between 1 and 100.");
if (typeof callback === "function") callback(null, "Invalid brightness.");
console.error("Brightness must be a number between 1 and 100");
if (typeof callback === "function") callback(null, "Invalid brightness");
return;
}
const value = Math.max(1, Math.min(Math.round((brightness / 100) * 31), 31));
+2 -2
View File
@@ -13,7 +13,7 @@ global.INTEGRATION = global.INTEGRATION || {
* @param {Object} args - The command-line arguments to customize the initialization process.
* @returns {boolean} Returns true if the initialization was successful.
*/
const init = (args) => {
const init = async (args) => {
if (!args.mqtt_url) {
return false;
}
@@ -196,7 +196,7 @@ const initRefresh = (client) => {
)
.on("message", (topic, message) => {
if (topic === `${root}/execute`) {
console.log("Refresh webview...");
console.log("Refreshing webview...");
hardware.setDisplayStatus("ON");
WEBVIEW.view.webContents.reloadIgnoringCache();
}
+2 -2
View File
@@ -15,9 +15,9 @@ global.WEBVIEW = global.WEBVIEW || {
* @param {Object} args - The command-line arguments to customize the initialization process.
* @returns {boolean} Returns true if the initialization was successful.
*/
const init = (args) => {
const init = async (args) => {
if (!args.web_url) {
console.error("Please provide the '--web-url' parameter.");
console.error("Please provide the '--web-url' parameter");
return app.quit();
}
const url = new URL(args.web_url);