param([switch]$Restart) $ErrorActionPreference='Stop' $configPath=if($env:BPQ_CONSOLE_CONFIG){$env:BPQ_CONSOLE_CONFIG}else{Join-Path $PSScriptRoot 'console.config.json'} if(!(Test-Path -LiteralPath $configPath)){throw 'Configurazione assente: eseguire Install.ps1 prima di avviare il pannello.'} $settings=Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json $port=if($env:BPQ_CONSOLE_PORT){[int]$env:BPQ_CONSOLE_PORT}elseif($settings.port){[int]$settings.port}else{8090} if($port -lt 1024 -or $port -gt 65535){throw 'Porta pannello non valida.'} $nodeExe=if($settings.nodeExe -and (Test-Path -LiteralPath $settings.nodeExe)){$settings.nodeExe}else{(Get-Command node.exe -ErrorAction SilentlyContinue).Source} if(!$nodeExe){throw 'Node.js non trovato. Eseguire Install.ps1 -NodeExe con il percorso corretto.'} $version=& $nodeExe --version if($LASTEXITCODE -ne 0 -or $version -notmatch '^v(\d+)\.' -or [int]$Matches[1] -lt 20){throw 'Serve Node.js 20 o successivo.'} $serverPath=Join-Path $PSScriptRoot 'server.mjs' $owned=@(Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object {$_.CommandLine -and $_.CommandLine.IndexOf($serverPath,[StringComparison]::OrdinalIgnoreCase) -ge 0}) $target=Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue if($target -and @($owned.ProcessId) -notcontains $target[0].OwningProcess){throw "Porta $port occupata da un altro processo: scegliere un'altra porta con Install.ps1 -Port."} if($Restart -and $owned.Count){ foreach($stateFile in @('winlink-radio.session.json','manual-forwarding.session.json')){ $pending=Join-Path $PSScriptRoot $stateFile if((Test-Path -LiteralPath $pending) -and (Get-Content -LiteralPath $pending -Raw | ConvertFrom-Json).restoring){throw 'Attendere il ripristino della sessione radio prima di riavviare il pannello.'} } foreach($proc in $owned){Stop-Process -Id $proc.ProcessId -ErrorAction Stop;Wait-Process -Id $proc.ProcessId -Timeout 10 -ErrorAction SilentlyContinue} } $listener=Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue if($listener){ $proc=Get-CimInstance Win32_Process -Filter "ProcessId=$($listener[0].OwningProcess)" if($proc.Name -eq 'node.exe' -and $proc.CommandLine -and $proc.CommandLine.IndexOf($serverPath,[StringComparison]::OrdinalIgnoreCase) -ge 0){ if((Get-Item -LiteralPath $configPath).LastWriteTime -gt $proc.CreationDate){throw 'Configurazione modificata: usare Start-Console.ps1 -Restart per applicarla.'} Write-Host "Pannello gia in esecuzione su http://127.0.0.1:$port";return } throw "Porta $port occupata da un altro processo: scegliere un'altra porta con Install.ps1 -Port." } if(!$Restart -and $owned.Count){throw "Il pannello di questa cartella e gia in esecuzione su un'altra porta: usare -Restart."} $child=Start-Process -FilePath $nodeExe -ArgumentList ('"'+$serverPath+'"') -WorkingDirectory $PSScriptRoot -WindowStyle Hidden -RedirectStandardOutput (Join-Path $PSScriptRoot 'server.log') -RedirectStandardError (Join-Path $PSScriptRoot 'server-error.log') -PassThru $ready=$false for($attempt=0;$attempt -lt 40;$attempt++){ $child.Refresh();if($child.HasExited){break} try{$health=Invoke-RestMethod -Uri "http://127.0.0.1:$port/health" -TimeoutSec 1;if($health.service -eq 'bpq-console' -and $health.pid -eq $child.Id){$ready=$true;break}}catch{} Start-Sleep -Milliseconds 250 } if(!$ready){if(!$child.HasExited){Stop-Process -Id $child.Id -ErrorAction SilentlyContinue};throw 'Avvio pannello non confermato: consultare server-error.log e correggere la configurazione.'} Write-Host "Pannello avviato su http://127.0.0.1:$port"