-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientManager.cs
More file actions
121 lines (105 loc) · 3.72 KB
/
Copy pathClientManager.cs
File metadata and controls
121 lines (105 loc) · 3.72 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Renci.SshNet;
using static TechLogManager.Utils;
namespace TechLogManager;
public class ClientManager : IDisposable
{
private readonly HttpClient _httpClient;
private readonly ScpClient _scpClient;
private readonly SshClient _sshClient;
private readonly string _teamNumber;
private bool _disposed;
public ClientManager(string teamNumber)
{
var hostname = GetRioHostname(teamNumber);
_teamNumber = teamNumber;
_sshClient = new SshClient(hostname, "lvuser", "");
_scpClient = new ScpClient(hostname, "lvuser", "");
_httpClient = new HttpClient();
}
public void Dispose()
{
if (_disposed) return;
_httpClient.Dispose();
_sshClient.Dispose();
_scpClient.Dispose();
_disposed = true;
GC.SuppressFinalize(this);
}
public async Task ConnectAsync(bool ssh, bool scp)
{
await Task.Run(() =>
{
if (ssh)
{
_sshClient.Connect();
if (!_sshClient.IsConnected)
throw new Exception($"Failed to connect SSH client to {GetRioHostname(_teamNumber)}");
}
if (scp)
{
_scpClient.Connect();
if (!_scpClient.IsConnected)
throw new Exception($"Failed to connect SCP client to {GetRioHostname(_teamNumber)}");
}
});
}
public async Task<string> RunCommandAsync(string command)
{
return await Task.Run(() =>
{
try
{
if (!_sshClient.IsConnected)
throw new Exception("SSH client is not connected");
SshCommand result = _sshClient.RunCommand(command);
return result.ExitStatus != 0
? throw new Exception($"Command failed with exit code {result.ExitStatus}: {result.Error}")
: result.Result;
}
catch (Exception ex)
{
throw new Exception($"SSH command execution failed: {ex.GetType().Name} {ex.Message}", ex);
}
});
}
public async Task<List<string>> RunCommandsAsync(params string[] commands)
{
var results = new List<string>();
foreach (var command in commands) results.Add(await RunCommandAsync(command));
return results;
}
public async Task<string> DownloadScpAsync(string remotePath, string localPath)
{
return await Task.Run(() =>
{
try
{
if (!_scpClient.IsConnected)
throw new Exception("SCP client is not connected");
_scpClient.Download(remotePath, new FileInfo(localPath));
return $"Downloaded {remotePath} to {localPath}";
}
catch (Exception ex)
{
throw new Exception($"SCP download failed: {ex.Message}", ex);
}
});
}
public async Task<List<string>> DownloadFilesScpAsync(IEnumerable<(string remotePath, string localPath)> files)
{
var results = new List<string>();
foreach (var (remotePath, localPath) in files) results.Add(await DownloadScpAsync(remotePath, localPath));
return results;
}
public async Task DownloadFileHttpAsync(string url, string outputPath)
{
HttpResponseMessage response = await _httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
await using FileStream fileStream = File.Create(outputPath);
await response.Content.CopyToAsync(fileStream);
}
public async Task<HttpResponseMessage> PerformRequest(HttpRequestMessage request)
{
return await _httpClient.SendAsync(request);
}
}