forked from torizon/vscode-torizon-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnectDevice.ps1
137 lines (115 loc) · 3.76 KB
/
connectDevice.ps1
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env pwsh
param(
[Parameter(Mandatory=$true)]
[int] $id,
[Parameter(Mandatory=$true)]
[string] $login,
[Parameter(Mandatory=$true)]
[string] $pass
)
$errorActionPreference = "Stop"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', "Internal PS variable"
)]
$PSNativeCommandUseErrorActionPreference = $true
# include
. "$env:HOME/.apollox/scripts/utils/animations.ps1"
. "$env:HOME/.apollox/scripts/utils/stackTrace.ps1"
. "$env:HOME/.apollox/scripts/utils/network.ps1"
# present it to the user with the index identifier
Write-Host "📡 :: CONNECTING DEVICE :: 📡"
Write-Host ""
# connect to the device
try {
$_connectDevs = New-Object System.Collections.ArrayList
# first check if the device is already connected
if (Test-Path $env:HOME/.tcd/connected.json) {
$_jsonArray = Get-Content $env:HOME/.tcd/connected.json `
| ConvertFrom-Json -Depth 100
foreach ($_dev in $_jsonArray) {
$_connectDevs.Add($_dev) | Out-Null
}
}
# get the scan from the file
if (Test-Path $env:HOME/.tcd/scan.json) {
$_nets = Get-Content $env:HOME/.tcd/scan.json `
| ConvertFrom-Json -Depth 100
} else {
$_nets = @()
}
if ($_nets.Count -eq 0) {
Write-Host -ForegroundColor DarkYellow `
"`t ⚠️ :: No network devices found :: ⚠️"
Write-Host ""
Write-Host "Did you ran the scan command?"
Write-Host ""
exit 404
}
if ($id -lt 0 -or $id -ge $_nets.Count) {
Write-Host -ForegroundColor Red `
"`t ❌ :: Invalid device id :: ❌"
Write-Host ""
exit 400
}
$_net = $_nets[$id]
if ($_connectDevs.Count -gt 0) {
# check if the device is already connected
foreach ($_dev in $_connectDevs) {
if ($_dev.Ip -eq $_net.Ip) {
$_hostName = $_net.Hostname
Write-Host -ForegroundColor DarkYellow `
"`t ⚠️ :: Device $_hostname already connected :: ⚠️"
Write-Host ""
exit 0
}
}
}
$_hostName = $_net.Hostname
Write-Host "`t Trying to connect to $_hostName"
Write-Host ""
$_dev = RunCommandInBackgrounWithWaitAnimation {
param(
[Parameter(Mandatory=$true)]
[int] $id,
[Parameter(Mandatory=$true)]
[string] $login,
[Parameter(Mandatory=$true)]
[string] $pass,
[Parameter(Mandatory=$true)]
[string] $hostIp
)
node $env:HOME/.apollox/scripts/node/connectNetworkDevice.mjs `
$id `
$login `
$pass `
$hostIp
} @($id, $login, $pass, $(GetHostIp)) `
| ConvertFrom-Json -Depth 100
# add the __pass__ member to the object
$_dev | Add-Member -MemberType NoteProperty `
-Name "__pass__" -Value $pass
$_connectDevs.Add($_dev) | Out-Null
$_connectDevs | ConvertTo-Json -AsArray -Depth 100 `
| Out-File $env:HOME/.tcd/connected.json
$_hostName = $_dev.Hostname
$_ip = $_dev.Ip
$_tcVersion = $_dev.Version
$_model = $_dev.Model
$_arch = $_dev.Arch
Write-Host -ForegroundColor Green `
"`t ✅ :: Connected to device :: ✅"
Write-Host ""
Write-Host "`t Hostname: $_hostName"
Write-Host "`t IP Address: $_ip"
Write-Host "`t TorizonCore Version: $_tcVersion"
Write-Host "`t Model: $_model"
Write-Host "`t Architecture: $_arch"
Write-Host ""
} catch {
Write-Host -ForegroundColor Red `
"`t ❌ :: Could not connect to device :: ❌"
Write-Host ""
ShowStackTrace $_
exit 500
}
Write-Host ""