OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
OSDPCaptureFileWriter.cs
1using System;
2using System.IO;
3using System.Text.Json;
4
5namespace OSDP.Net.Tracing;
6
11public class OSDPCaptureFileWriter : IDisposable
12{
13 private readonly StreamWriter _writer;
14 private readonly string _source;
15 private bool _disposed;
16
24 public OSDPCaptureFileWriter(string filePath, string source, bool append = true)
25 : this(filePath, source, new FileSystemAdapter(), append)
26 {
27 }
28
37 internal OSDPCaptureFileWriter(string filePath, string source, IFileSystem fileSystem, bool append = true)
38 {
39 if (string.IsNullOrEmpty(filePath))
40 throw new ArgumentNullException(nameof(filePath));
41 if (string.IsNullOrEmpty(source))
42 throw new ArgumentNullException(nameof(source));
43
44 var directory = fileSystem.GetDirectoryName(filePath);
45 if (!string.IsNullOrEmpty(directory))
46 fileSystem.CreateDirectory(directory);
47
48 _source = source;
49 _writer = fileSystem.CreateStreamWriter(filePath, append);
50 }
51
59 public void WritePacket(byte[] data, TraceDirection direction)
60 {
61 if (_disposed) throw new ObjectDisposedException(nameof(OSDPCaptureFileWriter));
62 if (data == null) throw new ArgumentNullException(nameof(data));
63
64 WritePacketInternal(data, direction, DateTime.UtcNow);
65 }
66
75 public void WritePacket(byte[] data, TraceDirection direction, DateTime timestamp)
76 {
77 if (_disposed) throw new ObjectDisposedException(nameof(OSDPCaptureFileWriter));
78 if (data == null) throw new ArgumentNullException(nameof(data));
79
80 WritePacketInternal(data, direction, timestamp);
81 }
82
89 public void WriteTrace(TraceEntry trace)
90 {
91 if (_disposed) throw new ObjectDisposedException(nameof(OSDPCaptureFileWriter));
92
93 WritePacketInternal(trace.Data, trace.Direction, DateTime.UtcNow);
94 }
95
96 private void WritePacketInternal(byte[] data, TraceDirection direction, DateTime timestamp)
97 {
98 var unixTime = timestamp.Subtract(new DateTime(1970, 1, 1));
99 long timeSec = (long)Math.Floor(unixTime.TotalSeconds);
100 long timeNano = (unixTime.Ticks - timeSec * TimeSpan.TicksPerSecond) * 100L;
101
102 string ioValue = direction switch
103 {
104 TraceDirection.Input => "input",
105 TraceDirection.Output => "output",
106 TraceDirection.Trace => "trace",
107 _ => "unknown"
108 };
109
110 var entry = new
111 {
112 timeSec = timeSec.ToString("F0"),
113 timeNano = timeNano.ToString("000000000"),
114 io = ioValue,
115 data = BitConverter.ToString(data),
116 osdpTraceVersion = "1",
117 osdpSource = _source
118 };
119
120 string json = JsonSerializer.Serialize(entry);
121 _writer.WriteLine(json);
122 _writer.Flush(); // Ensure data is written immediately for real-time monitoring
123 }
124
128 public void Dispose()
129 {
130 Dispose(true);
131 GC.SuppressFinalize(this);
132 }
133
138 protected virtual void Dispose(bool disposing)
139 {
140 if (!_disposed)
141 {
142 if (disposing)
143 {
144 _writer?.Dispose();
145 }
146 _disposed = true;
147 }
148 }
149}
void WriteTrace(TraceEntry trace)
Writes a trace entry to the capture file.
void WritePacket(byte[] data, TraceDirection direction, DateTime timestamp)
Writes a packet to the capture file with a specific timestamp.
void WritePacket(byte[] data, TraceDirection direction)
Writes a packet to the capture file.
virtual void Dispose(bool disposing)
Releases resources used by the OSDPCaptureFileWriter.
OSDPCaptureFileWriter(string filePath, string source, bool append=true)
Initializes a new instance of the OSDPCaptureFileWriter class.
void Dispose()
Releases all resources used by the OSDPCaptureFileWriter.
Abstraction for file system operations to enable testing without actual file I/O.
Definition IFileSystem.cs:9
StreamWriter CreateStreamWriter(string path, bool append)
Creates a StreamWriter for the specified file path.
void CreateDirectory(string path)
Creates all directories in the specified path.
string GetDirectoryName(string path)
Gets the directory name from a file path.
TraceDirection
Direction of trace.
Definition TraceEntry.cs:9
@ Output
Data is sent from the device.
Definition TraceEntry.cs:13
@ Input
Data is sent to the device.
Definition TraceEntry.cs:11
@ Trace
Data is being monitored.
Definition TraceEntry.cs:15
Represents low level data sent on the wire between the control panel and the devices.
Definition TraceEntry.cs:22
byte[] Data
The data that is sent/received.
Definition TraceEntry.cs:36
TraceDirection Direction
The direction in which the data is sent.
Definition TraceEntry.cs:26