-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature: Export button * Feature: Export button * Feature: Export button * Feature: Export button * Feature: ExportFileType settings * Feature: Export dialog added * Feature: BitCalculator export * Feature: DiscoveryProtocol export data * Feature: IP Geolocation export added * Feature: Network Interface export data * Docs: #2626
- Loading branch information
1 parent
906b3d3
commit 5661b46
Showing
28 changed files
with
1,241 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
Source/NETworkManager.Models/Export/ExportManager.BitCaluclatorInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Linq; | ||
using NETworkManager.Models.Network; | ||
using Newtonsoft.Json; | ||
|
||
namespace NETworkManager.Models.Export; | ||
|
||
public static partial class ExportManager | ||
{ | ||
/// <summary> | ||
/// Method to export objects from type <see cref="BitCaluclatorInfo" /> 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{BitCaluclatorInfo}" /> to export.</param> | ||
public static void Export(string filePath, ExportFileType fileType, IReadOnlyList<BitCaluclatorInfo> 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="BitCaluclatorInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateCsv(IEnumerable<BitCaluclatorInfo> collection, string filePath) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
|
||
stringBuilder.AppendLine( | ||
$"{nameof(BitCaluclatorInfo.Bits)},{nameof(BitCaluclatorInfo.Bytes)},{nameof(BitCaluclatorInfo.Kilobits)},{nameof(BitCaluclatorInfo.Kilobytes)},{nameof(BitCaluclatorInfo.Megabits)},{nameof(BitCaluclatorInfo.Megabytes)},{nameof(BitCaluclatorInfo.Gigabits)},{nameof(BitCaluclatorInfo.Gigabytes)},{nameof(BitCaluclatorInfo.Terabits)},{nameof(BitCaluclatorInfo.Terabytes)},{nameof(BitCaluclatorInfo.Petabits)},{nameof(BitCaluclatorInfo.Petabytes)}"); | ||
|
||
foreach (var info in collection) | ||
stringBuilder.AppendLine( | ||
$"{info.Bits},{info.Bytes},{info.Kilobits},{info.Kilobytes},{info.Megabits},{info.Megabytes},{info.Gigabits},{info.Gigabytes},{info.Terabits},{info.Terabytes},{info.Petabits},{info.Petabytes}"); | ||
|
||
File.WriteAllText(filePath, stringBuilder.ToString()); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a XML file from the given <see cref="BitCaluclatorInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateXml(IEnumerable<BitCaluclatorInfo> collection, string filePath) | ||
{ | ||
var document = new XDocument(DefaultXDeclaration, | ||
new XElement(ApplicationName.BitCalculator.ToString(), | ||
new XElement(nameof(BitCaluclatorInfo) + "s", | ||
from info in collection | ||
select | ||
new XElement(nameof(BitCaluclatorInfo), | ||
new XElement(nameof(BitCaluclatorInfo.Bits), info.Bits), | ||
new XElement(nameof(BitCaluclatorInfo.Bytes), info.Bytes), | ||
new XElement(nameof(BitCaluclatorInfo.Kilobits), info.Kilobits), | ||
new XElement(nameof(BitCaluclatorInfo.Kilobytes), info.Kilobytes), | ||
new XElement(nameof(BitCaluclatorInfo.Megabits), info.Megabits), | ||
new XElement(nameof(BitCaluclatorInfo.Megabytes), info.Megabytes), | ||
new XElement(nameof(BitCaluclatorInfo.Gigabits), info.Gigabits), | ||
new XElement(nameof(BitCaluclatorInfo.Gigabytes), info.Gigabytes), | ||
new XElement(nameof(BitCaluclatorInfo.Terabits), info.Terabits), | ||
new XElement(nameof(BitCaluclatorInfo.Terabytes), info.Terabytes), | ||
new XElement(nameof(BitCaluclatorInfo.Petabits), info.Petabits), | ||
new XElement(nameof(BitCaluclatorInfo.Petabytes), info.Petabytes))))); | ||
|
||
document.Save(filePath); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a JSON file from the given <see cref="BitCaluclatorInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{BitCaluclatorInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateJson(IReadOnlyList<BitCaluclatorInfo> collection, string filePath) | ||
{ | ||
var jsonData = new object[collection.Count]; | ||
|
||
for (var i = 0; i < collection.Count; i++) | ||
jsonData[i] = new | ||
{ | ||
collection[i].Bits, | ||
collection[i].Bytes, | ||
collection[i].Kilobits, | ||
collection[i].Kilobytes, | ||
collection[i].Megabits, | ||
collection[i].Megabytes, | ||
collection[i].Gigabits, | ||
collection[i].Gigabytes, | ||
collection[i].Terabits, | ||
collection[i].Terabytes, | ||
collection[i].Petabits, | ||
collection[i].Petabytes | ||
}; | ||
|
||
File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented)); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
Source/NETworkManager.Models/Export/ExportManager.DiscoveryProtocolPackageInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Xml.Linq; | ||
using NETworkManager.Models.Network; | ||
using Newtonsoft.Json; | ||
|
||
namespace NETworkManager.Models.Export; | ||
|
||
public static partial class ExportManager | ||
{ | ||
/// <summary> | ||
/// Method to export objects from type <see cref="DiscoveryProtocolPackageInfo" /> 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{DiscoveryProtocolPackageInfo}" /> to export.</param> | ||
public static void Export(string filePath, ExportFileType fileType, | ||
IReadOnlyList<DiscoveryProtocolPackageInfo> 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="DiscoveryProtocolPackageInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateCsv(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
|
||
|
||
stringBuilder.AppendLine( | ||
$"{nameof(DiscoveryProtocolPackageInfo.Device)},{nameof(DiscoveryProtocolPackageInfo.DeviceDescription)},{nameof(DiscoveryProtocolPackageInfo.Port)},{nameof(DiscoveryProtocolPackageInfo.PortDescription)},{nameof(DiscoveryProtocolPackageInfo.Model)},{nameof(DiscoveryProtocolPackageInfo.VLAN)},{nameof(DiscoveryProtocolPackageInfo.IPAddress)},{nameof(DiscoveryProtocolPackageInfo.Protocol)},{nameof(DiscoveryProtocolPackageInfo.TimeToLive)}"); | ||
|
||
foreach (var info in collection) | ||
stringBuilder.AppendLine( | ||
$"{info.Device},{info.DeviceDescription},{info.Port},{info.PortDescription},{info.Model},{info.VLAN},{info.IPAddress},{info.Protocol},{info.TimeToLive}"); | ||
|
||
File.WriteAllText(filePath, stringBuilder.ToString()); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a XML file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateXml(IEnumerable<DiscoveryProtocolPackageInfo> collection, string filePath) | ||
{ | ||
var document = new XDocument(DefaultXDeclaration, | ||
new XElement(ApplicationName.DiscoveryProtocol.ToString(), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo) + "s", | ||
from info in collection | ||
select | ||
new XElement(nameof(DiscoveryProtocolPackageInfo), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.Device), info.Device), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.DeviceDescription), | ||
info.DeviceDescription), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.Port), info.Port), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.PortDescription), info.PortDescription), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.Model), info.Model), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.VLAN), info.VLAN), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.IPAddress), info.IPAddress), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.Protocol), info.Protocol), | ||
new XElement(nameof(DiscoveryProtocolPackageInfo.TimeToLive), info.TimeToLive))))); | ||
|
||
document.Save(filePath); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a JSON file from the given <see cref="DiscoveryProtocolPackageInfo" /> collection. | ||
/// </summary> | ||
/// <param name="collection">Objects as <see cref="IReadOnlyList{DiscoveryProtocolPackageInfo}" /> to export.</param> | ||
/// <param name="filePath">Path to the export file.</param> | ||
private static void CreateJson(IReadOnlyList<DiscoveryProtocolPackageInfo> collection, string filePath) | ||
{ | ||
var jsonData = new object[collection.Count]; | ||
|
||
for (var i = 0; i < collection.Count; i++) | ||
jsonData[i] = new | ||
{ | ||
collection[i].Device, | ||
collection[i].DeviceDescription, | ||
collection[i].Port, | ||
collection[i].PortDescription, | ||
collection[i].Model, | ||
collection[i].VLAN, | ||
collection[i].IPAddress, | ||
collection[i].Protocol, | ||
collection[i].TimeToLive | ||
}; | ||
|
||
File.WriteAllText(filePath, JsonConvert.SerializeObject(jsonData, Formatting.Indented)); | ||
} | ||
} |
Oops, something went wrong.