From 874b77279efff293334e538a34bd0fd51aefeaf4 Mon Sep 17 00:00:00 2001 From: erivieccio Date: Tue, 3 Sep 2024 11:26:03 +0200 Subject: [PATCH] workaround pycaw crash for fix https://github.com/AndreMiras/pycaw/issues/85 --- client-scripts/windows/install.ps1 | 14 +++-- client-scripts/windows/mic-in-use-windows.py | 2 +- .../windows/run-mic-in-use-windows.py | 51 +++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 client-scripts/windows/run-mic-in-use-windows.py diff --git a/client-scripts/windows/install.ps1 b/client-scripts/windows/install.ps1 index 895a13d..155189d 100644 --- a/client-scripts/windows/install.ps1 +++ b/client-scripts/windows/install.ps1 @@ -8,10 +8,10 @@ # Paths for installation and files $installPath = "C:\busylight" -$pythonScriptPath = "$installPath\mic-in-use-windows.py" +$pythonScriptPath = "$installPath\run-mic-in-use-windows.py" $requirementsPath = "$installPath\requirements.txt" $taskName = "MicInUseTask" -$taskDescription = "Task to run the mic-in-use-windows.py script at logon." +$taskDescription = "Task to run the run-mic-in-use-windows.py script at logon." # Create installation folder if it does not exist if (-not (Test-Path -Path $installPath)) { @@ -23,9 +23,17 @@ if (-not (Test-Path -Path $installPath)) { $scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition # Check if the files exist and move them +if (Test-Path "$scriptDirectory\run-mic-in-use-windows.py") { + Write-Output "Moving run-mic-in-use-windows.py to $installPath..." + Move-Item -Path "$scriptDirectory\run-mic-in-use-windows.py" -Destination $pythonScriptPath -Force +} else { + Write-Error "run-mic-in-use-windows.py not found in the script directory." + exit 1 +} + if (Test-Path "$scriptDirectory\mic-in-use-windows.py") { Write-Output "Moving mic-in-use-windows.py to $installPath..." - Move-Item -Path "$scriptDirectory\mic-in-use-windows.py" -Destination $pythonScriptPath -Force + Move-Item -Path "$scriptDirectory\mic-in-use-windows.py" -Destination "$installPath\mic-in-use-windows.py" -Force } else { Write-Error "mic-in-use-windows.py not found in the script directory." exit 1 diff --git a/client-scripts/windows/mic-in-use-windows.py b/client-scripts/windows/mic-in-use-windows.py index ec6f2a0..317febb 100644 --- a/client-scripts/windows/mic-in-use-windows.py +++ b/client-scripts/windows/mic-in-use-windows.py @@ -90,7 +90,7 @@ def is_microphone_in_use(): if process_name and process_name not in ignored_processes: return process_name except Exception as e: - continue # Continue to the next session instead of halting + print(f"Error checking session: {e}") return None diff --git a/client-scripts/windows/run-mic-in-use-windows.py b/client-scripts/windows/run-mic-in-use-windows.py new file mode 100644 index 0000000..1dc23bf --- /dev/null +++ b/client-scripts/windows/run-mic-in-use-windows.py @@ -0,0 +1,51 @@ +# --------------------------------------------------------------------------------------- +# Project: BusyLight Windows Client - Runner Script +# Author: Evaristo R. Rivieccio Vega - SysAdmin +# GitHub: https://github.com/evaristorivi +# LinkedIn: https://www.linkedin.com/in/evaristorivieccio/ +# Web: https://www.evaristorivieccio.es/ +# --------------------------------------------------------------------------------------- +# Description: +# This Python script is a runner for the BusyLight Windows Client. It is designed to +# ensure that the main script, mic-in-use-windows.py, continues to run indefinitely by +# restarting it if it closes unexpectedly. +# +# The main script monitors the status of audio sessions in real-time and sends signals +# to the BusyLight API to control the LED strip based on whether the microphone is in use +# or not. +# +# The runner script is responsible for: +# - Launching the main script. +# - Detecting if the main script terminates. +# - Restarting the main script if it exits. +# +# Usage: +# Run this script to ensure that the BusyLight client script is always running. +# +# --------------------------------------------------------------------------------------- + +import subprocess +import time +import os +import sys + +def run_script(): + print("The service is running. You can minimise this window.") + script_path = os.path.join(os.path.dirname(__file__), 'mic-in-use-windows.py') + + process = subprocess.Popen([sys.executable, script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + while True: + exit_code = process.wait() + + # stdout, stderr = process.communicate() + # if stdout: + # print(stdout.decode()) + # if stderr: + # print(stderr.decode()) + + time.sleep(1) + process = subprocess.Popen([sys.executable, script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + +if __name__ == "__main__": + run_script()