# ============================================================================== # lf_mri_platform -- Start services and GUI # Usage: # .\start.ps1 -- plug mode: Docker + GUI # .\start.ps1 -Mode real -- real mode: Docker + spectrometer + GUI # .\start.ps1 -GuiOnly -- GUI only (services already running) # .\start.ps1 -ServicesOnly -- Docker + spectrometer, no GUI # .\start.ps1 -SkipInstall -- skip venv check # .\start.ps1 -SkipSpectrometer -- skip native spectrometer even in real mode # ============================================================================== param( [ValidateSet("plug", "real")] [string]$Mode = "plug", [switch]$GuiOnly, [switch]$ServicesOnly, [switch]$SkipInstall, [switch]$SkipSpectrometer ) $ErrorActionPreference = "Stop" $Root = $PSScriptRoot $GuiDir = Join-Path $Root "apps\gui" $VenvPython = Join-Path $GuiDir ".venv\Scripts\python.exe" $AppScript = Join-Path $Root "apps\gui\app.py" $EnvFile = Join-Path $Root ".env" $EnvExample = Join-Path $Root ".env.example" function Write-Step($msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan } function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor Yellow } function Write-Fail($msg) { Write-Host "`n[FAIL] $msg" -ForegroundColor Red Write-Host "`n Press Enter to close..." -ForegroundColor DarkGray $null = Read-Host exit 1 } # Keep window open on any unhandled error trap { Write-Host "`n[ERROR] $_" -ForegroundColor Red Write-Host "`n Press Enter to close..." -ForegroundColor DarkGray $null = Read-Host exit 1 } function Get-EnvPort($key, $default) { if (Test-Path $EnvFile) { $line = Get-Content $EnvFile | Select-String "^$key=(\d+)" if ($line) { return $line.Matches[0].Groups[1].Value } } return $default } # -- 1. Check / install GUI venv ----------------------------------------------- if (-not $GuiOnly -and -not $SkipInstall) { Write-Step "Checking GUI environment" $needsInstall = $false if (-not (Test-Path $VenvPython)) { Write-Warn "Virtual environment not found -- running install" $needsInstall = $true } else { & $VenvPython -c "import PySide6" 2>$null if ($LASTEXITCODE -ne 0) { Write-Warn "GUI packages missing -- running install" $needsInstall = $true } else { Write-OK "Virtual environment ready" } } if ($needsInstall) { $installScript = Join-Path $Root "install.ps1" if (-not (Test-Path $installScript)) { Write-Fail "install.ps1 not found" } & powershell.exe -ExecutionPolicy Bypass -File $installScript if ($LASTEXITCODE -ne 0) { Write-Fail "install.ps1 failed" } } } # -- 2. Docker ----------------------------------------------------------------- if (-not $GuiOnly) { Write-Step "Checking Docker" $dockerOk = $false try { & docker info *>$null; $dockerOk = $true } catch {} if (-not $dockerOk) { Write-Warn "Docker not responding -- starting Docker Desktop..." $dockerExe = "C:\Program Files\Docker\Docker\Docker Desktop.exe" if (Test-Path $dockerExe) { Start-Process $dockerExe } else { Write-Fail "Docker Desktop not found -- install from https://docker.com" } $waited = 0 while ($waited -lt 60) { Start-Sleep 5 $waited += 5 try { & docker info *>$null; $dockerOk = $true; break } catch {} Write-Host " ... $waited/60 s" -ForegroundColor DarkGray } if (-not $dockerOk) { Write-Fail "Docker did not start in time" } } Write-OK "Docker is running" # -- 3. .env --------------------------------------------------------------- if (-not (Test-Path $EnvFile)) { if (Test-Path $EnvExample) { Copy-Item $EnvExample $EnvFile Write-Warn ".env created from .env.example" } else { Write-Warn ".env missing -- using Compose defaults" } } # -- 4. Start containers --------------------------------------------------- Write-Step "Starting Docker services (mode: $Mode)" $env:ORCHESTRATOR_MODE = $Mode $envArgs = if (Test-Path $EnvFile) { @("--env-file", $EnvFile) } else { @() } & docker compose @envArgs up -d if ($LASTEXITCODE -ne 0) { Write-Fail "docker compose up failed" } Write-OK "Containers started" # -- 5. Native spectrometer ------------------------------------------------ if (-not $SkipSpectrometer) { Write-Step "Starting native spectrometer" $SpecDir = Join-Path $Root "services\spectrometer" $SpecVenv = Join-Path $SpecDir "mvenv\Scripts\python.exe" $PicoExe = Join-Path $SpecDir "bin\pico-tcp.exe" $oldPico = Get-Process -Name "pico-tcp" -ErrorAction SilentlyContinue if ($oldPico) { $oldPico | Stop-Process -Force; Write-Warn "Killed stale pico-tcp.exe" } if (-not (Test-Path $SpecVenv)) { Write-Warn "Spectrometer venv not found -- creating..." Push-Location $SpecDir & python -m venv mvenv if ($LASTEXITCODE -ne 0) { Write-Fail "Failed to create spectrometer venv" } & $SpecVenv -m pip install -q --upgrade pip & $SpecVenv -m pip install -q -r requirements.txt if ($LASTEXITCODE -ne 0) { Write-Fail "Failed to install spectrometer packages" } Pop-Location Write-OK "Spectrometer venv created" } else { Write-OK "Spectrometer venv found" } Push-Location $SpecDir & $SpecVenv manage.py migrate --noinput 2>&1 | Out-Null Pop-Location Write-OK "Migrations applied" if (Test-Path $PicoExe) { Start-Process $PicoExe -WindowStyle Normal Write-OK "pico-tcp.exe started (visible window)" } else { Write-Warn "bin\pico-tcp.exe not found -- ADC proxy not started" } # Check if runserver is already up by probing port 8000 $specPort = Get-EnvPort "SPECTROMETER_PORT" "8000" $alreadyUp = $false try { $r = Invoke-WebRequest "http://localhost:$specPort/api/" ` -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop $alreadyUp = ($r.StatusCode -lt 400) } catch {} if ($alreadyUp) { Write-OK "Spectrometer already responding on port $specPort" } else { Start-Process $SpecVenv ` -ArgumentList "manage.py runserver 0.0.0.0:$specPort" ` -WorkingDirectory $SpecDir ` -WindowStyle Normal Write-OK "Spectrometer started in new window (port $specPort)" } } # -- 6. Health check ------------------------------------------------------- Write-Step "Waiting for services to become healthy" $checkSpec = -not $SkipSpectrometer $services = @( @{ Name = "Orchestrator"; Url = "http://localhost:$(Get-EnvPort 'ORCHESTRATOR_PORT' '1717')/health"; Required = $true }, @{ Name = "Seq-Interp"; Url = "http://localhost:$(Get-EnvPort 'SEQ_INTERP_PORT' '7475')/health"; Required = $true }, @{ Name = "Reconstructor"; Url = "http://localhost:$(Get-EnvPort 'RECONSTRUCTOR_PORT' '8081')/health"; Required = $true }, @{ Name = "Spectroscopy"; Url = "http://localhost:$(Get-EnvPort 'SPECTROSCOPY_PORT' '8002')/health"; Required = $true }, @{ Name = "Spectrometer"; Url = "http://localhost:$(Get-EnvPort 'SPECTROMETER_PORT' '8000')/api/"; Required = $checkSpec } ) $maxWait = 120; $interval = 3; $elapsed = 0 while ($elapsed -lt $maxWait) { $pending = @() foreach ($svc in $services) { if (-not $svc.Required) { continue } $up = $false try { $r = Invoke-WebRequest $svc.Url -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop $up = ($r.StatusCode -lt 400) } catch {} if (-not $up) { $pending += $svc.Name } } if ($pending.Count -eq 0) { Write-OK "All required services healthy"; break } Write-Host (" ... {0}/{1} s waiting: {2}" -f $elapsed, $maxWait, ($pending -join ", ")) -ForegroundColor DarkGray Start-Sleep $interval $elapsed += $interval } if ($elapsed -ge $maxWait) { Write-Warn "Some services did not respond in time -- continuing anyway" } Write-Host "" foreach ($svc in $services) { $ok = $false try { $r = Invoke-WebRequest $svc.Url -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop $ok = ($r.StatusCode -lt 400) } catch {} $icon = if ($ok) { "[OK]" } else { "[--]" } $color = if ($ok) { "Green" } elseif ($svc.Required) { "Yellow" } else { "DarkGray" } if (-not $svc.Required -and -not $ok) { $suffix = " (native -- start manually)" } else { $suffix = "" } Write-Host (" {0,-6} {1,-16} {2}{3}" -f $icon, $svc.Name, $svc.Url, $suffix) -ForegroundColor $color } # -- 7. Write server_config.json ------------------------------------------- Write-Step "Configuring GUI" $orchPort = Get-EnvPort "ORCHESTRATOR_PORT" "1717" $seqPort = Get-EnvPort "SEQ_INTERP_PORT" "7475" $spectPort = Get-EnvPort "SPECTROSCOPY_PORT" "8002" $cfgPath = Join-Path $Root "apps\gui\cfg\server_config.json" $cfgObj = @{ srv_name = "srv_interp" log_dir = "log" upload_dir = "data/input" output_dir = "data/output" server_host = "0.0.0.0" server_port = [int]$seqPort orchestrator_url = "http://localhost:$orchPort" seq_interp_url = "http://localhost:$seqPort" spectroscopy_url = "http://localhost:$spectPort" mode = $Mode } $cfgObj | ConvertTo-Json -Depth 3 | Set-Content $cfgPath -Encoding utf8 Write-OK "server_config.json updated (mode=$Mode orch=:$orchPort seq=:$seqPort spectro=:$spectPort)" } # -- 8. Launch GUI ------------------------------------------------------------- if (-not $ServicesOnly) { Write-Step "Launching GUI" if (-not (Test-Path $VenvPython)) { Write-Warn "Venv not found -- falling back to system Python" $VenvPython = "python" } if (-not (Test-Path $AppScript)) { Write-Fail "GUI entry point not found: $AppScript" } Start-Process $VenvPython -ArgumentList "`"$AppScript`"" -WorkingDirectory $Root -WindowStyle Normal Write-OK "GUI launched" } # -- Summary ------------------------------------------------------------------- Write-Host "" Write-Host "============================================================" -ForegroundColor Green Write-Host (" LF-MRI platform is running [{0} mode]" -f $Mode.ToUpper()) -ForegroundColor Green Write-Host "============================================================" -ForegroundColor Green Write-Host "" if (-not $SkipSpectrometer) { Write-Host " Stop spectrometer: close the spectrometer terminal window" Write-Host " Stop pico-tcp: services\spectrometer\autokill.bat" } Write-Host " Stop all: .\stop.ps1" Write-Host " Update code: .\update.bat" Write-Host "" Write-Host " Press Enter to close this window..." -ForegroundColor DarkGray $null = Read-Host