Repository navigation
Expand file tree
/
Copy pathplugin_api_powershell.ps1
More file actions
100 lines (96 loc) · 3.36 KB
/
Copy pathplugin_api_powershell.ps1
File metadata and controls
100 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
param(
[string]$BaseUrl = '',
[string]$DiscoveryFile = $env:CAMREMOTE_DISCOVERY_FILE,
[string]$CameraId = 'active',
[string]$Token = $env:CAMREMOTE_TOKEN,
[ValidateSet('Cameras', 'Discover', 'Connect', 'Reconnect', 'Properties', 'Frame', 'Capture')]
[string]$Command = 'Cameras',
[int]$Index = -1,
[string]$Ip = '',
[string]$User = '',
[string]$Password = '',
[string]$Transport = '',
[string]$Output = '.\frame.jpg'
)
function Test-CamRemoteService($Service) {
if (-not $Service.ApiBaseUrl -or -not $Service.HealthUrl) {
return $null
}
try {
$identity = Invoke-RestMethod $Service.HealthUrl -TimeoutSec 2
if ($identity.name -eq 'Sony Camera Control Plugin API' -and
"$($identity.apiVersion)".Split('.')[0] -eq '1') {
return $Service.ApiBaseUrl
}
} catch {}
return $null
}
if (-not $BaseUrl) {
if (-not $DiscoveryFile) {
$DiscoveryFile = Join-Path $env:LOCALAPPDATA 'SonyCameraControl\service.json'
}
if (Test-Path -LiteralPath $DiscoveryFile) {
try {
$service = Get-Content -LiteralPath $DiscoveryFile -Raw | ConvertFrom-Json
$BaseUrl = Test-CamRemoteService $service
} catch {}
}
if (-not $BaseUrl) {
$service = Get-ItemProperty `
'HKCU:\Software\SonyCameraControl\Service' -ErrorAction SilentlyContinue
$BaseUrl = Test-CamRemoteService $service
}
if (-not $BaseUrl) {
$BaseUrl = 'http://127.0.0.1:8080/api/v1'
}
}
$headers = @{}
if ($Token) {
$headers.Authorization = "Bearer $Token"
}
$camera = [Uri]::EscapeDataString($CameraId)
switch ($Command) {
'Cameras' {
Invoke-RestMethod "$BaseUrl/cameras" -Headers $headers
}
'Discover' {
$body = @{ timeoutSeconds = 2; preferCache = $true } | ConvertTo-Json
$result = Invoke-RestMethod "$BaseUrl/discovery" -Method Post `
-Headers $headers -ContentType 'application/json' -Body $body
while (-not $result.discovery.complete) {
Start-Sleep -Milliseconds 250
$result = Invoke-RestMethod "$BaseUrl$($result.statusUrl)" -Headers $headers
}
$result
}
'Connect' {
$request = @{ user = $User; password = $Password; transport = $Transport }
if ($Ip) {
$request.ip = $Ip
} elseif ($Index -ge 0) {
$request.index = $Index
} else {
throw 'Connect requires -Index or -Ip.'
}
$body = $request | ConvertTo-Json
Invoke-RestMethod "$BaseUrl/cameras/$camera/actions/connect" `
-Method Post -Headers $headers -ContentType 'application/json' -Body $body
}
'Reconnect' {
Invoke-RestMethod "$BaseUrl/cameras/$camera/actions/reconnect" `
-Method Post -Headers $headers -ContentType 'application/json' -Body '{}'
}
'Properties' {
Invoke-RestMethod "$BaseUrl/cameras/$camera/properties" -Headers $headers
}
'Frame' {
Invoke-WebRequest "$BaseUrl/cameras/$camera/live-view/frame" `
-Headers $headers -OutFile $Output
Write-Output "Saved $Output"
}
'Capture' {
$body = @{ waitMs = 15000 } | ConvertTo-Json
Invoke-RestMethod "$BaseUrl/cameras/$camera/actions/capture" `
-Method Post -Headers $headers -ContentType 'application/json' -Body $body
}
}