ESP32/tools/pc-metrics/start-metrics.ps1
2026-06-23 20:38:23 +08:00

40 lines
1.1 KiB
PowerShell

param(
[string]$Port = "COM3",
[double]$IntervalSeconds = 1
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$streamScript = Join-Path $scriptDir "stream-metrics.ps1"
$pidFile = Join-Path $scriptDir "metrics-stream.pid"
$logFile = Join-Path $scriptDir "metrics-stream.log"
$errFile = Join-Path $scriptDir "metrics-stream.err.log"
if (Test-Path $pidFile) {
$oldPid = Get-Content $pidFile -ErrorAction SilentlyContinue
if ($oldPid -and (Get-Process -Id ([int]$oldPid) -ErrorAction SilentlyContinue)) {
Write-Host "Metrics stream is already running. PID: $oldPid"
exit 0
}
}
$args = @(
"-NoProfile",
"-ExecutionPolicy", "Bypass",
"-File", "`"$streamScript`"",
"-Port", $Port,
"-IntervalSeconds", $IntervalSeconds
)
$process = Start-Process powershell.exe `
-ArgumentList $args `
-WindowStyle Hidden `
-RedirectStandardOutput $logFile `
-RedirectStandardError $errFile `
-PassThru
Set-Content -Path $pidFile -Value $process.Id -Encoding ascii
Write-Host "Metrics stream started. PID: $($process.Id)"
Write-Host "Log: $logFile"