From 640156ba87bd7498d5b5c720cf189ead1813fb0d Mon Sep 17 00:00:00 2001 From: leukipp Date: Tue, 7 Jan 2025 16:08:07 +0100 Subject: [PATCH] ref: use code prettier --- index.js | 7 +- package.json | 8 +- src/integration.js | 404 +++++++++++++++++++-------------------------- src/webview.js | 13 +- yarn.lock | 13 +- 5 files changed, 193 insertions(+), 252 deletions(-) diff --git a/index.js b/index.js index 0b72f2b..579a738 100644 --- a/index.js +++ b/index.js @@ -135,12 +135,7 @@ const promptArgs = async (proc) => { const answer = await read.question(prompt); const value = (answer.trim() || fallback.match(/[YN]/)[0]).toLowerCase(); if (!["y", "yes"].includes(value)) { - ignore = ignore.concat([ - "mqtt_url", - "mqtt_user", - "mqtt_password", - "mqtt_discovery", - ]); + ignore = ignore.concat(["mqtt_url", "mqtt_user", "mqtt_password", "mqtt_discovery"]); } } else if (key === "check") { const json = JSON.stringify(args, null, 2); diff --git a/package.json b/package.json index fbb74f2..c3cd456 100644 --- a/package.json +++ b/package.json @@ -49,14 +49,18 @@ ] } }, + "prettier": { + "printWidth": 120 + }, "devDependencies": { "@electron-forge/cli": "^7.6.0", "@electron-forge/maker-deb": "^7.6.0", "@electron-forge/maker-zip": "^7.6.0", "@electron-forge/publisher-github": "^7.6.0", - "electron": "^33.2.1" + "electron": "^33.3.1", + "prettier": "^3.4.2" }, "dependencies": { "mqtt": "^5.10.3" } -} \ No newline at end of file +} diff --git a/src/integration.js b/src/integration.js index 2c778b2..648e36b 100644 --- a/src/integration.js +++ b/src/integration.js @@ -90,12 +90,12 @@ const init = async (args) => { updateDisplay(INTEGRATION.client); }); - // Update heartbeat time periodically + // Update heartbeat time periodically (30s) setInterval(() => { updateHeartbeat(INTEGRATION.client); }, 30 * 1000); - // Update sensor states periodically + // Update sensor states periodically (1min) setInterval(update, 60 * 1000); return true; @@ -124,18 +124,15 @@ const update = () => { */ const initShutdown = (client) => { const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/shutdown`; + const config = { + name: "Shutdown", + unique_id: `${INTEGRATION.node}_shutdown`, + command_topic: `${root}/execute`, + icon: "mdi:power", + device: INTEGRATION.device, + }; client - .publish( - `${root}/config`, - JSON.stringify({ - name: "Shutdown", - unique_id: `${INTEGRATION.node}_shutdown`, - command_topic: `${root}/execute`, - icon: "mdi:power", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ) + .publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }) .on("message", (topic, message) => { if (topic === `${root}/execute`) { console.log("Shutdown system..."); @@ -153,18 +150,15 @@ const initShutdown = (client) => { */ const initReboot = (client) => { const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/reboot`; + const config = { + name: "Reboot", + unique_id: `${INTEGRATION.node}_reboot`, + command_topic: `${root}/execute`, + icon: "mdi:restart", + device: INTEGRATION.device, + }; client - .publish( - `${root}/config`, - JSON.stringify({ - name: "Reboot", - unique_id: `${INTEGRATION.node}_reboot`, - command_topic: `${root}/execute`, - icon: "mdi:restart", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ) + .publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }) .on("message", (topic, message) => { if (topic === `${root}/execute`) { console.log("Rebooting system..."); @@ -182,18 +176,15 @@ const initReboot = (client) => { */ const initRefresh = (client) => { const root = `${INTEGRATION.discovery}/button/${INTEGRATION.node}/refresh`; + const config = { + name: "Refresh", + unique_id: `${INTEGRATION.node}_refresh`, + command_topic: `${root}/execute`, + icon: "mdi:web-refresh", + device: INTEGRATION.device, + }; client - .publish( - `${root}/config`, - JSON.stringify({ - name: "Refresh", - unique_id: `${INTEGRATION.node}_refresh`, - command_topic: `${root}/execute`, - icon: "mdi:web-refresh", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ) + .publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }) .on("message", (topic, message) => { if (topic === `${root}/execute`) { console.log("Refreshing webview..."); @@ -211,27 +202,18 @@ const initRefresh = (client) => { */ const initKiosk = (client) => { const root = `${INTEGRATION.discovery}/select/${INTEGRATION.node}/kiosk`; + const config = { + name: `Kiosk`, + unique_id: `${INTEGRATION.node}_kiosk`, + command_topic: `${root}/set`, + state_topic: `${root}/status`, + value_template: "{{ value }}", + options: ["Framed", "Fullscreen", "Maximized", "Minimized", "Terminated"], + icon: "mdi:overscan", + device: INTEGRATION.device, + }; client - .publish( - `${root}/config`, - JSON.stringify({ - name: `Kiosk`, - unique_id: `${INTEGRATION.node}_kiosk`, - command_topic: `${root}/set`, - state_topic: `${root}/status`, - value_template: "{{ value }}", - options: [ - "Framed", - "Fullscreen", - "Maximized", - "Minimized", - "Terminated", - ], - icon: "mdi:overscan", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ) + .publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }) .on("message", (topic, message) => { if (topic === `${root}/set`) { const status = message.toString(); @@ -275,10 +257,9 @@ const initKiosk = (client) => { const updateKiosk = (client) => { const kiosk = WEBVIEW.status; const root = `${INTEGRATION.discovery}/select/${INTEGRATION.node}/kiosk`; - client.publish(`${root}/status`, `${kiosk}`, { - qos: 1, - retain: true, - }); + if (kiosk !== null) { + client.publish(`${root}/status`, `${kiosk}`, { qos: 1, retain: true }); + } }; /** @@ -288,23 +269,22 @@ const updateKiosk = (client) => { */ const initDisplay = (client) => { const root = `${INTEGRATION.discovery}/light/${INTEGRATION.node}/display`; + const config = { + name: `Display`, + unique_id: `${INTEGRATION.node}_display`, + command_topic: `${root}/set`, + state_topic: `${root}/status`, + icon: "mdi:monitor-shimmer", + platform: "light", + device: INTEGRATION.device, + }; + if (HARDWARE.display.brightness.path !== null) { + config.brightness_scale = 100; + config.brightness_command_topic = `${root}/brightness/set`; + config.brightness_state_topic = `${root}/brightness/status`; + } client - .publish( - `${root}/config`, - JSON.stringify({ - name: `Display`, - unique_id: `${INTEGRATION.node}_display`, - command_topic: `${root}/set`, - state_topic: `${root}/status`, - brightness_command_topic: `${root}/brightness/set`, - brightness_state_topic: `${root}/brightness/status`, - brightness_scale: 100, - icon: "mdi:monitor-shimmer", - platform: "light", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ) + .publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }) .on("message", (topic, message) => { if (topic === `${root}/set`) { const status = message.toString(); @@ -330,14 +310,12 @@ const updateDisplay = (client) => { const status = hardware.getDisplayStatus(); const brightness = hardware.getDisplayBrightness(); const root = `${INTEGRATION.discovery}/light/${INTEGRATION.node}/display`; - client.publish(`${root}/status`, `${status}`, { - qos: 1, - retain: true, - }); - client.publish(`${root}/brightness/status`, `${brightness}`, { - qos: 1, - retain: true, - }); + if (status !== null) { + client.publish(`${root}/status`, `${status}`, { qos: 1, retain: true }); + } + if (brightness !== null) { + client.publish(`${root}/brightness/status`, `${brightness}`, { qos: 1, retain: true }); + } }; /** @@ -347,18 +325,15 @@ const updateDisplay = (client) => { */ const initModel = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/model`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Model`, - unique_id: `${INTEGRATION.node}_model`, - state_topic: `${root}/status`, - value_template: "{{ value }}", - icon: "mdi:raspberry-pi", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Model`, + unique_id: `${INTEGRATION.node}_model`, + state_topic: `${root}/status`, + value_template: "{{ value }}", + icon: "mdi:raspberry-pi", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateModel(client); }; @@ -370,10 +345,9 @@ const initModel = (client) => { const updateModel = (client) => { const model = hardware.getModel(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/model`; - client.publish(`${root}/status`, `${model}`, { - qos: 1, - retain: true, - }); + if (model !== null) { + client.publish(`${root}/status`, `${model}`, { qos: 1, retain: true }); + } }; /** @@ -383,18 +357,15 @@ const updateModel = (client) => { */ const initSerialNumber = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/serial_number`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Serial Number`, - unique_id: `${INTEGRATION.node}_serial_number`, - state_topic: `${root}/status`, - value_template: "{{ value }}", - icon: "mdi:hexadecimal", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Serial Number`, + unique_id: `${INTEGRATION.node}_serial_number`, + state_topic: `${root}/status`, + value_template: "{{ value }}", + icon: "mdi:hexadecimal", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateSerialNumber(client); }; @@ -406,10 +377,9 @@ const initSerialNumber = (client) => { const updateSerialNumber = (client) => { const serialNumber = hardware.getSerialNumber(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/serial_number`; - client.publish(`${root}/status`, `${serialNumber}`, { - qos: 1, - retain: true, - }); + if (serialNumber !== null) { + client.publish(`${root}/status`, `${serialNumber}`, { qos: 1, retain: true }); + } }; /** @@ -419,18 +389,15 @@ const updateSerialNumber = (client) => { */ const initHostName = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/host_name`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Host Name`, - unique_id: `${INTEGRATION.node}_host_name`, - state_topic: `${root}/status`, - value_template: "{{ value }}", - icon: "mdi:console-network", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Host Name`, + unique_id: `${INTEGRATION.node}_host_name`, + state_topic: `${root}/status`, + value_template: "{{ value }}", + icon: "mdi:console-network", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateHostName(client); }; @@ -442,10 +409,9 @@ const initHostName = (client) => { const updateHostName = (client) => { const hostName = hardware.getHostName(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/host_name`; - client.publish(`${root}/status`, `${hostName}`, { - qos: 1, - retain: true, - }); + if (hostName !== null) { + client.publish(`${root}/status`, `${hostName}`, { qos: 1, retain: true }); + } }; /** @@ -455,19 +421,16 @@ const updateHostName = (client) => { */ const initUpTime = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/up_time`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Up Time`, - unique_id: `${INTEGRATION.node}_up_time`, - state_topic: `${root}/status`, - value_template: "{{ (value | float) | round(0) }}", - unit_of_measurement: "min", - icon: "mdi:timeline-clock", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Up Time`, + unique_id: `${INTEGRATION.node}_up_time`, + state_topic: `${root}/status`, + value_template: "{{ (value | float) | round(0) }}", + unit_of_measurement: "min", + icon: "mdi:timeline-clock", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateUpTime(client); }; @@ -479,10 +442,9 @@ const initUpTime = (client) => { const updateUpTime = (client) => { const upTime = hardware.getUpTime(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/up_time`; - client.publish(`${root}/status`, `${upTime}`, { - qos: 1, - retain: true, - }); + if (upTime !== null) { + client.publish(`${root}/status`, `${upTime}`, { qos: 1, retain: true }); + } }; /** @@ -492,19 +454,16 @@ const updateUpTime = (client) => { */ const initMemorySize = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_size`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Memory Size`, - unique_id: `${INTEGRATION.node}_memory_size`, - state_topic: `${root}/status`, - value_template: "{{ (value | float) | round(2) }}", - unit_of_measurement: "GiB", - icon: "mdi:memory", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Memory Size`, + unique_id: `${INTEGRATION.node}_memory_size`, + state_topic: `${root}/status`, + value_template: "{{ (value | float) | round(2) }}", + unit_of_measurement: "GiB", + icon: "mdi:memory", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateMemorySize(client); }; @@ -516,10 +475,9 @@ const initMemorySize = (client) => { const updateMemorySize = (client) => { const memorySize = hardware.getMemorySize(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_size`; - client.publish(`${root}/status`, `${memorySize}`, { - qos: 1, - retain: true, - }); + if (memorySize !== null) { + client.publish(`${root}/status`, `${memorySize}`, { qos: 1, retain: true }); + } }; /** @@ -529,19 +487,16 @@ const updateMemorySize = (client) => { */ const initMemoryUsage = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_usage`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Memory Usage`, - unique_id: `${INTEGRATION.node}_memory_usage`, - state_topic: `${root}/status`, - value_template: "{{ (value | float) | round(0) }}", - unit_of_measurement: "%", - icon: "mdi:memory-arrow-down", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Memory Usage`, + unique_id: `${INTEGRATION.node}_memory_usage`, + state_topic: `${root}/status`, + value_template: "{{ (value | float) | round(0) }}", + unit_of_measurement: "%", + icon: "mdi:memory-arrow-down", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateMemoryUsage(client); }; @@ -553,10 +508,9 @@ const initMemoryUsage = (client) => { const updateMemoryUsage = (client) => { const memoryUsage = hardware.getMemoryUsage(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/memory_usage`; - client.publish(`${root}/status`, `${memoryUsage}`, { - qos: 1, - retain: true, - }); + if (memoryUsage !== null) { + client.publish(`${root}/status`, `${memoryUsage}`, { qos: 1, retain: true }); + } }; /** @@ -566,19 +520,16 @@ const updateMemoryUsage = (client) => { */ const initProcessorUsage = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_usage`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Processor Usage`, - unique_id: `${INTEGRATION.node}_processor_usage`, - state_topic: `${root}/status`, - value_template: "{{ (value | float) | round(0) }}", - unit_of_measurement: "%", - icon: "mdi:cpu-64-bit", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Processor Usage`, + unique_id: `${INTEGRATION.node}_processor_usage`, + state_topic: `${root}/status`, + value_template: "{{ (value | float) | round(0) }}", + unit_of_measurement: "%", + icon: "mdi:cpu-64-bit", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateProcessorUsage(client); }; @@ -590,10 +541,9 @@ const initProcessorUsage = (client) => { const updateProcessorUsage = (client) => { const processorUsage = hardware.getProcessorUsage(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_usage`; - client.publish(`${root}/status`, `${processorUsage}`, { - qos: 1, - retain: true, - }); + if (processorUsage !== null) { + client.publish(`${root}/status`, `${processorUsage}`, { qos: 1, retain: true }); + } }; /** @@ -603,19 +553,16 @@ const updateProcessorUsage = (client) => { */ const initProcessorTemperature = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_temperature`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Processor Temperature`, - unique_id: `${INTEGRATION.node}_processor_temperature`, - state_topic: `${root}/status`, - value_template: "{{ (value | float) | round(0) }}", - unit_of_measurement: "°C", - icon: "mdi:radiator", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Processor Temperature`, + unique_id: `${INTEGRATION.node}_processor_temperature`, + state_topic: `${root}/status`, + value_template: "{{ (value | float) | round(0) }}", + unit_of_measurement: "°C", + icon: "mdi:radiator", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateProcessorTemperature(client); }; @@ -627,10 +574,9 @@ const initProcessorTemperature = (client) => { const updateProcessorTemperature = (client) => { const processorTemperature = hardware.getProcessorTemperature(); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/processor_temperature`; - client.publish(`${root}/status`, `${processorTemperature}`, { - qos: 1, - retain: true, - }); + if (processorTemperature !== null) { + client.publish(`${root}/status`, `${processorTemperature}`, { qos: 1, retain: true }); + } }; /** @@ -640,18 +586,15 @@ const updateProcessorTemperature = (client) => { */ const initHeartbeat = (client) => { const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/heartbeat`; - client.publish( - `${root}/config`, - JSON.stringify({ - name: `Heartbeat`, - unique_id: `${INTEGRATION.node}_heartbeat`, - state_topic: `${root}/status`, - value_template: "{{ value }}", - icon: "mdi:heart-flash", - device: INTEGRATION.device, - }), - { qos: 1, retain: true } - ); + const config = { + name: `Heartbeat`, + unique_id: `${INTEGRATION.node}_heartbeat`, + state_topic: `${root}/status`, + value_template: "{{ value }}", + icon: "mdi:heart-flash", + device: INTEGRATION.device, + }; + client.publish(`${root}/config`, JSON.stringify(config), { qos: 1, retain: true }); updateHeartbeat(client); }; @@ -665,10 +608,7 @@ const updateHeartbeat = (client) => { const local = new Date(now.getTime() - now.getTimezoneOffset() * 60 * 1000); const heartbeat = local.toISOString().replace(/\.\d{3}Z$/, ""); const root = `${INTEGRATION.discovery}/sensor/${INTEGRATION.node}/heartbeat`; - client.publish(`${root}/status`, `${heartbeat}`, { - qos: 1, - retain: true, - }); + client.publish(`${root}/status`, `${heartbeat}`, { qos: 1, retain: true }); }; module.exports = { diff --git a/src/webview.js b/src/webview.js index 9470363..a4a0ecc 100644 --- a/src/webview.js +++ b/src/webview.js @@ -93,7 +93,7 @@ const windowEvents = (window, zoom) => { window.blur(); } /* This also works, but it doesn't allow to react for screen wake up. - window.setIgnoreMouseEvents(getDisplayStatus() === "OFF"); + window.setIgnoreMouseEvents(hardware.getDisplayStatus() === "OFF"); */ }); window.on("focus", () => { @@ -136,13 +136,10 @@ const windowEvents = (window, zoom) => { const viewEvents = (view) => { // Enable webview touch emulation view.webContents.debugger.attach("1.1"); - view.webContents.debugger.sendCommand( - "Emulation.setEmitTouchEventsForMouse", - { - enabled: true, - configuration: "mobile", - } - ); + view.webContents.debugger.sendCommand("Emulation.setEmitTouchEventsForMouse", { + enabled: true, + configuration: "mobile", + }); // Disable webview hyperlinks view.webContents.setWindowOpenHandler(() => { diff --git a/yarn.lock b/yarn.lock index 97bf50e..25cd4f1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1129,10 +1129,10 @@ electron-installer-debian@^3.2.0: word-wrap "^1.2.3" yargs "^16.0.2" -electron@^33.2.1: - version "33.2.1" - resolved "https://registry.yarnpkg.com/electron/-/electron-33.2.1.tgz#d0d7bba7a7abf4f14881d0a6e03c498b301a2d5f" - integrity sha512-SG/nmSsK9Qg1p6wAW+ZfqU+AV8cmXMTIklUL18NnOKfZLlum4ZsDoVdmmmlL39ZmeCaq27dr7CgslRPahfoVJg== +electron@^33.3.1: + version "33.3.1" + resolved "https://registry.yarnpkg.com/electron/-/electron-33.3.1.tgz#26d63b3a110c4a9db639c33633bf806f91b4f649" + integrity sha512-Z7l2bVgpdKxHQMI4i0CirBX2n+iCYKOx5mbzNM3BpOyFELwlobEXKmzCmEnwP+3EcNeIhUQyIEBFQxN06QgdIw== dependencies: "@electron/get" "^2.0.0" "@types/node" "^20.9.0" @@ -2396,6 +2396,11 @@ postject@^1.0.0-alpha.6: dependencies: commander "^9.4.0" +prettier@^3.4.2: + version "3.4.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.4.2.tgz#a5ce1fb522a588bf2b78ca44c6e6fe5aa5a2b13f" + integrity sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ== + proc-log@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/proc-log/-/proc-log-2.0.1.tgz#8f3f69a1f608de27878f91f5c688b225391cb685"