-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathControlSystemSoftware.cs
More file actions
152 lines (128 loc) · 4.67 KB
/
ControlSystemSoftware.cs
File metadata and controls
152 lines (128 loc) · 4.67 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using CSAUSBTool.CrossPlatform.Core;
using ReactiveUI;
namespace CSAUSBTool.CrossPlatform.Models
{
public class ControlSystemSoftware : ReactiveObject
{
[JsonPropertyName("Name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("FileName")]
public string? FileName { get; set; }
[JsonPropertyName("Description")]
public string Description { get; set; } = string.Empty;
[JsonPropertyName("Tags")]
public List<string> Tags { get; set; } = [];
[JsonPropertyName("Uri")]
public string? Uri { get; set; }
[JsonPropertyName("Hash")]
public string? Hash { get; set; }
[JsonPropertyName("Platform")]
public string? Platform { get; set; }
private double _downloadProgress;
public double DownloadProgress
{
get => _downloadProgress;
set => this.RaiseAndSetIfChanged(ref _downloadProgress, value);
}
private bool _isChecked;
public bool IsChecked
{
get => _isChecked;
set => this.RaiseAndSetIfChanged(ref _isChecked, value);
}
private string _statusText = "Pending";
public string StatusText
{
get => _statusText;
set => this.RaiseAndSetIfChanged(ref _statusText, value);
}
private string _displayText = string.Empty;
public string DisplayText
{
get => _displayText;
set => this.RaiseAndSetIfChanged(ref _displayText, value);
}
[JsonIgnore]
public bool IsSelectable => !string.IsNullOrWhiteSpace(Uri);
public void RefreshDisplayText()
{
var tags = Tags.Count > 0 ? $" [{string.Join(", ", Tags)}]" : string.Empty;
DisplayText = IsSelectable ? $"{Name}{tags}" : $"{Name}{tags} (no download URI)";
}
public string ResolveFileName()
{
if (!string.IsNullOrWhiteSpace(FileName))
{
return FileName!;
}
if (!string.IsNullOrWhiteSpace(Uri))
{
var parsed = new global::System.Uri(Uri);
var fromUrl = global::System.Uri.UnescapeDataString(Path.GetFileName(parsed.LocalPath));
if (!string.IsNullOrWhiteSpace(fromUrl))
{
return fromUrl;
}
}
return $"{Name}.bin";
}
// Legacy compatibility for older views still calling Download().
public async Task Download(string outputPath, CancellationToken token)
{
if (string.IsNullOrWhiteSpace(Uri))
{
throw new InvalidOperationException("No download URI.");
}
var resolvedName = ResolveFileName();
var outputFile = Path.Combine(outputPath, resolvedName);
using var client = new HttpClientDownloadWithProgress(Uri, outputFile);
client.ProgressChanged += (_, _, p) => DownloadProgress = p ?? 0;
await client.StartDownload(token);
}
public static string CalculateHash(string filePath, string algorithmName)
{
using var stream = File.OpenRead(filePath);
byte[] hash = algorithmName switch
{
"MD5" => MD5.HashData(stream),
"SHA1" => SHA1.HashData(stream),
"SHA256" => SHA256.HashData(stream),
_ => throw new InvalidOperationException($"Invalid hash algorithm: {algorithmName}")
};
return BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant();
}
public static string? GetHashAlgorithmFromLength(string? hash)
{
if (string.IsNullOrWhiteSpace(hash))
{
return null;
}
return hash.Length switch
{
32 => "MD5",
40 => "SHA1",
64 => "SHA256",
_ => null
};
}
}
public class DesignControlSystemSoftware : ControlSystemSoftware
{
public DesignControlSystemSoftware()
{
Name = "FRC Driver Station";
Tags = ["Driver Station", "FRC"];
Description = "The FRC Driver Station is the software used to control your robot during a match.";
Uri = "https://example.com/driverstation.exe";
RefreshDisplayText();
}
}
}