|
|
@@ -189,7 +189,8 @@ if (-not $GuiOnly) {
|
|
|
} catch {}
|
|
|
|
|
|
if ($alreadyUp) {
|
|
|
- Write-OK "Spectrometer already responding on port $specPort"
|
|
|
+ Write-OK "Spectrometer already responding on port $specPort -- checking devices..."
|
|
|
+ # Fall through to device registration check below
|
|
|
} else {
|
|
|
# Title makes the window easy to find in the taskbar.
|
|
|
# /k keeps the window open even if Django crashes at startup.
|
|
|
@@ -201,6 +202,59 @@ if (-not $GuiOnly) {
|
|
|
-WindowStyle Normal
|
|
|
Write-OK "Spectrometer started (window: 'LF-MRI Spectrometer', port $specPort)"
|
|
|
}
|
|
|
+
|
|
|
+ # -- Auto-register hardware devices if DB is empty --------------------
|
|
|
+ Write-Host " Waiting for spectrometer API..." -ForegroundColor DarkGray
|
|
|
+ $apiReady = $false
|
|
|
+ $apiWait = 0
|
|
|
+ while ($apiWait -lt 20) {
|
|
|
+ if (Test-ServiceUp @{ Url = "http://localhost:$specPort/api/" }) {
|
|
|
+ $apiReady = $true; break
|
|
|
+ }
|
|
|
+ Start-Sleep 1; $apiWait++
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($apiReady) {
|
|
|
+ try {
|
|
|
+ $devResp = Invoke-WebRequest "http://localhost:$specPort/api/devices/" `
|
|
|
+ -UseBasicParsing -TimeoutSec 5 -ErrorAction Stop
|
|
|
+ $devJson = $devResp.Content | ConvertFrom-Json
|
|
|
+ $devCount = if ($devJson.PSObject.Properties['results']) {
|
|
|
+ $devJson.results.Count
|
|
|
+ } else {
|
|
|
+ @($devJson).Count
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($devCount -eq 0) {
|
|
|
+ Write-Warn "No devices in DB -- registering hardware..."
|
|
|
+ $base = "http://localhost:$specPort/api/devices/"
|
|
|
+ $hdr = @{ "Content-Type" = "application/json" }
|
|
|
+ $devs = @(
|
|
|
+ '{"device_type":"ADC", "brend":"Picoscope", "serial_model":"PS4000A", "proto":"adc_default", "proto_interface":"TCP"}',
|
|
|
+ '{"device_type":"SDR", "brend":"HackRF", "serial_model":"HackRF", "proto":"sdr_default", "proto_interface":"USB"}',
|
|
|
+ '{"device_type":"SYNC", "brend":"Arduino", "serial_model":"DuePP", "proto":"sync_default", "proto_interface":"USB"}',
|
|
|
+ '{"device_type":"GRA", "brend":"ITMO", "serial_model":"GRU", "proto":"gra_default", "proto_interface":"UDP"}'
|
|
|
+ )
|
|
|
+ foreach ($body in $devs) {
|
|
|
+ try {
|
|
|
+ Invoke-WebRequest $base -Method Post -Headers $hdr `
|
|
|
+ -Body $body -UseBasicParsing -TimeoutSec 5 | Out-Null
|
|
|
+ $name = ($body | Select-String '"serial_model":"([^"]+)"').Matches[0].Groups[1].Value
|
|
|
+ Write-Host " Registered: $name" -ForegroundColor DarkGray
|
|
|
+ } catch {
|
|
|
+ Write-Warn " Failed to register device: $_"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Write-OK "Hardware devices registered"
|
|
|
+ } else {
|
|
|
+ Write-OK "Devices already in DB ($devCount found)"
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ Write-Warn "Could not check/register devices: $_"
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Write-Warn "Spectrometer API not ready in 20 s -- skipping device registration"
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
# -- 6. Health check -------------------------------------------------------
|