mock-edge-device.ps1 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. [CmdletBinding()]
  2. param(
  3. [ValidateSet('start', 'stop', 'restart', 'status')]
  4. [string]$Action = 'start',
  5. [string]$Listen = '0.0.0.0:18443',
  6. [switch]$MqttFallbackPlain
  7. )
  8. $ErrorActionPreference = 'Stop'
  9. $Root = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
  10. $RunDir = Join-Path $Root '.run\mock-edge-device'
  11. $GoCache = Join-Path $Root '.gocache-mock-edge'
  12. $Exe = Join-Path $RunDir 'mock-edge-device.exe'
  13. $State = Join-Path $RunDir 'process.json'
  14. $Stdout = Join-Path $RunDir 'stdout.log'
  15. $Stderr = Join-Path $RunDir 'stderr.log'
  16. function Read-State {
  17. if (-not (Test-Path -LiteralPath $State)) { return $null }
  18. try { return Get-Content -LiteralPath $State -Raw | ConvertFrom-Json } catch { return $null }
  19. }
  20. function Stop-Mock {
  21. $item = Read-State
  22. if ($null -ne $item) {
  23. $process = Get-Process -Id ([int]$item.pid) -ErrorAction SilentlyContinue
  24. if ($null -ne $process) {
  25. & taskkill.exe /PID ([int]$item.pid) /T /F 2>$null | Out-Null
  26. $process | Wait-Process -Timeout 5 -ErrorAction SilentlyContinue
  27. }
  28. }
  29. if (Test-Path -LiteralPath $State) { Remove-Item -LiteralPath $State -Force }
  30. Write-Host 'Mock edge device stopped.'
  31. }
  32. if ($Action -eq 'stop') { Stop-Mock; exit 0 }
  33. if ($Action -eq 'status') {
  34. $item = Read-State
  35. if ($null -eq $item) { Write-Host 'Mock edge device is not managed.'; exit 0 }
  36. $process = Get-Process -Id ([int]$item.pid) -ErrorAction SilentlyContinue
  37. Write-Host ("PID {0}, listen {1}, {2}" -f $item.pid, $item.listen, $(if ($null -eq $process) { 'stopped' } else { 'running' }))
  38. exit 0
  39. }
  40. if ($Action -eq 'restart') { Stop-Mock }
  41. New-Item -ItemType Directory -Path $RunDir -Force | Out-Null
  42. New-Item -ItemType Directory -Path $GoCache -Force | Out-Null
  43. Push-Location $Root
  44. try {
  45. $env:GOCACHE = $GoCache
  46. & go build -o $Exe .\cmd\mock-edge-device
  47. if ($LASTEXITCODE -ne 0) { throw 'Mock edge device build failed.' }
  48. } finally { Pop-Location }
  49. $arguments = "-listen `"$Listen`" -data-dir `"$RunDir`""
  50. if ($MqttFallbackPlain) { $arguments += ' -mqtt-fallback-plain' }
  51. $process = Start-Process -FilePath $Exe -ArgumentList $arguments -WorkingDirectory $Root -RedirectStandardOutput $Stdout -RedirectStandardError $Stderr -PassThru
  52. [ordered]@{ pid = $process.Id; listen = $Listen; started_at = (Get-Date).ToString('o') } | ConvertTo-Json | Set-Content -LiteralPath $State -Encoding UTF8
  53. Start-Sleep -Milliseconds 500
  54. if ($process.HasExited) { Get-Content -LiteralPath $Stderr -ErrorAction SilentlyContinue; throw 'Mock edge device exited during startup.' }
  55. Write-Host "Mock edge device started (PID $($process.Id))."
  56. Write-Host "Manual IP: use a non-loopback host IP with HTTPS port $($Listen.Split(':')[-1])."
  57. Write-Host "Automatic discovery: UDP port 31001"
  58. Write-Host "Pairing code: 246810"
  59. Write-Host "Logs: $Stdout / $Stderr"