feat: improve WebUntis sync and competency insights
CI / build-and-test (push) Canceled after 0s

This commit is contained in:
admin committed 2026-09-20 22:17:29 +02:00
1 parent cdb4637c65
commit f0c0479bcb
22 files changed
+1099 -76

No files matched your search

+72 -2
View File
@@ -40,6 +40,8 @@ public class UntisSyncService : IDisposable
private readonly PeriodScheduleService _periodSchedule;
private readonly UntisMatchingService _matchingService;
private readonly UntisDiffService _diffService;
private readonly WebUntisIntegrationService? _restIntegration;
private readonly WebUntisRestSyncProcessor? _restProcessor;
private readonly AppLogger? _logger;
private readonly SemaphoreSlim _gate = new(1, 1);
private readonly Timer _timer;
@@ -53,14 +55,15 @@ public class UntisSyncService : IDisposable
ITimetableSlotRepository timetableSlots, ISupervisionDutyRepository supervisionDuties,
ISchoolHolidayRepository schoolHolidays, PublicHolidayService publicHolidays,
SchoolCalendarSettingsService calendarSettings, PeriodScheduleService periodSchedule,
UntisMatchingService matchingService, UntisDiffService diffService, AppLogger? logger = null)
UntisMatchingService matchingService, UntisDiffService diffService, AppLogger? logger = null,
WebUntisIntegrationService? restIntegration = null, WebUntisRestSyncProcessor? restProcessor = null)
{
_http = http; _settings = settings; _snapshots = snapshots; _mappings = mappings;
_substitutions = substitutions; _groups = groups; _timetableSlots = timetableSlots;
_supervisionDuties = supervisionDuties;
_schoolHolidays = schoolHolidays; _publicHolidays = publicHolidays; _calendarSettings = calendarSettings;
_periodSchedule = periodSchedule; _matchingService = matchingService; _diffService = diffService;
_logger = logger;
_logger = logger; _restIntegration = restIntegration; _restProcessor = restProcessor;
_timer = new Timer(async _ => await PollAsync(), null,
TimeSpan.FromMinutes(PollIntervalMinutes), TimeSpan.FromMinutes(PollIntervalMinutes));
}
@@ -70,6 +73,12 @@ public class UntisSyncService : IDisposable
if (!await _gate.WaitAsync(0).ConfigureAwait(false)) return;
try
{
if (_settings.ApiIsConfigured && _restIntegration is not null && _restProcessor is not null)
{
await PollRestAsync(cancellationToken).ConfigureAwait(false);
return;
}
var url = _settings.GetIcalUrl();
if (string.IsNullOrEmpty(url)) return;
@@ -107,6 +116,67 @@ public class UntisSyncService : IDisposable
finally { _gate.Release(); }
}
private async Task PollRestAsync(CancellationToken cancellationToken)
{
var restIntegration = _restIntegration
?? throw new InvalidOperationException("WebUntis-REST-Integration fehlt.");
var restProcessor = _restProcessor
?? throw new InvalidOperationException("WebUntis-REST-Verarbeitung fehlt.");
var today = DateOnly.FromDateTime(DateTime.Today);
var daysSinceMonday = ((int)today.DayOfWeek + 6) % 7;
var firstMonday = today.DayOfWeek switch
{
DayOfWeek.Saturday => today.AddDays(2),
DayOfWeek.Sunday => today.AddDays(1),
_ => today.AddDays(-daysSinceMonday),
};
var entries = new List<UntisTimetableGridEntryDto>();
try
{
var years = await restIntegration.GetSchoolYearsAsync(cancellationToken).ConfigureAwait(false);
// WebUntis selbst lädt das Grid wochenweise. Vier kleine Wochenabrufe halten dieselbe
// Semantik ein und teilen sich innerhalb der Sitzung denselben kurzlebigen Bearer.
for (var week = 0; week < 4; week++)
{
var monday = firstMonday.AddDays(week * 7);
var numericDate = monday.Year * 10000 + monday.Month * 100 + monday.Day;
var schoolYear = years.FirstOrDefault(year =>
year.StartDate <= numericDate && year.EndDate >= numericDate)
?? throw new WebUntisIntegrationException(
$"WebUntis hat für die Woche ab {monday:dd.MM.yyyy} kein Schuljahr geliefert.");
entries.AddRange(await restIntegration.GetOwnTimetableGridAsync(schoolYear.UntisId,
monday, monday.AddDays(4),
cancellationToken).ConfigureAwait(false));
}
}
catch (Exception ex)
{
// REST ist bei konfigurierten API-Zugangsdaten die autoritative Quelle. Kein stiller
// iCal-Fallback: zwei konkurrierende Abgleiche würden wieder doppelte/widersprüchliche
// Ausnahmen erzeugen. Der letzte erfolgreiche lokale Stand bleibt stattdessen erhalten.
_logger?.Error("WebUntis-REST-Abgleich: Abruf fehlgeschlagen", ex);
_settings.SetLastSync(DateTime.UtcNow, $"REST-Fehler beim Abruf: {ex.Message}");
return;
}
try
{
var result = restProcessor.Process(entries, today, firstMonday.AddDays(13), firstMonday.AddDays(27));
var totalChanges = result.SubstitutionChanges + result.ExamChanges;
_settings.SetLastSync(DateTime.UtcNow,
$"{result.SubstitutionChanges} Stundenplanänderung(en), {result.ExamChanges} Arbeit(en) geändert " +
$"({result.EntryCount} REST-Termine geprüft)." );
_logger?.Info($"WebUntis-REST-Abgleich: {result.SubstitutionChanges} Stundenplanänderung(en), " +
$"{result.ExamChanges} Arbeit(en) aus {result.EntryCount} Grid-Einträgen.");
if (totalChanges > 0) DataChanged?.Invoke();
}
catch (Exception ex)
{
_logger?.Error("WebUntis-REST-Abgleich: Verarbeitung fehlgeschlagen", ex);
_settings.SetLastSync(DateTime.UtcNow, $"REST-Fehler bei der Verarbeitung: {ex.Message}");
}
}
// Reiner Kern ohne HTTP - direkt mit vorgefertigtem ICS-Text testbar (kein InternalsVisibleTo
// in dieser Codebasis üblich, siehe CLAUDE.md - deshalb public statt internal).
public UntisPollResult ProcessIcsText(string icsText)