forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraCollection.cs
201 lines (169 loc) · 4.71 KB
/
CameraCollection.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace SAAI
{
/// <summary>
/// Mainly a wrapper around the dictionary holding all the cameras.
/// However, it also keeps track of the current camera and adds some helper methods.
/// It also allow for persistence through the Load and Save methods.
/// </summary>
[Serializable]
public class CameraCollection : IDisposable
{
public Dictionary<string, CameraData> CameraDictionary { get; }
public string CurrentCameraPath { get; set; }
public CameraCollection()
{
CameraDictionary = new Dictionary<string, CameraData>();
CurrentCameraPath = string.Empty;
}
public CameraCollection(CameraCollection src)
{
CameraDictionary = new Dictionary<string, CameraData>();
if (null != src && null != src.CameraDictionary && null != src.CameraDictionary.Values)
{
foreach (var cam in src.CameraDictionary.Values)
{
CameraData newCam = new CameraData(cam);
CameraDictionary.Add(CameraData.PathAndPrefix(newCam), newCam);
newCam.Init();
}
CurrentCameraPath = src.CurrentCameraPath;
}
}
public CameraData this[string cameraPath]
{
get
{
if (string.IsNullOrEmpty(cameraPath))
{
return null;
}
else
{
if (CameraDictionary.TryGetValue(cameraPath.ToLower(), out CameraData cam))
{
return cam;
}
else
{
return null;
}
}
}
}
public CameraData CurrentCamera
{
get
{
if (string.IsNullOrEmpty(CurrentCameraPath))
{
return null;
}
else
{
if (CameraDictionary.TryGetValue(CurrentCameraPath, out CameraData camData))
{
return camData;
}
else
{
return null;
}
}
}
}
public void AddCamera(CameraData camData)
{
CameraDictionary.Add(CameraData.PathAndPrefix(camData), camData);
}
public void DeleteCamera(CameraData camData)
{
CameraDictionary.Remove(CameraData.PathAndPrefix(camData));
}
public void StopMonitoring()
{
foreach(var cam in CameraDictionary.Values)
{
if (cam.Monitoring)
{
cam.Monitor?.Dispose();
cam.Monitor = null;
}
}
}
public void StartMonitoring()
{
foreach (var cam in CameraDictionary.Values)
{
if (cam.Monitoring)
{
cam.Monitor = new DirectoryMonitor(cam);
}
}
}
/// <summary>
/// All camera data is stored in separate files.
/// It is binary serialized due to the fact that the
/// dictionary can't be XML serialized.
/// </summary>
/// <returns></returns>
public static CameraCollection Load()
{
CameraCollection all = Storage.GetAllCameras();
/*CameraCollection all = new CameraCollection();
string path = Storage.GetFilePath("CameraData.bin");
if (File.Exists(path))
{
BinaryFormatter serializer = new BinaryFormatter();
if (File.Exists(path))
{
using (Stream reader = new FileStream(path, FileMode.Open))
{
all = (CameraCollection)serializer.Deserialize(reader);
}
}
}*/
foreach (var cam in all.CameraDictionary.Values)
{
cam.Init();
}
return all;
}
public static void Save(CameraCollection allCameras)
{
Storage.SaveCameras(allCameras);
/*BinaryFormatter serializer = new BinaryFormatter();
using (Stream stream = new FileStream(Storage.GetFilePath("CameraData.bin"), FileMode.Create))
{
serializer.Serialize(stream, allCameras);
}*/
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
foreach (var cam in CameraDictionary.Values)
{
cam.Dispose();
}
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}