144 lines
3.5 KiB
C#
144 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Diagnostics;
|
|
|
|
namespace Tidstagning
|
|
{
|
|
class Procedure
|
|
{
|
|
Relay horn;
|
|
ResultList log;
|
|
List<DateTime> signaler;
|
|
int signalLength = 500;
|
|
|
|
Timer timer;
|
|
Int16 idx = 0;
|
|
|
|
public Procedure()
|
|
{
|
|
signaler = new List<DateTime>();
|
|
}
|
|
|
|
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<DateTime>();
|
|
idx = 0;
|
|
if(timer != null)
|
|
timer.Dispose();
|
|
}
|
|
|
|
public void setObjects(Relay horn_obj, ResultList log_obj)
|
|
{
|
|
this.horn = horn_obj;
|
|
this.log = log_obj;
|
|
Run();
|
|
}
|
|
|
|
public void adjustSignalLength(int 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
|
|
}
|
|
log.Write("Næste signal: " + alertTime.ToString(@"HH\:mm\:ss") + " Om: " + timeToGo.ToString(@"hh\:mm\:ss"));
|
|
timer = new System.Threading.Timer(x =>
|
|
{
|
|
this.Alarm();
|
|
}, null, timeToGo, new System.TimeSpan (0));
|
|
}
|
|
|
|
private void Alarm()
|
|
{
|
|
log.Write("HORN!");
|
|
|
|
horn.Sound(signalLength);
|
|
next();
|
|
Run();
|
|
|
|
}
|
|
|
|
private bool next()
|
|
{
|
|
if (idx >= (signaler.Count - 1))
|
|
return false;
|
|
|
|
idx++;
|
|
return true;
|
|
}
|
|
}
|
|
}
|