diff --git a/src/hardware.js b/src/hardware.js index 11c30eb..67cde72 100644 --- a/src/hardware.js +++ b/src/hardware.js @@ -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)); diff --git a/src/integration.js b/src/integration.js index ce22f8e..2c778b2 100644 --- a/src/integration.js +++ b/src/integration.js @@ -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(); } diff --git a/src/webview.js b/src/webview.js index 850d641..60cc5c1 100644 --- a/src/webview.js +++ b/src/webview.js @@ -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);