Add auto updater
This commit is contained in:
@@ -9,6 +9,8 @@ namespace PamhagenSysCtrl.Helpers {
|
||||
private readonly string FileName;
|
||||
|
||||
public string PlcPort = "COM1";
|
||||
public string? UpdateUrl = "https://elwig.at/files/pamhagen-sysctrl/?format=json";
|
||||
public bool UpdateAuto = true;
|
||||
|
||||
public Config(string filename) {
|
||||
FileName = filename;
|
||||
@@ -19,6 +21,9 @@ namespace PamhagenSysCtrl.Helpers {
|
||||
try {
|
||||
var config = new ConfigurationBuilder().AddIniFile(FileName).Build();
|
||||
PlcPort = config["plc:port"] ?? "COM1";
|
||||
UpdateUrl = config["update:url"] ?? UpdateUrl;
|
||||
var updateAuto = config["update:auto"];
|
||||
UpdateAuto = updateAuto == null || TrueValues.Contains(updateAuto.ToLower());
|
||||
} catch (Exception exc) {
|
||||
MessageBox.Show($"Die Konfigurationsdatei konnte nicht gelesen werden:\n\n{exc.Message}", "Konfigurationsdatei lesen", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Application.Current.Shutdown();
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PamhagenSysCtrl.Helpers {
|
||||
public sealed record UpdateInstaller(Version Version, Uri Url, long Size);
|
||||
|
||||
public static class UpdateService {
|
||||
|
||||
private static readonly HttpClient HttpClient = new() {
|
||||
Timeout = Timeout.InfiniteTimeSpan,
|
||||
};
|
||||
|
||||
public static async Task<UpdateInstaller> GetLatestInstallerAsync(string feedUrl, CancellationToken cancellationToken = default) {
|
||||
using var response = await HttpClient.GetAsync(feedUrl, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var json = JsonNode.Parse(await response.Content.ReadAsStringAsync(cancellationToken));
|
||||
var latest = json!["data"]!.AsArray()[^1]!;
|
||||
return new(
|
||||
new Version((string)latest["version"]!),
|
||||
new Uri((string)latest["url"]!),
|
||||
(long)latest["size"]!
|
||||
);
|
||||
}
|
||||
|
||||
public static async Task<string> DownloadInstallerAsync(UpdateInstaller installer, string targetDirectory, IProgress<double>? progress = null, CancellationToken cancellationToken = default) {
|
||||
Directory.CreateDirectory(targetDirectory);
|
||||
var fileName = Path.Combine(targetDirectory, $"PamhagenSysCtrl-{installer.Version}.msi");
|
||||
|
||||
try {
|
||||
using var response = await HttpClient.GetAsync(installer.Url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var contentLength = response.Content.Headers.ContentLength;
|
||||
|
||||
await using (var destination = new FileStream(fileName, FileMode.Create)) {
|
||||
await using var source = await response.Content.ReadAsStreamAsync(cancellationToken);
|
||||
var buffer = new byte[81920];
|
||||
long downloaded = 0;
|
||||
int read;
|
||||
while ((read = await source.ReadAsync(buffer, cancellationToken)) != 0) {
|
||||
await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken);
|
||||
downloaded += read;
|
||||
if (contentLength.HasValue) {
|
||||
progress?.Report((double)downloaded / contentLength.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
progress?.Report(1);
|
||||
return fileName;
|
||||
} catch {
|
||||
File.Delete(fileName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static void StartInstaller(string fileName) {
|
||||
var startInfo = new ProcessStartInfo {
|
||||
FileName = "msiexec.exe",
|
||||
UseShellExecute = true,
|
||||
Verb = "runas",
|
||||
};
|
||||
startInfo.ArgumentList.Add("/i");
|
||||
startInfo.ArgumentList.Add(Path.GetFullPath(fileName));
|
||||
|
||||
if (Process.Start(startInfo) == null) {
|
||||
throw new InvalidOperationException("Der Installer konnte nicht gestartet werden.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user