96 lines
2.2 KiB
C#
96 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
|
|
namespace Tidstagning
|
|
{
|
|
class Procedure
|
|
{
|
|
|
|
struct Signal
|
|
{
|
|
public TimeSpan tid;
|
|
public Int32 Duration;
|
|
|
|
public Signal(string parse)
|
|
{
|
|
string[] parts = parse.Split(',');
|
|
this.tid = TimeSpan.Parse(parts[0]);
|
|
this.Duration = Int32.Parse(parts[1]);
|
|
}
|
|
}
|
|
|
|
Relay horn;
|
|
ResultList log;
|
|
readonly List<Signal> signaler;
|
|
Timer cooldownTimer;
|
|
Timer timer;
|
|
Int16 idx = 0;
|
|
public Procedure()
|
|
{
|
|
signaler = new List<Signal>();
|
|
}
|
|
|
|
public void addEntry(string input) {
|
|
signaler.Add(new Signal(input));
|
|
}
|
|
|
|
public void setObjects(Relay horn_obj, ResultList log_obj)
|
|
{
|
|
this.horn = horn_obj;
|
|
this.log = log_obj;
|
|
Run();
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
SetUpTimer(signaler[idx].tid);
|
|
|
|
}
|
|
private void SetUpTimer(TimeSpan alertTime)
|
|
{
|
|
|
|
DateTime current = DateTime.Now;
|
|
TimeSpan timeToGo = alertTime - current.TimeOfDay;
|
|
|
|
log.Write("Næste signal: " + alertTime.ToString() + " Om: " + timeToGo.ToString());
|
|
if (timeToGo < TimeSpan.Zero)
|
|
{
|
|
next();
|
|
Run();
|
|
return;//time already passed
|
|
}
|
|
timer = new System.Threading.Timer(x =>
|
|
{
|
|
this.Alarm();
|
|
}, null, timeToGo, new System.TimeSpan (0));
|
|
}
|
|
|
|
private void Alarm()
|
|
{
|
|
log.Write("HORN!");
|
|
horn.Set(0x00, 0x01);
|
|
cooldownTimer = new System.Threading.Timer(x =>
|
|
{
|
|
this.Cooldown();
|
|
}, null, new TimeSpan(0,0,signaler[idx].Duration/1000), new System.TimeSpan(0));
|
|
|
|
}
|
|
|
|
private void Cooldown()
|
|
{
|
|
horn.Set(0x00, 0x00);
|
|
next();
|
|
this.Run();
|
|
}
|
|
|
|
private void next()
|
|
{
|
|
if (idx >= (signaler.Count - 1))
|
|
return;
|
|
|
|
idx++;
|
|
}
|
|
}
|
|
}
|