diff --git a/Eu07.BlackBox.Views/BlackBoxStrings.cs b/Eu07.BlackBox.Views/BlackBoxStrings.cs index f92dc95..4ffd5c0 100644 --- a/Eu07.BlackBox.Views/BlackBoxStrings.cs +++ b/Eu07.BlackBox.Views/BlackBoxStrings.cs @@ -181,6 +181,9 @@ public sealed class DefaultBlackBoxStrings : IBlackBoxStrings ["EventHornMissed"] = "W6: NO HORN", ["EventCab"] = "Cab {0} of {1}", ["EventSecurityBraking"] = "BRAKING BY {0}", + ["EventSecurityAlarm"] = "{0} buzzer", + ["EventSecurityAlarmAcknowledged"] = "{0} buzzer, acknowledged after {1:0.0} s", + ["EventSecurityAlarmMissed"] = "{0} buzzer, not acknowledged", ["EventTimetable"] = "Timetable {0}, {1}", ["CabSpeed"] = "Speed", ["CabAiTarget"] = "AI {0:0} km/h", @@ -452,6 +455,9 @@ public sealed class DefaultBlackBoxStrings : IBlackBoxStrings ["EventHornMissed"] = "W6: BEZ TRĄBIENIA", ["EventCab"] = "Kabina {0} w {1}", ["EventSecurityBraking"] = "HAMOWANIE PRZEZ {0}", + ["EventSecurityAlarm"] = "Brzęczyk {0}", + ["EventSecurityAlarmAcknowledged"] = "Brzęczyk {0}, potwierdzony po {1:0.0} s", + ["EventSecurityAlarmMissed"] = "Brzęczyk {0}, bez potwierdzenia", ["EventTimetable"] = "Rozkład {0}, {1}", ["CabSpeed"] = "Prędkość", ["CabAiTarget"] = "AI {0:0} km/h", diff --git a/Eu07.BlackBox.Views/CabPanel.cs b/Eu07.BlackBox.Views/CabPanel.cs index f896579..48e7615 100644 --- a/Eu07.BlackBox.Views/CabPanel.cs +++ b/Eu07.BlackBox.Views/CabPanel.cs @@ -530,12 +530,12 @@ public sealed class CabPanel : Control if (s.Security != SecurityFlags.None || s.CabEnd != 0) { // A recording which tells them apart. - safety.Add((Strings["DevVigilance"], Security(SecurityFlags.VigilanceBeeping, Palette.LampAlarm) ?? Security(SecurityFlags.Vigilance, Palette.Vigilance))); - safety.Add((Strings["DevCabSignal"], Security(SecurityFlags.CabSignalBeeping, Palette.LampAlarm) ?? Security(SecurityFlags.CabSignal, Palette.CabSignal))); - safety.Add((Strings["DevSecurityBraking"], Security(SecurityFlags.Braking, Palette.LampAlarm))); + safety.Add((Strings["DevVigilance"], s.VigilanceBuzzer ? Palette.Buzzer : Security(SecurityFlags.Vigilance, Palette.Vigilance))); + safety.Add((Strings["DevCabSignal"], s.CabSignalBuzzer ? Palette.Buzzer : Security(SecurityFlags.CabSignal, Palette.CabSignal))); + safety.Add((Strings["DevSecurityBraking"], s.ProtectionBraking ? Palette.LampAlarm : null)); } else - safety.Add((Strings["DevAlerter"], (s.Flags & CabFlags.AlerterBeeping) != 0 ? Palette.LampAlarm + safety.Add((Strings["DevAlerter"], (s.Flags & CabFlags.AlerterBeeping) != 0 ? Palette.Buzzer : (s.Flags & CabFlags.AlerterBlinking) != 0 ? Palette.LampWarning : null)); safety.Add(Flag("DevWheelSlip", CabFlags.WheelSlip, Palette.LampAlarm)); safety.Add(On("DevOverloadRelay", CabDevices.OverloadRelay, Palette.LampAlarm)); diff --git a/Eu07.BlackBox.Views/EventList.cs b/Eu07.BlackBox.Views/EventList.cs index 48f63cc..4bcb854 100644 --- a/Eu07.BlackBox.Views/EventList.cs +++ b/Eu07.BlackBox.Views/EventList.cs @@ -178,14 +178,7 @@ public sealed class EventList : ListBox foreach (var horn in recording.UserHornChecks()) events.Add(new BlackBoxEvent(horn.Request.Time, strings[horn.Sounded ? "EventHornSounded" : "EventHornMissed"], horn.Sounded ? Palette.HornPoint : Palette.Bad, !horn.Sounded)); - foreach (var span in recording.SecurityBrakings()) - { - // Which of them was not acknowledged: the lamps blinking when it braked. - string who = recording.CabAt(span.Start) is { } s - ? s.Has(SecurityFlags.CabSignal) && s.Has(SecurityFlags.Vigilance) ? "CA/SHP" : s.Has(SecurityFlags.CabSignal) ? "SHP" : "CA" - : "CA"; - events.Add(new BlackBoxEvent(span.Start, string.Format(culture, strings["EventSecurityBraking"], who), Palette.Bad, true)); - } + AddSecurity(recording, strings, events, culture); AddCameras(recording, strings, events, culture); AddCouplings(recording, strings, events, culture); AddCabChanges(recording, strings, events, culture); @@ -198,6 +191,35 @@ public sealed class EventList : ListBox return events.OrderBy(e => e.Time).ToList(); } + /// + /// The train protection: its buzzer, acknowledged or not, and the braking apart. A lamp acknowledged before the + /// buzzer is the driving as usual, not told. + /// + private static void AddSecurity(Recording recording, IBlackBoxStrings strings, List events, CultureInfo culture) + { + var brakings = recording.SecurityBrakings(); + foreach (var span in recording.SecurityAlarms()) + { + string who = ProtectionName(recording.CabAt(span.Start)); + string text = brakings.Any(b => b.Start >= span.Start && b.Start <= span.End) + ? string.Format(culture, strings["EventSecurityAlarmMissed"], who) + // Acknowledged when the sample it ended at buzzes no more: not the end of the recording or a gap in it. + : recording.CabAt(span.End) is { } after && !after.VigilanceBuzzer && !after.CabSignalBuzzer + ? string.Format(culture, strings["EventSecurityAlarmAcknowledged"], who, span.End - span.Start) + : string.Format(culture, strings["EventSecurityAlarm"], who); + events.Add(new BlackBoxEvent(span.Start, text, Palette.Buzzer, false)); + } + foreach (var span in brakings) + events.Add(new BlackBoxEvent(span.Start, string.Format(culture, strings["EventSecurityBraking"], + ProtectionName(recording.CabAt(span.Start))), Palette.Bad, true)); + } + + /// Which of them it was: the lamps blinking. + private static string ProtectionName(CabSample? sample) => + sample is not { } s ? "CA" + : s.Has(SecurityFlags.CabSignal) && s.Has(SecurityFlags.Vigilance) ? "CA/SHP" + : s.Has(SecurityFlags.CabSignal) ? "SHP" : "CA"; + private static void AddUserVehicles(Recording recording, IBlackBoxStrings strings, List events, CultureInfo culture) { UserVehicleChange? before = null; diff --git a/Eu07.BlackBox.Views/Palette.cs b/Eu07.BlackBox.Views/Palette.cs index 91319b3..b09ee5a 100644 --- a/Eu07.BlackBox.Views/Palette.cs +++ b/Eu07.BlackBox.Views/Palette.cs @@ -79,6 +79,8 @@ public static class Palette public static readonly IBrush LampOn = Brush("#4cc46e"); public static readonly IBrush LampWarning = Brush("#e8c547"); public static readonly IBrush LampAlarm = Brush("#f0584a"); + /// The train protection's buzzer: still to be acknowledged, not braking yet. + public static readonly IBrush Buzzer = Brush("#c77dff"); public static readonly IBrush LightWhite = Brush("#fff6d8"); public static readonly IBrush LightRed = Brush("#ff3b30"); public static readonly IBrush Command = Brush("#8b96a3"); diff --git a/Eu07.BlackBox.Views/TimelineView.cs b/Eu07.BlackBox.Views/TimelineView.cs index 0ce392b..1ebe001 100644 --- a/Eu07.BlackBox.Views/TimelineView.cs +++ b/Eu07.BlackBox.Views/TimelineView.cs @@ -245,16 +245,16 @@ public sealed class TimelineView : Control rows[2].Add(x0, x1, s.BrakeCylinder > 0.2f ? BrakeShade(s.BrakeCylinder) : null, BandHeight); if (s.Security != SecurityFlags.None || s.CabEnd != 0) { - // The alerter and the cab signalling apart, red when not acknowledged in time. - rows[3].Add(x0, x1, s.Has(SecurityFlags.VigilanceBeeping) ? Palette.LampAlarm + // The alerter and the cab signalling apart: the lamp, then its buzzer, red when it braked. + rows[3].Add(x0, x1, s.VigilanceBuzzer ? Palette.Buzzer : s.Has(SecurityFlags.Vigilance) ? Palette.Vigilance : null, BandHeight); - rows[4].Add(x0, x1, s.Has(SecurityFlags.CabSignalBeeping) ? Palette.LampAlarm + rows[4].Add(x0, x1, s.CabSignalBuzzer ? Palette.Buzzer : s.Has(SecurityFlags.CabSignal) ? Palette.CabSignal : null, BandHeight); - if (s.Has(SecurityFlags.Braking)) + if (s.ProtectionBraking) rows[s.Has(SecurityFlags.CabSignal) && !s.Has(SecurityFlags.Vigilance) ? 4 : 3].Add(x0, x1, Palette.Bad, BandHeight); } else - rows[3].Add(x0, x1, (s.Flags & CabFlags.AlerterBeeping) != 0 ? Palette.LampAlarm + rows[3].Add(x0, x1, (s.Flags & CabFlags.AlerterBeeping) != 0 ? Palette.Buzzer : (s.Flags & CabFlags.AlerterBlinking) != 0 ? Palette.Vigilance : null, BandHeight); // Where the user looked from: the samples go on in time, and so does the camera. while (camera + 1 < cameras.Count && cameras[camera + 1].Time <= s.Time) diff --git a/Eu07.BlackBox/Recording.cs b/Eu07.BlackBox/Recording.cs index 62263ce..b764f9a 100644 --- a/Eu07.BlackBox/Recording.cs +++ b/Eu07.BlackBox/Recording.cs @@ -519,13 +519,14 @@ public sealed class Recording return false; } + /// When the train protection braked the user's train: the alerter or the cab signalling not acknowledged. + public IReadOnlyList SecurityBrakings() => CabSpans(s => s.ProtectionBraking); + /// - /// When the train protection braked the user's train: the alerter or the cab signalling not acknowledged, its lamp - /// blinking. Braking with neither is the protection with no power (TSecuritySystem::is_braking), a cab not yet - /// started or a power cut: no driver's fault, not counted. + /// When the train protection's buzzer sounded in the user's cab: a lamp not acknowledged in time, the braking to + /// follow unless acknowledged on. Until it ends, braking included. /// - public IReadOnlyList SecurityBrakings() => - CabSpans(s => s.Has(SecurityFlags.Braking) && (s.Has(SecurityFlags.Vigilance) || s.Has(SecurityFlags.CabSignal))); + public IReadOnlyList SecurityAlarms() => CabSpans(s => s.VigilanceBuzzer || s.CabSignalBuzzer); /// The commands the user gave in a vehicle, presses only. public IEnumerable UserCommands() => _commands.Where(c => c.Pressed && c.Vehicle != 0); diff --git a/Eu07.BlackBox/Records.cs b/Eu07.BlackBox/Records.cs index a5046af..c02db64 100644 --- a/Eu07.BlackBox/Records.cs +++ b/Eu07.BlackBox/Records.cs @@ -371,6 +371,21 @@ public readonly record struct CabSample(double Time, int Vehicle, Position Posit public bool Has(CabDevices device) => (Devices & device) == device; public bool Has(SecurityFlags flag) => (Security & flag) == flag; + /// + /// The alerter's buzzer: its lamp not acknowledged in time. A protection with one acknowledgement for both tells + /// the cab signalling's buzzer as the alerter's too, so the alerter's lamp has to blink with it. + /// + public bool VigilanceBuzzer => Has(SecurityFlags.VigilanceBeeping) && Has(SecurityFlags.Vigilance); + + /// The cab signalling's buzzer: its lamp not acknowledged in time. + public bool CabSignalBuzzer => Has(SecurityFlags.CabSignalBeeping); + + /// + /// The train protection braking for an alarm not acknowledged, its lamp blinking. Braking with no lamp is the + /// protection with no power (TSecuritySystem::is_braking), a cab not started or a power cut: no driver's fault. + /// + public bool ProtectionBraking => Has(SecurityFlags.Braking) && (Has(SecurityFlags.Vigilance) || Has(SecurityFlags.CabSignal)); + public bool AiValid => (Flags & CabFlags.AiValid) != 0; public bool InCab => (Flags & CabFlags.InCab) != 0; public float AbsSpeedKmh => Math.Abs(SpeedKmh); diff --git a/StarterNG.Tests/BlackBox/SecurityBrakingTests.cs b/StarterNG.Tests/BlackBox/SecurityBrakingTests.cs index 8666db7..e6d6ae5 100644 --- a/StarterNG.Tests/BlackBox/SecurityBrakingTests.cs +++ b/StarterNG.Tests/BlackBox/SecurityBrakingTests.cs @@ -1,19 +1,68 @@ +using System.Globalization; using Eu07.BlackBox; +using Eu07.BlackBox.Views; namespace StarterNG.Tests.BlackBox; public class SecurityBrakingTests { + // as the simulator writes them: one acknowledgement for both, so the cab signalling's buzzer is the alerter's too + private const byte CabSignal = 0x02, CabSignalBuzzing = 0x0e, CabSignalBraking = 0x1e, NoPower = 0x10; + + private static Recording Drive(System.Func security) + { + var file = new BlackBoxFile().Info().Vehicle(1, "EP07-424").User(0, 1, true); + for (double t = 0; t <= 20; t += 0.5) + file.Cab(t, 1, 100, 0, security: security(t)); + return BlackBoxReader.Read(file.Stream()); + } + + private static List Events(Recording recording) + { + var culture = CultureInfo.CurrentCulture; + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + try + { + return EventList.Collect(recording, new DefaultBlackBoxStrings("pl")).Select(e => e.Text).ToList(); + } + finally + { + CultureInfo.CurrentCulture = culture; + } + } + [Fact] public void The_protection_braking_with_no_power_is_not_the_drivers_fault() { - var file = new BlackBoxFile().Info().Vehicle(1, "EP07-424").User(0, 1, true); // no power for the first seconds, then the cab signalling not acknowledged - for (double t = 0; t <= 20; t += 0.5) - file.Cab(t, 1, 100, 0, security: (byte)(t < 5 ? 0x10 : t is >= 10 and < 12 ? 0x1a : 0)); - var recording = BlackBoxReader.Read(file.Stream()); + var recording = Drive(t => t < 5 ? NoPower : t is >= 10 and < 12 ? (byte)0x1a : (byte)0); var braking = Assert.Single(recording.SecurityBrakings()); Assert.Equal(10, braking.Start, 6); } + + [Fact] + public void A_buzzer_acknowledged_in_time_is_told_apart_from_braking() + { + var recording = Drive(t => t is >= 2 and < 4 ? CabSignal : t is >= 4 and < 5.5 ? CabSignalBuzzing : (byte)0); + + var events = Events(recording); + + Assert.Contains("Brzęczyk SHP, potwierdzony po 1.5 s", events); + Assert.DoesNotContain(events, e => e.StartsWith("HAMOWANIE", System.StringComparison.Ordinal)); + Assert.Empty(recording.SecurityBrakings()); + } + + [Fact] + public void A_buzzer_not_acknowledged_is_followed_by_the_braking() + { + // the cab signalling at the start: the buzzer at once, braking when not acknowledged + var recording = Drive(t => t < 3 ? CabSignalBuzzing : t < 6 ? CabSignalBraking : (byte)0); + + var events = Events(recording); + + Assert.Equal(["Brzęczyk SHP, bez potwierdzenia", "HAMOWANIE PRZEZ SHP"], + events.Where(e => e.Contains("SHP", System.StringComparison.Ordinal))); + Assert.DoesNotContain(events, e => e.Contains("CA", System.StringComparison.Ordinal)); + } }