WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
AlarmHandler.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 
6 namespace DirectOutput.PinballSupport
7 {
14  public class AlarmHandler
15  {
16 
17 
22  public DateTime GetNextAlarmTime()
23  {
24  DateTime IA = GetNextIntervalAlarm();
25  DateTime A = GetNextAlarm();
26 
27  if (IA < A)
28  {
29  return IA;
30  }
31  return A;
32  }
33 
39  public bool ExecuteAlarms(DateTime AlarmTime)
40  {
41  bool AlarmsExecuted = false;
42 
43  AlarmsExecuted |= Alarm(AlarmTime);
44  AlarmsExecuted |= IntervalAlarm(AlarmTime);
45 
46  return AlarmsExecuted;
47  }
48 
49 
50 
51 
52 
53  #region IntervalAlarm
54  private object IntervalAlarmLocker = new object();
55  private List<IntervalAlarmSettingBase> IntervalAlarmList = new List<IntervalAlarmSettingBase>();
56  private abstract class IntervalAlarmSettingBase
57  {
58  public int IntervalMs { get; set; }
59  public DateTime NextAlarm { get; set; }
60  public abstract void Execute();
61 
62 
63  }
64 
65  private class IntervalAlarmSettingPara : IntervalAlarmSettingBase
66  {
67 
68  public Action<object> IntervalAlarmHandler { get; set; }
69  public object Para { get; set; }
70 
71 
72  public override void Execute()
73  {
74  try
75  {
76  IntervalAlarmHandler(Para);
77  }
78  catch (Exception E)
79  {
80  throw new Exception("A exception occured in IntervalAlarm for AlarmHandler {0} with parameter {1}.".Build(IntervalAlarmHandler.ToString(), Para.ToString().Replace("\n", ",")), E);
81  }
82  }
83 
84  public IntervalAlarmSettingPara() { }
85  public IntervalAlarmSettingPara(int IntervalMs, Action<object> IntervalAlarmHandler, object Para)
86  {
87  this.IntervalAlarmHandler = IntervalAlarmHandler;
88  this.IntervalMs = IntervalMs;
89  this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
90  this.Para = Para;
91  }
92  }
93 
94  private class IntervalAlarmSettingNoPara : IntervalAlarmSettingBase
95  {
96 
97  public Action IntervalAlarmHandler { get; set; }
98 
99 
100  public override void Execute()
101  {
102  try
103  {
104  IntervalAlarmHandler();
105  }
106  catch (Exception E)
107  {
108  throw new Exception("A exception occured in IntervalAlarm for AlarmHandler {0}.".Build(IntervalAlarmHandler.ToString()), E);
109  }
110  }
111 
112  public IntervalAlarmSettingNoPara() { }
113  public IntervalAlarmSettingNoPara(int IntervalMs, Action IntervalAlarmHandler)
114  {
115  this.IntervalAlarmHandler = IntervalAlarmHandler;
116  this.IntervalMs = IntervalMs;
117  this.NextAlarm = DateTime.Now.AddMilliseconds(IntervalMs);
118  }
119  }
120 
121  private DateTime GetNextIntervalAlarm()
122  {
123  if (IntervalAlarmList.Count > 0)
124  {
125  return IntervalAlarmList.Min(x => x.NextAlarm);
126  }
127  return DateTime.MaxValue;
128  }
129 
130  private bool IntervalAlarm(DateTime AlarmTime)
131  {
132  lock (IntervalAlarmLocker)
133  {
134  bool AlarmTriggered = false;
135  IntervalAlarmList.Where(x => x.NextAlarm <= AlarmTime).ToList().ForEach(delegate(IntervalAlarmSettingBase S)
136  {
137 
138  try
139  {
140  S.Execute();
141  }
142  catch (Exception E)
143  {
144  Log.Exception("A exception occured when excuting the handler for a interval alarm. This interval alarm will be disabled.", E);
145  }
146 
147  AlarmTriggered = true;
148 
149  if (S.NextAlarm.AddMilliseconds(S.IntervalMs) <= AlarmTime)
150  {
151  S.NextAlarm = AlarmTime.AddMilliseconds(1);
152  }
153  else
154  {
155  S.NextAlarm = S.NextAlarm.AddMilliseconds(S.IntervalMs);
156  }
157  });
158  return AlarmTriggered;
159  }
160  }
161 
168  public void RegisterIntervalAlarm(int IntervalMs, Action IntervalAlarmHandler)
169  {
170  lock (IntervalAlarmLocker)
171  {
172  UnregisterIntervalAlarm(IntervalAlarmHandler);
173  IntervalAlarmList.Add(new IntervalAlarmSettingNoPara(IntervalMs, IntervalAlarmHandler));
174  }
175  }
176 
177 
182  public void UnregisterIntervalAlarm(Action IntervalAlarmHandler)
183  {
184  lock (IntervalAlarmLocker)
185  {
186  IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingNoPara && ((IntervalAlarmSettingNoPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
187  }
188  }
189 
190 
198  public void RegisterIntervalAlarm(int IntervalMs, Action<object> IntervalAlarmHandler, object Parameter)
199  {
200  lock (IntervalAlarmLocker)
201  {
202  UnregisterIntervalAlarm(IntervalAlarmHandler);
203  IntervalAlarmList.Add(new IntervalAlarmSettingPara(IntervalMs, IntervalAlarmHandler, Parameter));
204  }
205  }
206 
207 
212  public void UnregisterIntervalAlarm(Action<object> IntervalAlarmHandler)
213  {
214  lock (IntervalAlarmLocker)
215  {
216  IntervalAlarmList.RemoveAll(x => x is IntervalAlarmSettingPara && ((IntervalAlarmSettingPara)x).IntervalAlarmHandler == IntervalAlarmHandler);
217  }
218  }
219 
220  #endregion
221 
222 
223 
224  #region Alarm
225  private object AlarmLocker = new object();
226  private List<AlarmSettingsBase> AlarmList = new List<AlarmSettingsBase>();
227 
228  private abstract class AlarmSettingsBase
229  {
230  public DateTime AlarmTime { get; set; }
231  public abstract void Execute();
232  }
233 
234  private class AlarmSettingNoPara : AlarmSettingsBase
235  {
236 
237  public Action AlarmHandler { get; set; }
238 
239 
240  public override void Execute()
241  {
242  try
243  {
244  AlarmHandler();
245  }
246  catch (Exception E)
247  {
248  Log.Exception("A exception occured for AlarmHandler {0}.".Build(AlarmHandler.ToString()), E);
249  }
250  }
251 
252  public AlarmSettingNoPara() { }
253  public AlarmSettingNoPara(DateTime AlarmTime, Action AlarmHandler)
254  {
255  this.AlarmTime = AlarmTime;
256  this.AlarmHandler = AlarmHandler;
257 
258  }
259  }
260 
261  private class AlarmSettingPara : AlarmSettingsBase
262  {
263 
264  public Action<object> AlarmHandler { get; set; }
265  public object Para { get; set; }
266 
267  public override void Execute()
268  {
269  try
270  {
271  AlarmHandler(Para);
272  }
273  catch (Exception E)
274  {
275  Log.Exception("A exception occured for AlarmHandler {0} with parameter {1}.".Build(AlarmHandler.ToString(), Para.ToString().Replace("\n", ",")), E);
276  }
277  }
278 
279  public AlarmSettingPara() { }
280  public AlarmSettingPara(DateTime AlarmTime, Action<object> AlarmHandler, object Para)
281  {
282  this.AlarmTime = AlarmTime;
283  this.AlarmHandler = AlarmHandler;
284  this.Para = Para;
285  }
286  }
287 
288  private DateTime GetNextAlarm()
289  {
290  if (AlarmList.Count > 0)
291  {
292  return AlarmList.Min(x => x.AlarmTime);
293  }
294  return DateTime.MaxValue;
295  }
296 
297  private bool Alarm(DateTime AlarmTime)
298  {
299 
300  lock (AlarmLocker)
301  {
302  List<AlarmSettingsBase> L = AlarmList.Where(x => x.AlarmTime <= AlarmTime).ToList();
303  AlarmList.RemoveAll(x => x.AlarmTime <= AlarmTime);
304  L.ForEach(delegate(AlarmSettingsBase S)
305  {
306  S.Execute();
307  });
308  return (L.Count > 0);
309  }
310  }
311 
312 
313 
314 
315 
322  public void RegisterAlarm(int DurationMs, Action AlarmHandler, bool DontUnregister = false)
323  {
324  RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, DontUnregister);
325  }
326 
333  public void RegisterAlarm(DateTime AlarmTime, Action AlarmHandler, bool DontUnregister = false)
334  {
335  lock (AlarmLocker)
336  {
337  if (!DontUnregister) UnregisterAlarm(AlarmHandler);
338  AlarmList.Add(new AlarmSettingNoPara(AlarmTime, AlarmHandler));
339  }
340  }
341 
342 
347  public void UnregisterAlarm(Action AlarmHandler)
348  {
349  lock (AlarmLocker)
350  {
351  AlarmList.RemoveAll(x => x is AlarmSettingNoPara && ((AlarmSettingNoPara)x).AlarmHandler == AlarmHandler);
352  }
353  }
354 
355 
363  public void RegisterAlarm(int DurationMs, Action<object> AlarmHandler, object Parameter, bool DontUnregister = false)
364  {
365  RegisterAlarm(DateTime.Now.AddMilliseconds(DurationMs), AlarmHandler, Parameter, DontUnregister);
366  }
367 
375  public void RegisterAlarm(DateTime AlarmTime, Action<object> AlarmHandler, object Parameter, bool DontUnregister = false)
376  {
377  lock (AlarmLocker)
378  {
379  if(!DontUnregister) UnregisterAlarm(AlarmHandler);
380  AlarmList.Add(new AlarmSettingPara(AlarmTime, AlarmHandler, Parameter));
381  }
382  }
383 
384 
389  public void UnregisterAlarm(Action<object> AlarmHandler)
390  {
391  lock (AlarmLocker)
392  {
393  AlarmList.RemoveAll(x => x is AlarmSettingPara && ((AlarmSettingPara)x).AlarmHandler == AlarmHandler);
394  }
395  }
396 
397  #endregion
398 
399 
403  public void Init(Pinball Pinball)
404  {
405 
406  }
407 
411  public void Finish()
412  {
413 
414  AlarmList.Clear();
415  IntervalAlarmList.Clear();
416  }
417 
418 
419  #region Constructor
420  public AlarmHandler()
424  {
425 
426 
427  }
428 
429 
430 
431 
432  #endregion
433 
434 
435 
436  }
437 }
The AlarmHandler classed is used to execute scheduled events (e.g. regular updates on a effect) in th...
Definition: AlarmHandler.cs:14
DateTime GetNextAlarmTime()
Gets the time when the next alarm (interval or single) is scheduled.
Definition: AlarmHandler.cs:22
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...
Definition: Pinball.cs:23
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..
Definition: AlarmHandler.cs:39
void RegisterAlarm(DateTime AlarmTime, Action AlarmHandler, bool DontUnregister=false)
Registers the specified AlarmHandler for a alarm at the certain time.