2 using System.Collections.Generic;
4 using DirectOutput.General.Statistics;
6 namespace DirectOutput.PinballSupport
23 public DateTime GetNextAlarmTime()
25 DateTime IA = GetNextIntervalAlarm();
26 DateTime A = GetNextAlarm();
40 public bool ExecuteAlarms(DateTime AlarmTime)
42 bool AlarmsExecuted =
false;
44 AlarmsExecuted |= Alarm(AlarmTime);
45 AlarmsExecuted |= IntervalAlarm(AlarmTime);
47 return AlarmsExecuted;
55 private object IntervalAlarmLocker =
new object();
56 private List<IntervalAlarmSettingBase> IntervalAlarmList =
new List<IntervalAlarmSettingBase>();
57 private abstract class IntervalAlarmSettingBase
59 public int IntervalMs {
get;
set; }
60 public DateTime NextAlarm {
get;
set; }
61 public abstract void Execute();
66 private class IntervalAlarmSettingPara : IntervalAlarmSettingBase
69 public Action<object> IntervalAlarmHandler {
get;
set; }
70 public object Para {
get;
set; }
73 public override void Execute()
77 IntervalAlarmHandler(Para);
81 throw new Exception(
"A exception occured in IntervalAlarm for AlarmHandler {0} with parameter {1}.".Build(IntervalAlarmHandler.ToString(), Para.ToString().Replace(
"\n",
",")), E);
85 public IntervalAlarmSettingPara() { }
86 public IntervalAlarmSettingPara(
int IntervalMs, Action<object> IntervalAlarmHandler,
object Para)
88 this.IntervalAlarmHandler = IntervalAlarmHandler;
89 this.IntervalMs = IntervalMs;
90 this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
95 private class IntervalAlarmSettingNoPara : IntervalAlarmSettingBase
98 public Action IntervalAlarmHandler {
get;
set; }
101 public override void Execute()
105 IntervalAlarmHandler();
109 throw new Exception(
"A exception occured in IntervalAlarm for AlarmHandler {0}.".Build(IntervalAlarmHandler.ToString()), E);
113 public IntervalAlarmSettingNoPara() { }
114 public IntervalAlarmSettingNoPara(
int IntervalMs, Action IntervalAlarmHandler)
116 this.IntervalAlarmHandler = IntervalAlarmHandler;
117 this.IntervalMs = IntervalMs;
118 this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
122 private DateTime GetNextIntervalAlarm()
124 if (IntervalAlarmList.Count > 0)
126 return IntervalAlarmList.Min(x => x.NextAlarm);
128 return DateTime.MaxValue;
131 private bool IntervalAlarm(DateTime AlarmTime)
133 lock (IntervalAlarmLocker)
135 bool AlarmTriggered =
false;
136 IntervalAlarmList.Where(x => x.NextAlarm <= AlarmTime).ToList().ForEach(delegate(IntervalAlarmSettingBase S)
138 IntervalAlarmStatistics.MeasurementStart();
145 Log.Exception(
"A exception occured when excuting the handler for a interval alarm. This interval alarm will be disabled.", E);
147 IntervalAlarmStatistics.MeasurementStop();
148 AlarmTriggered =
true;
150 if (S.NextAlarm.AddMilliseconds(S.IntervalMs) <= AlarmTime)
152 S.NextAlarm = AlarmTime.AddMilliseconds(1);
156 S.NextAlarm = S.NextAlarm.AddMilliseconds(S.IntervalMs);
159 return AlarmTriggered;
169 public void RegisterIntervalAlarm(
int IntervalMs, Action IntervalAlarmHandler)
171 lock (IntervalAlarmLocker)
173 UnregisterIntervalAlarm(IntervalAlarmHandler);
174 IntervalAlarmList.Add(
new IntervalAlarmSettingNoPara(IntervalMs, IntervalAlarmHandler));
183 public void UnregisterIntervalAlarm(Action IntervalAlarmHandler)
185 lock (IntervalAlarmLocker)
187 IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingNoPara && ((IntervalAlarmSettingNoPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
199 public void RegisterIntervalAlarm(
int IntervalMs, Action<object> IntervalAlarmHandler,
object Parameter)
201 lock (IntervalAlarmLocker)
203 UnregisterIntervalAlarm(IntervalAlarmHandler);
204 IntervalAlarmList.Add(
new IntervalAlarmSettingPara(IntervalMs, IntervalAlarmHandler, Parameter));
213 public void UnregisterIntervalAlarm(Action<object> IntervalAlarmHandler)
215 lock (IntervalAlarmLocker)
217 IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingPara && ((IntervalAlarmSettingPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
226 private object AlarmLocker =
new object();
227 private List<AlarmSettingsBase> AlarmList =
new List<AlarmSettingsBase>();
229 private abstract class AlarmSettingsBase
231 public DateTime AlarmTime {
get;
set; }
232 public abstract void Execute();
235 private class AlarmSettingNoPara : AlarmSettingsBase
238 public Action AlarmHandler {
get;
set; }
241 public override void Execute()
249 Log.Exception(
"A exception occured for AlarmHandler {0}.".Build(AlarmHandler.ToString()), E);
253 public AlarmSettingNoPara() { }
254 public AlarmSettingNoPara(DateTime AlarmTime, Action AlarmHandler)
256 this.AlarmTime = AlarmTime;
257 this.AlarmHandler = AlarmHandler;
262 private class AlarmSettingPara : AlarmSettingsBase
265 public Action<object> AlarmHandler {
get;
set; }
266 public object Para {
get;
set; }
268 public override void Execute()
276 Log.Exception(
"A exception occured for AlarmHandler {0} with parameter {1}.".Build(AlarmHandler.ToString(), Para.ToString().Replace(
"\n",
",")), E);
280 public AlarmSettingPara() { }
281 public AlarmSettingPara(DateTime AlarmTime, Action<object> AlarmHandler,
object Para)
283 this.AlarmTime = AlarmTime;
284 this.AlarmHandler = AlarmHandler;
289 private DateTime GetNextAlarm()
291 if (AlarmList.Count > 0)
293 return AlarmList.Min(x => x.AlarmTime);
295 return DateTime.MaxValue;
298 private bool Alarm(DateTime AlarmTime)
303 List<AlarmSettingsBase> L = AlarmList.Where(x => x.AlarmTime <= AlarmTime).ToList();
304 AlarmList.RemoveAll(x => x.AlarmTime <= AlarmTime);
305 L.ForEach(delegate(AlarmSettingsBase S)
309 return (L.Count > 0);
323 public void RegisterAlarm(
int DurationMs, Action
AlarmHandler,
bool DontUnregister =
false)
325 RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, DontUnregister);
334 public void RegisterAlarm(DateTime AlarmTime, Action
AlarmHandler,
bool DontUnregister =
false)
338 if (!DontUnregister) UnregisterAlarm(AlarmHandler);
339 AlarmList.Add(
new AlarmSettingNoPara(AlarmTime, AlarmHandler));
352 AlarmList.RemoveAll(x => x is AlarmSettingNoPara && ((AlarmSettingNoPara)x).AlarmHandler == AlarmHandler);
364 public void RegisterAlarm(
int DurationMs, Action<object>
AlarmHandler,
object Parameter,
bool DontUnregister =
false)
366 RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, Parameter, DontUnregister);
376 public void RegisterAlarm(DateTime AlarmTime, Action<object>
AlarmHandler,
object Parameter,
bool DontUnregister =
false)
380 if(!DontUnregister) UnregisterAlarm(AlarmHandler);
381 AlarmList.Add(
new AlarmSettingPara(AlarmTime, AlarmHandler, Parameter));
394 AlarmList.RemoveAll(x => x is AlarmSettingPara && ((AlarmSettingPara)x).AlarmHandler == AlarmHandler);
406 AlarmStatistics =
new TimeSpanStatisticsItem() { Name =
"Alarm calls", GroupName =
"Pinball - Alarm calls" };
408 IntervalAlarmStatistics =
new TimeSpanStatisticsItem() { Name =
"Interval Alarm calls", GroupName =
"Pinball - Alarm calls" };
419 IntervalAlarmList.Clear();