-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathExportManager.SNTPLookupResultInfo.cs
More file actions
111 lines (100 loc) · 5.52 KB
/
ExportManager.SNTPLookupResultInfo.cs
File metadata and controls
111 lines (100 loc) · 5.52 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
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using NETworkManager.Models.Network;
using NETworkManager.Utilities;
using Newtonsoft.Json;
namespace NETworkManager.Models.Export;
public static partial class ExportManager
{
/// <summary>
/// Method to export objects from type <see cref="SNTPLookupInfo" /> to a file.
/// </summary>
/// <param name="filePath">Path to the export file.</param>
/// <param name="fileType">Allowed <see cref="ExportFileType" /> are CSV, XML or JSON.</param>
/// <param name="collection">Objects as <see cref="IReadOnlyList{SNTPLookupInfo}" /> to export.</param>
public static void Export(string filePath, ExportFileType fileType,
IReadOnlyList<SNTPLookupInfo> collection)
{
switch (fileType)
{
case ExportFileType.Csv:
CreateCsv(collection, filePath);
break;
case ExportFileType.Xml:
CreateXml(collection, filePath);
break;
case ExportFileType.Json:
CreateJson(collection, filePath);
break;
case ExportFileType.Txt:
default:
throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
}
}
/// <summary>
/// Creates a CSV file from the given <see cref="SNTPLookupInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{SNTPLookupInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateCsv(IEnumerable<SNTPLookupInfo> collection, string filePath)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(
$"{nameof(SNTPLookupInfo.Server)},{nameof(SNTPLookupInfo.IPEndPoint)},{nameof(SNTPLookupInfo.DateTime.NetworkTime)},{nameof(SNTPLookupInfo.DateTime.LocalStartTime)},{nameof(SNTPLookupInfo.DateTime.LocalEndTime)},{nameof(SNTPLookupInfo.DateTime.Offset)},{nameof(SNTPLookupInfo.DateTime.RoundTripDelay)}");
foreach (var info in collection)
stringBuilder.AppendLine(
$"{info.Server},{info.IPEndPoint},{DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.NetworkTime)},{DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.LocalStartTime)},{DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.LocalEndTime)},{info.DateTime.Offset} s,{info.DateTime.RoundTripDelay} ms");
File.WriteAllText(filePath, stringBuilder.ToString());
}
/// <summary>
/// Creates a XML file from the given <see cref="SNTPLookupInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{SNTPLookupInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateXml(IEnumerable<SNTPLookupInfo> collection, string filePath)
{
var document = new XDocument(DefaultXDeclaration,
new XElement(ApplicationName.SNTPLookup.ToString(),
new XElement(nameof(SNTPLookupInfo) + "s",
from info in collection
select
new XElement(nameof(SNTPLookupInfo),
new XElement(nameof(SNTPLookupInfo.Server), info.Server),
new XElement(nameof(SNTPLookupInfo.IPEndPoint), info.IPEndPoint),
new XElement(nameof(SNTPLookupInfo.DateTime.NetworkTime),
DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.NetworkTime)),
new XElement(nameof(SNTPLookupInfo.DateTime.LocalStartTime),
DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.LocalStartTime)),
new XElement(nameof(SNTPLookupInfo.DateTime.LocalEndTime),
DateTimeHelper.DateTimeToFullDateTimeString(info.DateTime.LocalEndTime)),
new XElement(nameof(SNTPLookupInfo.DateTime.Offset), $"{info.DateTime.Offset} s"),
new XElement(nameof(SNTPLookupInfo.DateTime.RoundTripDelay),
$"{info.DateTime.RoundTripDelay} ms")))));
document.Save(filePath);
}
/// <summary>
/// Creates a JSON file from the given <see cref="SNTPLookupInfo" /> collection.
/// </summary>
/// <param name="collection">Objects as <see cref="IReadOnlyList{SNTPLookupInfo}" /> to export.</param>
/// <param name="filePath">Path to the export file.</param>
private static void CreateJson(IReadOnlyList<SNTPLookupInfo> collection, string filePath)
{
var jsonData = new object[collection.Count];
for (var i = 0; i < collection.Count; i++)
jsonData[i] = new
{
collection[i].Server,
collection[i].IPEndPoint,
NetworkTime = DateTimeHelper.DateTimeToFullDateTimeString(collection[i].DateTime.NetworkTime),
LocalStartTime = DateTimeHelper.DateTimeToFullDateTimeString(collection[i].DateTime.LocalStartTime),
LocalEndTime = DateTimeHelper.DateTimeToFullDateTimeString(collection[i].DateTime.LocalEndTime),
Offset = $"{collection[i].DateTime.Offset} s",
RoundTripDelay = $"{collection[i].DateTime.RoundTripDelay} ms"
};
File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented));
}
}