forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameAccumulator.cs
More file actions
104 lines (78 loc) · 2.35 KB
/
Copy pathFrameAccumulator.cs
File metadata and controls
104 lines (78 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SAAI
{
public abstract class FrameAccumulator : IDisposable
{
public delegate void EmailAccumulated(SortedList<DateTime, Frame> frames);
SortedList<DateTime, Frame> Frames { get; set; }
protected readonly object _lock = new object();
public int TimeToAccumulate { get; set; }
protected bool DisposedValue { get => disposedValue; set => disposedValue = value; }
protected System.Threading.Timer _timer;
private bool disposedValue = false; // To detect redundant calls
public void Init(int timeToAccumulate)
{
TimeToAccumulate = timeToAccumulate;
Frames = new SortedList<DateTime, Frame>();
}
public void Add(Frame frame)
{
lock (_lock)
{
Frames.Add(frame.Item.TimeEnqueued, frame);
if (null == _timer)
{
_timer = new System.Threading.Timer(DoneAccumulating, null, TimeToAccumulate * 1000, 0);
}
}
}
// This triggers when the timer expires or when we have the number of frames we were expecting
void DoneAccumulating(object obj)
{
lock (_lock)
{
_timer = null;
if (Frames.Values.Count > 0)
{
lock (Frames.Values[0].Item.CamData.AccumulateLock)
{
Frames.Values[0].Item.CamData.Accumulating = false; // no longer accumulating
Frames.Values[0].Item.CamData.TimeLastAccumulatorCompleted = DateTime.Now;
}
}
List<Frame> aCopy = new List<Frame>();
foreach (Frame frame in Frames.Values)
{
Frame cpy = new Frame(frame);
aCopy.Add(cpy);
}
ProcessAccumulatedFrames(aCopy);
Frames.Clear();
}
}
protected virtual void Dispose(bool disposing)
{
if (!DisposedValue)
{
if (disposing)
{
lock (_lock)
{
if (_timer != null) _timer.Dispose();
}
}
DisposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public abstract void ProcessAccumulatedFrames(List<Frame> frames);
}
}