Neuer optionaler Schritt 9: RouterOS-Update prüfen und starten
RouterOsUpdateService nutzt RouterOS' eigene Update-Funktion (/system/package/update/check-for-updates + /print), inklusive Polling bis der Status nicht mehr "Checking..." ist. Schlägt die Prüfung fehl (z. B. weil das Gerät noch keinen Internetzugang hat), wird das als "nicht geprüft werden konnte" behandelt statt als Fehler. Zeitlicher Ablauf wie gewünscht: Die Prüfung wird zuerst im Hintergrund in der Geräte-Erkennung versucht (Schritt 2) — funktioniert nur, wenn das Gerät zu diesem Zeitpunkt schon Internetzugang hat. Nach erfolgreichem Anwenden (Schritt 8, wenn WAN eingerichtet wurde) wird sie automatisch wiederholt. Beide Läufe blockieren die Navigation nicht (Fire-and-forget im Hintergrund). Neuer Schritt 9 "RouterOS-Update" zeigt das Ergebnis: nicht geprüft werden konnte (mit "Erneut prüfen"), bereits aktuell, oder eine neuere Version verfügbar — dann mit deutlicher Warnung vor dem automatischen Neustart und einer Pflicht-Checkbox, bevor "Jetzt aktualisieren" aktiv wird. Komplett optional, keine Pflicht zum Abschluss des Assistenten. Zusammenfassungs-Schritt lässt "Weiter" jetzt erst nach erfolgreichem Anwenden zu (vorher automatisch gesperrt, weil er der letzte Schritt war — das ist er jetzt nicht mehr). Simulator-Zugang liefert jetzt eine simulierte verfügbare Version (7.15.2 → 7.16.1), damit sich auch dieser Schritt gefahrlos durchspielen lässt. Per UI-Automation den kompletten Assistenten durchgeklickt: "nicht geprüft werden konnte"-Zustand (echtes Simulator-Verhalten ohne Update-Endpunkte) und "Update verfügbar"-Zustand beide verifiziert. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e403d60f3
commit
f12e46dceb
13 files changed
+419
No files matched your search
@@ -47,6 +47,9 @@
|
||||
<DataTemplate DataType="{x:Type steps:SummaryStepViewModel}">
|
||||
<stepViews:SummaryStepView />
|
||||
</DataTemplate>
|
||||
<DataTemplate DataType="{x:Type steps:UpdateStepViewModel}">
|
||||
<stepViews:UpdateStepView />
|
||||
</DataTemplate>
|
||||
</ResourceDictionary>
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -24,6 +24,7 @@ public partial class MainViewModel : ObservableObject
|
||||
new WlanStepViewModel(_session),
|
||||
new FirewallStepViewModel(_session),
|
||||
new SummaryStepViewModel(_session),
|
||||
new UpdateStepViewModel(_session),
|
||||
];
|
||||
|
||||
foreach (var step in Steps)
|
||||
|
||||
@@ -2,6 +2,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
using MikrotikInstaller.Core.Devices;
|
||||
using MikrotikInstaller.Core.Updates;
|
||||
|
||||
namespace MikrotikInstaller.App.ViewModels.Steps;
|
||||
|
||||
@@ -59,6 +60,11 @@ public partial class DeviceOverviewStepViewModel : WizardStepViewModelBase
|
||||
var device = await DeviceDiscoveryService.DiscoverAsync(_session.Client);
|
||||
Device = device;
|
||||
_session.Device = device;
|
||||
|
||||
// Läuft im Hintergrund weiter, ohne die Navigation zu blockieren — ein frisches Gerät
|
||||
// hat an dieser Stelle im Assistenten meist noch keinen Internetzugang, die Prüfung
|
||||
// schlägt dann einfach fehl (kein Fehlerzustand) und wird nach dem Anwenden wiederholt.
|
||||
_ = CheckForUpdatesInBackgroundAsync(_session.Client);
|
||||
}
|
||||
catch (RouterOsException ex)
|
||||
{
|
||||
@@ -70,4 +76,9 @@ public partial class DeviceOverviewStepViewModel : WizardStepViewModelBase
|
||||
IsLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CheckForUpdatesInBackgroundAsync(IRouterOsClient client)
|
||||
{
|
||||
_session.UpdateInfo = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using MikrotikInstaller.Core.Backup;
|
||||
using MikrotikInstaller.Core.Configuration;
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
using MikrotikInstaller.Core.Export;
|
||||
using MikrotikInstaller.Core.Updates;
|
||||
using Wpf.Ui.Controls;
|
||||
|
||||
namespace MikrotikInstaller.App.ViewModels.Steps;
|
||||
@@ -38,8 +39,11 @@ public partial class SummaryStepViewModel : WizardStepViewModelBase
|
||||
private string? _errorMessage;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(CanGoNext))]
|
||||
private bool _isFinished;
|
||||
|
||||
public override bool CanGoNext => IsFinished;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _backupName;
|
||||
|
||||
@@ -114,6 +118,10 @@ public partial class SummaryStepViewModel : WizardStepViewModelBase
|
||||
|
||||
ProgressText = null;
|
||||
IsFinished = true;
|
||||
|
||||
// Jetzt sollte Internet stehen (falls WAN eingerichtet wurde) — Update-Prüfung für den
|
||||
// folgenden Schritt im Hintergrund wiederholen, blockiert den Abschluss hier nicht.
|
||||
_ = RecheckUpdatesInBackgroundAsync(_session.Client);
|
||||
}
|
||||
catch (RouterOsException ex)
|
||||
{
|
||||
@@ -169,6 +177,11 @@ public partial class SummaryStepViewModel : WizardStepViewModelBase
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RecheckUpdatesInBackgroundAsync(IRouterOsClient client)
|
||||
{
|
||||
_session.UpdateInfo = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
}
|
||||
|
||||
private IReadOnlyList<ConfigurationAction> GetAllActions() =>
|
||||
(_session.WanActions ?? [])
|
||||
.Concat(_session.LanActions ?? [])
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
using MikrotikInstaller.Core.Updates;
|
||||
|
||||
namespace MikrotikInstaller.App.ViewModels.Steps;
|
||||
|
||||
/// <summary>
|
||||
/// Letzter, optionaler Schritt: zeigt, ob eine neuere RouterOS-Version verfügbar ist, und kann sie
|
||||
/// auf Wunsch installieren. Komplett optional — der Assistent gilt bereits mit dem vorherigen Schritt
|
||||
/// als abgeschlossen.
|
||||
/// </summary>
|
||||
public partial class UpdateStepViewModel : WizardStepViewModelBase
|
||||
{
|
||||
private readonly WizardSession _session;
|
||||
|
||||
public UpdateStepViewModel(WizardSession session)
|
||||
: base(
|
||||
title: "RouterOS-Update",
|
||||
description: "Falls eine neuere RouterOS-Version verfügbar ist, kannst du sie hier optional installieren. Das ist kein Muss — dein Gerät ist bereits fertig eingerichtet.")
|
||||
{
|
||||
_session = session;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(ShowCheckFailed))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpToDate))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpdateAvailable))]
|
||||
private bool _isChecking;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(ShowCheckFailed))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpToDate))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpdateAvailable))]
|
||||
private bool _isCheckSuccessful;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpToDate))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpdateAvailable))]
|
||||
private bool _isUpdateAvailable;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _installedVersion;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _latestVersion;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _confirmReboot;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isUpdating;
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpToDate))]
|
||||
[NotifyPropertyChangedFor(nameof(ShowUpdateAvailable))]
|
||||
private bool _isUpdateStarted;
|
||||
|
||||
[ObservableProperty]
|
||||
private string? _errorMessage;
|
||||
|
||||
public bool ShowCheckFailed => !IsChecking && !IsCheckSuccessful;
|
||||
|
||||
public bool ShowUpToDate => !IsChecking && IsCheckSuccessful && !IsUpdateAvailable && !IsUpdateStarted;
|
||||
|
||||
public bool ShowUpdateAvailable => !IsChecking && IsCheckSuccessful && IsUpdateAvailable && !IsUpdateStarted;
|
||||
|
||||
public override Task OnActivatedAsync()
|
||||
{
|
||||
ApplyInfo(_session.UpdateInfo);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void ApplyInfo(RouterOsUpdateInfo? info)
|
||||
{
|
||||
IsCheckSuccessful = info?.IsCheckSuccessful ?? false;
|
||||
IsUpdateAvailable = info?.IsUpdateAvailable ?? false;
|
||||
InstalledVersion = string.IsNullOrWhiteSpace(info?.InstalledVersion) ? null : info.InstalledVersion;
|
||||
LatestVersion = info?.LatestVersion;
|
||||
StartUpdateCommand.NotifyCanExecuteChanged();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task RecheckAsync()
|
||||
{
|
||||
if (_session.Client is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsChecking = true;
|
||||
ErrorMessage = null;
|
||||
|
||||
try
|
||||
{
|
||||
var info = await RouterOsUpdateService.CheckForUpdatesAsync(_session.Client);
|
||||
_session.UpdateInfo = info;
|
||||
ApplyInfo(info);
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsChecking = false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanStartUpdate => IsUpdateAvailable && ConfirmReboot && !IsUpdating && !IsUpdateStarted;
|
||||
|
||||
partial void OnConfirmRebootChanged(bool value) => StartUpdateCommand.NotifyCanExecuteChanged();
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanStartUpdate))]
|
||||
private async Task StartUpdateAsync()
|
||||
{
|
||||
if (_session.Client is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
IsUpdating = true;
|
||||
ErrorMessage = null;
|
||||
|
||||
try
|
||||
{
|
||||
await RouterOsUpdateService.StartUpdateAsync(_session.Client);
|
||||
IsUpdateStarted = true;
|
||||
}
|
||||
catch (RouterOsException ex)
|
||||
{
|
||||
ErrorMessage = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsUpdating = false;
|
||||
StartUpdateCommand.NotifyCanExecuteChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using MikrotikInstaller.Core.Configuration;
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
using MikrotikInstaller.Core.Devices;
|
||||
using MikrotikInstaller.Core.Updates;
|
||||
|
||||
namespace MikrotikInstaller.App.ViewModels;
|
||||
|
||||
@@ -44,6 +45,12 @@ public sealed class WizardSession
|
||||
|
||||
public FirewallSettings? FirewallSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ergebnis der letzten Update-Prüfung. Wird zuerst in der Geräte-Erkennung versucht (funktioniert
|
||||
/// nur, wenn das Gerät schon Internetzugang hat) und nach erfolgreichem Anwenden erneut versucht.
|
||||
/// </summary>
|
||||
public RouterOsUpdateInfo? UpdateInfo { get; set; }
|
||||
|
||||
public async Task DisposeClientAsync()
|
||||
{
|
||||
if (Client is not null)
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
<UserControl x:Class="MikrotikInstaller.App.Views.Steps.UpdateStepView"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d"
|
||||
d:DesignWidth="600">
|
||||
<StackPanel Margin="0,16,0,0">
|
||||
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" Margin="0,0,0,20" Opacity="0.8" />
|
||||
|
||||
<StackPanel Orientation="Horizontal" Visibility="{Binding IsChecking, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<ui:ProgressRing IsIndeterminate="True" Width="20" Height="20" Margin="0,0,12,0" />
|
||||
<TextBlock Text="Es wird geprüft, ob ein Update verfügbar ist …" VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Visibility="{Binding ShowCheckFailed, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<TextBlock TextWrapping="Wrap" Opacity="0.8" Margin="0,0,0,12">
|
||||
Es konnte nicht geprüft werden, ob ein Update verfügbar ist. Das liegt meist daran, dass dein Gerät (noch) keinen Internetzugang hat.
|
||||
</TextBlock>
|
||||
<ui:Button Content="Erneut prüfen" Appearance="Secondary" HorizontalAlignment="Left"
|
||||
Command="{Binding RecheckCommand}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Visibility="{Binding ShowUpToDate, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<TextBlock TextWrapping="Wrap" FontWeight="SemiBold" Foreground="{StaticResource MikrotikAccentBrush}">
|
||||
<Run Text="Dein Gerät ist bereits auf dem neuesten Stand (Version" />
|
||||
<Run Text="{Binding InstalledVersion, Mode=OneWay}" />
|
||||
<Run Text=")." />
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Visibility="{Binding ShowUpdateAvailable, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<TextBlock TextWrapping="Wrap" Margin="0,0,0,16">
|
||||
<Run Text="Eine neuere Version ist verfügbar: " FontWeight="SemiBold" />
|
||||
<Run Text="{Binding InstalledVersion, Mode=OneWay}" />
|
||||
<Run Text=" → " />
|
||||
<Run Text="{Binding LatestVersion, Mode=OneWay}" FontWeight="SemiBold" Foreground="{StaticResource MikrotikAccentBrush}" />
|
||||
</TextBlock>
|
||||
|
||||
<TextBlock TextWrapping="Wrap" Margin="0,0,0,16" Opacity="0.85">
|
||||
Beim Update startet dein Gerät automatisch neu. Das dauert normalerweise ein bis zwei Minuten, in denen dein Netzwerk kurz nicht erreichbar ist. In seltenen Fällen (z. B. Stromausfall während des Updates) kann ein Update fehlschlagen.
|
||||
</TextBlock>
|
||||
|
||||
<CheckBox Content="Ich bin mir bewusst, dass mein Gerät dabei neu startet" IsChecked="{Binding ConfirmReboot}" Margin="0,0,0,16" />
|
||||
|
||||
<ui:Button Content="Jetzt aktualisieren" Appearance="Primary" HorizontalAlignment="Left"
|
||||
Command="{Binding StartUpdateCommand}" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Margin="0,16,0,0" Visibility="{Binding IsUpdating, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<ui:ProgressRing IsIndeterminate="True" Width="20" Height="20" Margin="0,0,12,0" />
|
||||
<TextBlock Text="Update wird gestartet …" VerticalAlignment="Center" />
|
||||
</StackPanel>
|
||||
|
||||
<StackPanel Visibility="{Binding IsUpdateStarted, Converter={StaticResource BoolToVisibilityConverter}}">
|
||||
<TextBlock TextWrapping="Wrap" FontWeight="SemiBold" Foreground="{StaticResource MikrotikAccentBrush}" Margin="0,0,0,8">
|
||||
Das Update wurde gestartet.
|
||||
</TextBlock>
|
||||
<TextBlock TextWrapping="Wrap" Opacity="0.85">
|
||||
Dein Gerät lädt die neue Version herunter und startet danach automatisch neu. Das dauert normalerweise ein bis zwei Minuten.
|
||||
</TextBlock>
|
||||
</StackPanel>
|
||||
|
||||
<TextBlock Text="{Binding ErrorMessage}" TextWrapping="Wrap" Foreground="{StaticResource MikrotikRedBrush}" Margin="0,16,0,0"
|
||||
Visibility="{Binding ErrorMessage, Converter={StaticResource StringToVisibilityConverter}}" />
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace MikrotikInstaller.App.Views.Steps;
|
||||
|
||||
public partial class UpdateStepView : UserControl
|
||||
{
|
||||
public UpdateStepView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using MikrotikInstaller.Core.Updates;
|
||||
|
||||
namespace MikrotikInstaller.Core.Tests.Updates;
|
||||
|
||||
public class RouterOsUpdateServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task CheckForUpdatesAsync_UpdateAvailable_ReturnsIsUpdateAvailableTrue()
|
||||
{
|
||||
var responses = new Queue<IReadOnlyList<IReadOnlyDictionary<string, string>>>();
|
||||
responses.Enqueue([new Dictionary<string, string> { ["status"] = "New version is available", ["installed-version"] = "7.10", ["latest-version"] = "7.15.2" }]);
|
||||
var client = new SequencedRouterOsClient(responses);
|
||||
|
||||
var info = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
|
||||
Assert.True(info.IsCheckSuccessful);
|
||||
Assert.True(info.IsUpdateAvailable);
|
||||
Assert.Equal("7.10", info.InstalledVersion);
|
||||
Assert.Equal("7.15.2", info.LatestVersion);
|
||||
Assert.Equal(1, client.ExecuteCallCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckForUpdatesAsync_AlreadyUpToDate_ReturnsIsUpdateAvailableFalse()
|
||||
{
|
||||
var responses = new Queue<IReadOnlyList<IReadOnlyDictionary<string, string>>>();
|
||||
responses.Enqueue([new Dictionary<string, string> { ["status"] = "System is already up to date", ["installed-version"] = "7.15.2", ["latest-version"] = "7.15.2" }]);
|
||||
var client = new SequencedRouterOsClient(responses);
|
||||
|
||||
var info = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
|
||||
Assert.True(info.IsCheckSuccessful);
|
||||
Assert.False(info.IsUpdateAvailable);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckForUpdatesAsync_WaitsWhileStatusIsStillChecking()
|
||||
{
|
||||
var responses = new Queue<IReadOnlyList<IReadOnlyDictionary<string, string>>>();
|
||||
responses.Enqueue([new Dictionary<string, string> { ["status"] = "Checking for updates..." }]);
|
||||
responses.Enqueue([new Dictionary<string, string> { ["status"] = "Checking for updates..." }]);
|
||||
responses.Enqueue([new Dictionary<string, string> { ["status"] = "System is already up to date", ["installed-version"] = "7.15.2", ["latest-version"] = "7.15.2" }]);
|
||||
var client = new SequencedRouterOsClient(responses);
|
||||
|
||||
var info = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
|
||||
Assert.True(info.IsCheckSuccessful);
|
||||
Assert.Empty(responses);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CheckForUpdatesAsync_NoInternet_ReturnsCheckFailed()
|
||||
{
|
||||
// Kein Eintrag jemals verfügbar (Gerät kann den Update-Server nicht erreichen).
|
||||
var client = new SequencedRouterOsClient(new Queue<IReadOnlyList<IReadOnlyDictionary<string, string>>>());
|
||||
|
||||
var info = await RouterOsUpdateService.CheckForUpdatesAsync(client);
|
||||
|
||||
Assert.False(info.IsCheckSuccessful);
|
||||
Assert.False(info.IsUpdateAvailable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
|
||||
namespace MikrotikInstaller.Core.Tests.Updates;
|
||||
|
||||
/// <summary>Test-Double, das bei GetAsync nacheinander vorbereitete Antworten liefert (simuliert Polling).</summary>
|
||||
internal sealed class SequencedRouterOsClient(Queue<IReadOnlyList<IReadOnlyDictionary<string, string>>> responses) : IRouterOsClient
|
||||
{
|
||||
public int ExecuteCallCount { get; private set; }
|
||||
|
||||
public RouterOsProtocol Protocol => RouterOsProtocol.Rest;
|
||||
|
||||
public Task<IReadOnlyList<IReadOnlyDictionary<string, string>>> GetAsync(string path, CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult(responses.Count > 0 ? responses.Dequeue() : []);
|
||||
|
||||
public Task<string> AddAsync(string path, IReadOnlyDictionary<string, string> parameters, CancellationToken cancellationToken = default) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public Task SetAsync(string path, string id, IReadOnlyDictionary<string, string> parameters, CancellationToken cancellationToken = default) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public Task RemoveAsync(string path, string id, CancellationToken cancellationToken = default) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
public Task ExecuteAsync(string path, IReadOnlyDictionary<string, string>? parameters = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
ExecuteCallCount++;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
}
|
||||
@@ -91,5 +91,9 @@ public sealed class DemoRouterOsClient : IRouterOsClient
|
||||
new() { [".id"] = "*3", ["name"] = "ether3", ["type"] = "ether", ["running"] = "false", ["disabled"] = "false" },
|
||||
new() { [".id"] = "*4", ["name"] = "wlan1", ["type"] = "wlan", ["running"] = "false", ["disabled"] = "true" },
|
||||
],
|
||||
["/system/package/update"] =
|
||||
[
|
||||
new() { ["installed-version"] = "7.15.2", ["latest-version"] = "7.16.1", ["status"] = "New version is available" },
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace MikrotikInstaller.Core.Updates;
|
||||
|
||||
/// <summary>
|
||||
/// Ergebnis einer Update-Prüfung. <see cref="IsCheckSuccessful"/> ist <c>false</c>, wenn das Gerät
|
||||
/// den MikroTik-Update-Server nicht erreichen konnte (z. B. weil noch kein Internetzugang eingerichtet
|
||||
/// ist) — das ist beim allerersten Einrichtungsschritt der Normalfall, kein Fehler.
|
||||
/// </summary>
|
||||
public sealed record RouterOsUpdateInfo(
|
||||
bool IsCheckSuccessful,
|
||||
string InstalledVersion,
|
||||
string? LatestVersion,
|
||||
bool IsUpdateAvailable)
|
||||
{
|
||||
public static RouterOsUpdateInfo CheckFailed() => new(false, string.Empty, null, false);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using MikrotikInstaller.Core.Connectivity;
|
||||
|
||||
namespace MikrotikInstaller.Core.Updates;
|
||||
|
||||
/// <summary>
|
||||
/// Prüft über die RouterOS-eigene Update-Funktion, ob eine neuere RouterOS-Version verfügbar ist,
|
||||
/// und kann eine gefundene Aktualisierung anstoßen. Das Gerät braucht dafür funktionierenden
|
||||
/// Internetzugang — schlägt die Prüfung fehl, wird das als "nicht geprüft werden konnte" behandelt,
|
||||
/// nicht als Fehler.
|
||||
/// </summary>
|
||||
public static class RouterOsUpdateService
|
||||
{
|
||||
private const int PollAttempts = 12;
|
||||
private static readonly TimeSpan PollDelay = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
public static async Task<RouterOsUpdateInfo> CheckForUpdatesAsync(IRouterOsClient client, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await client.ExecuteAsync("/system/package/update/check-for-updates", cancellationToken: cancellationToken);
|
||||
|
||||
for (var attempt = 0; attempt < PollAttempts; attempt++)
|
||||
{
|
||||
await Task.Delay(PollDelay, cancellationToken);
|
||||
|
||||
var rows = await client.GetAsync("/system/package/update", cancellationToken);
|
||||
var row = rows.FirstOrDefault();
|
||||
if (row is null)
|
||||
{
|
||||
return RouterOsUpdateInfo.CheckFailed();
|
||||
}
|
||||
|
||||
var status = row.GetValueOrDefault("status", string.Empty);
|
||||
if (status.Contains("checking", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var installed = row.GetValueOrDefault("installed-version", string.Empty);
|
||||
var latest = row.GetValueOrDefault("latest-version");
|
||||
var isAvailable = !string.IsNullOrWhiteSpace(latest)
|
||||
&& !string.Equals(latest, installed, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
return new RouterOsUpdateInfo(true, installed, latest, isAvailable);
|
||||
}
|
||||
|
||||
return RouterOsUpdateInfo.CheckFailed();
|
||||
}
|
||||
catch (RouterOsException)
|
||||
{
|
||||
return RouterOsUpdateInfo.CheckFailed();
|
||||
}
|
||||
}
|
||||
|
||||
public static Task StartUpdateAsync(IRouterOsClient client, CancellationToken cancellationToken = default) =>
|
||||
client.ExecuteAsync("/system/package/update/install", cancellationToken: cancellationToken);
|
||||
}
|
||||
Reference in new issue
Block a user