using System.Collections.Generic; using System.Diagnostics; using Timer=System.Threading.Timer; namespace Tidstagning { class Procedure { Relay? horn; ResultList? log; List signaler; uint signalLength = 500; Timer? delayTimer; Int16 idx = 0; public Procedure() { signaler = new List(); } public void ReadFile(string file) { System.IO.StreamReader procedure_handle = new System.IO.StreamReader(file); string? line; while ((line = procedure_handle.ReadLine()) != null) { addEntry(line); } procedure_handle.Dispose(); } public void addEntry(string input) { string input_filtered = input.Trim(); if (input_filtered.Length > 0) { try { signaler.Add(DateTime.Parse(input_filtered)); } catch { Debug.WriteLine("Failed to parse: " + input); } signaler.Sort(); } } public string TextualRepresentation() { string t = ""; if (signaler.Count > 0) { foreach (DateTime signal in signaler) { if (DateTime.Now > (signal.AddMilliseconds(signalLength))) { t += "✔"; } else if (DateTime.Now > signal) { t += "o "; } else { t += " "; } t += signal.ToString() + "\r\n"; } } return t; } public void Clear() { signaler = new List(); idx = 0; if (delayTimer != null) delayTimer.Dispose(); } public void setObjects(Relay horn_obj, ResultList log_obj) { this.horn = horn_obj; this.log = log_obj; Run(); } public void adjustSignalLength(uint lengthMS) { signalLength = lengthMS; } public void Run() { if (signaler.Count > 0) { SetUpTimer(signaler[idx]); } } private void SetUpTimer(DateTime alertTime) { DateTime current = DateTime.Now; TimeSpan timeToGo = alertTime.TimeOfDay - current.TimeOfDay; if (timeToGo < TimeSpan.Zero) { if (!next()) { return; } Run(); return;//time already passed } if (log is not null) { log.Write("Næste signal: " + alertTime.ToString(@"HH\:mm\:ss") + " Om: " + timeToGo.ToString(@"hh\:mm\:ss")); } delayTimer = new Timer(x => { this.Alarm(); }, null, timeToGo, new TimeSpan(0)); } private void Alarm() { if(log is not null) log.Write("HORN!"); if(horn is not null) horn.Sound(signalLength); next(); Run(); } private bool next() { if (idx >= (signaler.Count - 1)) return false; idx++; return true; } } }