2 using System.Collections.Generic;
24 DateTime IA = GetNextIntervalAlarm();
25 DateTime A = GetNextAlarm();
41 bool AlarmsExecuted =
false;
43 AlarmsExecuted |= Alarm(AlarmTime);
44 AlarmsExecuted |= IntervalAlarm(AlarmTime);
46 return AlarmsExecuted;
54 private object IntervalAlarmLocker =
new object();
55 private List<IntervalAlarmSettingBase> IntervalAlarmList =
new List<IntervalAlarmSettingBase>();
56 private abstract class IntervalAlarmSettingBase
58 public int IntervalMs {
get; set; }
59 public DateTime NextAlarm {
get; set; }
60 public abstract void Execute();
65 private class IntervalAlarmSettingPara : IntervalAlarmSettingBase
68 public Action<object> IntervalAlarmHandler {
get; set; }
69 public object Para {
get; set; }
72 public override void Execute()
76 IntervalAlarmHandler(Para);
80 throw new Exception(
"A exception occured in IntervalAlarm for AlarmHandler {0} with parameter {1}.".Build(IntervalAlarmHandler.ToString(), Para.ToString().Replace(
"\n",
",")), E);
84 public IntervalAlarmSettingPara() { }
85 public IntervalAlarmSettingPara(
int IntervalMs, Action<object> IntervalAlarmHandler,
object Para)
87 this.IntervalAlarmHandler = IntervalAlarmHandler;
88 this.IntervalMs = IntervalMs;
89 this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
94 private class IntervalAlarmSettingNoPara : IntervalAlarmSettingBase
97 public Action IntervalAlarmHandler {
get; set; }
100 public override void Execute()
104 IntervalAlarmHandler();
108 throw new Exception(
"A exception occured in IntervalAlarm for AlarmHandler {0}.".Build(IntervalAlarmHandler.ToString()), E);
112 public IntervalAlarmSettingNoPara() { }
113 public IntervalAlarmSettingNoPara(
int IntervalMs, Action IntervalAlarmHandler)
115 this.IntervalAlarmHandler = IntervalAlarmHandler;
116 this.IntervalMs = IntervalMs;
117 this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
121 private DateTime GetNextIntervalAlarm()
123 if (IntervalAlarmList.Count > 0)
125 return IntervalAlarmList.Min(x => x.NextAlarm);
127 return DateTime.MaxValue;
130 private bool IntervalAlarm(DateTime AlarmTime)
132 lock (IntervalAlarmLocker)
134 bool AlarmTriggered =
false;
135 IntervalAlarmList.Where(x => x.NextAlarm <= AlarmTime).ToList().ForEach(delegate(IntervalAlarmSettingBase S)
144 Log.Exception(
"A exception occured when excuting the handler for a interval alarm. This interval alarm will be disabled.", E);
147 AlarmTriggered =
true;
149 if (S.NextAlarm.AddMilliseconds(S.IntervalMs) <= AlarmTime)
151 S.NextAlarm = AlarmTime.AddMilliseconds(1);
155 S.NextAlarm = S.NextAlarm.AddMilliseconds(S.IntervalMs);
158 return AlarmTriggered;
170 lock (IntervalAlarmLocker)
172 UnregisterIntervalAlarm(IntervalAlarmHandler);
173 IntervalAlarmList.Add(
new IntervalAlarmSettingNoPara(IntervalMs, IntervalAlarmHandler));
184 lock (IntervalAlarmLocker)
186 IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingNoPara && ((IntervalAlarmSettingNoPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
200 lock (IntervalAlarmLocker)
202 UnregisterIntervalAlarm(IntervalAlarmHandler);
203 IntervalAlarmList.Add(
new IntervalAlarmSettingPara(IntervalMs, IntervalAlarmHandler, Parameter));
214 lock (IntervalAlarmLocker)
216 IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingPara && ((IntervalAlarmSettingPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
225 private object AlarmLocker =
new object();
226 private List<AlarmSettingsBase> AlarmList =
new List<AlarmSettingsBase>();
228 private abstract class AlarmSettingsBase
230 public DateTime AlarmTime {
get; set; }
231 public abstract void Execute();
234 private class AlarmSettingNoPara : AlarmSettingsBase
237 public Action AlarmHandler {
get; set; }
240 public override void Execute()
248 Log.Exception(
"A exception occured for AlarmHandler {0}.".Build(AlarmHandler.ToString()), E);
252 public AlarmSettingNoPara() { }
253 public AlarmSettingNoPara(DateTime AlarmTime, Action AlarmHandler)
255 this.AlarmTime = AlarmTime;
256 this.AlarmHandler = AlarmHandler;
261 private class AlarmSettingPara : AlarmSettingsBase
264 public Action<object> AlarmHandler {
get; set; }
265 public object Para {
get; set; }
267 public override void Execute()
275 Log.Exception(
"A exception occured for AlarmHandler {0} with parameter {1}.".Build(AlarmHandler.ToString(), Para.ToString().Replace(
"\n",
",")), E);
279 public AlarmSettingPara() { }
280 public AlarmSettingPara(DateTime AlarmTime, Action<object> AlarmHandler,
object Para)
282 this.AlarmTime = AlarmTime;
283 this.AlarmHandler = AlarmHandler;
288 private DateTime GetNextAlarm()
290 if (AlarmList.Count > 0)
292 return AlarmList.Min(x => x.AlarmTime);
294 return DateTime.MaxValue;
297 private bool Alarm(DateTime AlarmTime)
302 List<AlarmSettingsBase> L = AlarmList.Where(x => x.AlarmTime <= AlarmTime).ToList();
303 AlarmList.RemoveAll(x => x.AlarmTime <= AlarmTime);
304 L.ForEach(delegate(AlarmSettingsBase S)
308 return (L.Count > 0);
324 RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, DontUnregister);
337 if (!DontUnregister) UnregisterAlarm(AlarmHandler);
338 AlarmList.Add(
new AlarmSettingNoPara(AlarmTime, AlarmHandler));
351 AlarmList.RemoveAll(x => x is AlarmSettingNoPara && ((AlarmSettingNoPara)x).AlarmHandler == AlarmHandler);
365 RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, Parameter, DontUnregister);
379 if(!DontUnregister) UnregisterAlarm(AlarmHandler);
380 AlarmList.Add(
new AlarmSettingPara(AlarmTime, AlarmHandler, Parameter));
393 AlarmList.RemoveAll(x => x is AlarmSettingPara && ((AlarmSettingPara)x).AlarmHandler == AlarmHandler);
415 IntervalAlarmList.Clear();
The AlarmHandler classed is used to execute scheduled events (e.g. regular updates on a effect) in th...
DateTime GetNextAlarmTime()
Gets the time when the next alarm (interval or single) is scheduled.
void Init(Pinball Pinball)
Inits the object.
void RegisterAlarm(int DurationMs, Action AlarmHandler, bool DontUnregister=false)
Registers the specied AlarmHandler for a alarm after the specified duration.
void RegisterIntervalAlarm(int IntervalMs, Action IntervalAlarmHandler)
Registers the method specified in IntervalAlarmHandler for interval alarms. Interval alarms are fire...
void UnregisterAlarm(Action< object > AlarmHandler)
Unregisters all alarms for the specified alarm handler.
Pinball is the main object of the DirectOutput framework. It holds all objects required to process P...
void RegisterAlarm(int DurationMs, Action< object > AlarmHandler, object Parameter, bool DontUnregister=false)
Registers the specied AlarmHandler for a alarm after the specified duration.
void UnregisterIntervalAlarm(Action< object > IntervalAlarmHandler)
Unregisters the specified IntervalAlarmHandler.
void UnregisterAlarm(Action AlarmHandler)
Unregisters all alarm for the specified alarm handler.
void RegisterIntervalAlarm(int IntervalMs, Action< object > IntervalAlarmHandler, object Parameter)
Registers the method specified in IntervalAlarmHandler for interval alarms. Interval alarms are fire...
void Finish()
Finishes the object. Clears all alarm lists.
void RegisterAlarm(DateTime AlarmTime, Action< object > AlarmHandler, object Parameter, bool DontUnregister=false)
Registers the specified AlarmHandler for a alarm at the certain time.
void UnregisterIntervalAlarm(Action IntervalAlarmHandler)
Unregisters the specified IntervalAlarmHandler.
bool ExecuteAlarms(DateTime AlarmTime)
Executes all Alarmes which have expired until the specified AlarmTime..
void RegisterAlarm(DateTime AlarmTime, Action AlarmHandler, bool DontUnregister=false)
Registers the specified AlarmHandler for a alarm at the certain time.