forked from Ken98045/On-Guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryMonitor.cs
More file actions
106 lines (92 loc) · 3.4 KB
/
DirectoryMonitor.cs
File metadata and controls
106 lines (92 loc) · 3.4 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
105
106
using System;
using System.IO;
namespace SAAI
{
public delegate void CameraEventHandler(CameraData cameraData, string eventInfo);
/// <summary>
/// A class to monitor a directory for new files that have motion identified by Blue Iris
/// It then notifies the main UI of the new file.
/// </summary>
public class DirectoryMonitor : IDisposable
{
public FileSystemWatcher Watcher { get; }
public String Path() { return CameraData.PathAndPrefix(cameraData); }
public event CameraEventHandler OnNewImage;
readonly CameraData cameraData;
public DirectoryMonitor(CameraData location)
{
if (location == null || string.IsNullOrEmpty(location.Path))
{
string msg = "DirectoryMonitor constructor: The camera is null or the path is null/empty";
Dbg.Write(msg);
ArgumentException ex = new ArgumentException(msg);
throw ex;
}
if (!Directory.Exists(location.Path))
{
string msg = "DirectoryMonitor constructor: The directory being monitored does not exist: " + location.Path + " Check your camera settings!";
Dbg.Write(msg);
ArgumentException ex = new ArgumentException(msg);
throw ex;
}
try
{
Watcher = new FileSystemWatcher(location.Path, location.CameraPrefix + "*.jpg");
cameraData = location;
Watcher.Changed += FileChanged;
Watcher.NotifyFilter = NotifyFilters.LastWrite;
Watcher.EnableRaisingEvents = true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
Dbg.Write("DirectoryMonitor constructor exception: " + ex.Message);
}
}
// You can get at least 2 notifications for each new image file. One when it is
// created and on when it is written to. The client (main UI) must be able to handle that.
private void FileChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
if (null != OnNewImage)
{
OnNewImage.Invoke(cameraData, e.FullPath);
}
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Watcher.EnableRaisingEvents = false;
Watcher.Changed -= FileChanged;
Watcher.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~DirectoryMonitor()
// {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
}
}