90 lines
2.7 KiB
PowerShell
90 lines
2.7 KiB
PowerShell
param(
|
|
[string]$Name = "RLCD-Metrics",
|
|
[double]$IntervalSeconds = 1,
|
|
[string]$Disk = "C:\"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$venvDir = Join-Path $scriptDir ".venv-ble"
|
|
$venvPythonExe = Join-Path $venvDir "Scripts\python.exe"
|
|
$pythonExe = $venvPythonExe
|
|
$venvSitePackages = Join-Path $venvDir "Lib\site-packages"
|
|
$requirements = Join-Path $scriptDir "requirements-ble.txt"
|
|
$pushScript = Join-Path $scriptDir "push-metrics-ble.py"
|
|
$pidFile = Join-Path $scriptDir "metrics-ble.pid"
|
|
$logFile = Join-Path $scriptDir "metrics-ble.log"
|
|
$errFile = Join-Path $scriptDir "metrics-ble.err.log"
|
|
|
|
function Get-MetricsProcesses {
|
|
Get-CimInstance Win32_Process -Filter "name='python.exe'" |
|
|
Where-Object { $_.CommandLine -and $_.CommandLine.Contains($pushScript) }
|
|
}
|
|
|
|
if (-not (Test-Path $pythonExe)) {
|
|
python -m venv $venvDir
|
|
& $venvPythonExe -m pip install --upgrade pip
|
|
& $venvPythonExe -m pip install -r $requirements
|
|
}
|
|
|
|
$venvConfig = Join-Path $venvDir "pyvenv.cfg"
|
|
if (Test-Path $venvConfig) {
|
|
$executableLine = Get-Content $venvConfig |
|
|
Where-Object { $_ -match '^executable\s*=' } |
|
|
Select-Object -First 1
|
|
if ($executableLine) {
|
|
$basePython = ($executableLine -replace '^executable\s*=\s*', '').Trim()
|
|
if ($basePython -and (Test-Path $basePython)) {
|
|
$pythonExe = $basePython
|
|
}
|
|
}
|
|
}
|
|
|
|
$running = @(Get-MetricsProcesses)
|
|
if ($running.Count -gt 0) {
|
|
$existingPid = $running[0].ProcessId
|
|
Set-Content -Path $pidFile -Value $existingPid -Encoding ascii
|
|
Write-Host "BLE metrics stream is already running. PID: $existingPid"
|
|
exit 0
|
|
}
|
|
|
|
if (Test-Path $pidFile) {
|
|
$oldPid = Get-Content $pidFile -ErrorAction SilentlyContinue
|
|
if ($oldPid -and (Get-Process -Id ([int]$oldPid) -ErrorAction SilentlyContinue)) {
|
|
Write-Host "BLE metrics stream is already running. PID: $oldPid"
|
|
exit 0
|
|
}
|
|
}
|
|
|
|
$args = @(
|
|
"`"$pushScript`"",
|
|
"--name", $Name,
|
|
"--interval", $IntervalSeconds,
|
|
"--disk", $Disk
|
|
)
|
|
|
|
$oldPythonPath = $env:PYTHONPATH
|
|
if ($pythonExe -ne $venvPythonExe -and (Test-Path $venvSitePackages)) {
|
|
if ($oldPythonPath) {
|
|
$env:PYTHONPATH = "$venvSitePackages;$oldPythonPath"
|
|
} else {
|
|
$env:PYTHONPATH = $venvSitePackages
|
|
}
|
|
}
|
|
|
|
try {
|
|
$process = Start-Process $pythonExe `
|
|
-ArgumentList $args `
|
|
-WindowStyle Hidden `
|
|
-RedirectStandardOutput $logFile `
|
|
-RedirectStandardError $errFile `
|
|
-PassThru
|
|
} finally {
|
|
$env:PYTHONPATH = $oldPythonPath
|
|
}
|
|
|
|
Set-Content -Path $pidFile -Value $process.Id -Encoding ascii
|
|
Write-Host "BLE metrics stream started. PID: $($process.Id)"
|
|
Write-Host "Log: $logFile"
|