Skip to content

Commit fc9e56d

Browse files
committed
Refactor SNMP polling and generalize methods
1 parent a8ec5cc commit fc9e56d

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

Protest/Protocols/Snmp.Polling.Switch.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ internal static partial class Polling {
1414
internal static byte[] GetInterfaces(Dictionary<string, string> parameters) {
1515
if (parameters is null) return Data.CODE_INVALID_ARGUMENT.Array;
1616

17-
parameters.TryGetValue("file", out string file);
18-
if (string.IsNullOrEmpty(file)) {
17+
if (!parameters.TryGetValue("file", out string file)) {
1918
return Data.CODE_INVALID_ARGUMENT.Array;
2019
}
2120

@@ -113,7 +112,7 @@ private static PortDir[] ComputeInterface(IList<Variable> list, Dictionary<strin
113112
if ((b & (1 << (7 - k))) == 0) continue;
114113

115114
int portIndex = 8 * (j - startIndex) + (k + 1);
116-
if (!taggedMap.TryGetValue(vlanId, out var ports)) {
115+
if (!taggedMap.TryGetValue(vlanId, out List<int> ports)) {
117116
ports = new List<int>();
118117
taggedMap[vlanId] = ports;
119118
}
@@ -126,7 +125,7 @@ private static PortDir[] ComputeInterface(IList<Variable> list, Dictionary<strin
126125

127126
foreach (KeyValuePair<short, List<int>> pair in taggedMap) {
128127
foreach (int port in pair.Value) {
129-
tagged.TryGetValue(port, out var existing);
128+
tagged.TryGetValue(port, out string existing);
130129
tagged[port] = string.IsNullOrEmpty(existing) ? pair.Key.ToString() : $"{existing},{pair.Key.ToString()}";
131130
}
132131
}
@@ -197,7 +196,7 @@ private static string SwitchInterfaces_FetchFormat(PortDir type, PortDir descrip
197196
n = "vertical"
198197
});
199198

200-
private static string PortTypeToString(string value) =>
199+
internal static string PortTypeToString(string value) =>
201200
value switch {
202201
"10000" => "SFP+",
203202
"25000" => "SFP+",
@@ -209,7 +208,7 @@ private static string PortTypeToString(string value) =>
209208
_ => "Ethernet"
210209
};
211210

212-
private static string PortSpeedToString(string value) =>
211+
internal static string PortSpeedToString(string value) =>
213212
value switch {
214213
"10" => "10 Mbps",
215214
"100" => "100 Mbps",
@@ -225,5 +224,4 @@ private static string PortSpeedToString(string value) =>
225224
"800000" => "800 Gbps",
226225
_ => "N/A"
227226
};
228-
229227
}

Protest/Tools/LiveStats.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ internal static class LiveStats {
2424
private static readonly string[] PRINTER_TYPES = new string[] { "fax", "multiprinter", "ticket printer", "printer" };
2525
private static readonly string[] SWITCH_TYPES = new string[] { "switch", "router", "firewall" };
2626

27-
private static Dictionary<string, string> speedMap = new Dictionary<string, string> {
28-
["10"] = "10 Mbps",
29-
["100"] = "100 Mbps",
30-
["1000"] = "1 Gbps",
31-
["2500"] = "2.5 Gbps",
32-
["5000"] = "5 Gbps",
33-
["10000"] = "10 Gbps",
34-
["25000"] = "25 Gbps",
35-
["40000"] = "40 Gbps",
36-
["100000"] = "100 Gbps",
37-
["200000"] = "200 Gbps",
38-
["400000"] = "400 Gbps",
39-
["800000"] = "800 Gbps"
40-
};
41-
4227
private static void WsWriteText(WebSocket ws, [StringSyntax(StringSyntaxAttribute.Json)] string text, Lock mutex) {
4328
lock (mutex) {
4429
WsWriteText(ws, Encoding.UTF8.GetBytes(text), mutex);
@@ -419,8 +404,7 @@ private static void SnmpQueryPrinter(WebSocket ws, Lock mutex, IPAddress ipAddre
419404

420405
if (printerFormatted is not null) {
421406
if (printerFormatted.TryGetValue(Protocols.Snmp.Oid.PRINTER_STATUS, out string snmpPrinterStatus)) {
422-
string status = snmpPrinterStatus switch
423-
{
407+
string status = snmpPrinterStatus switch {
424408
"1" => "Other",
425409
"2" => "Processing",
426410
"3" => "Idle",
@@ -460,7 +444,6 @@ private static void SnmpQuerySwitch(WebSocket ws, Lock mutex, IPAddress ipAddres
460444
}
461445

462446
Dictionary<string, string> parsedResult = Protocols.Snmp.Polling.ParseResponse(result);
463-
464447
if (parsedResult is null) {
465448
WsWriteText(ws, "{\"switchInfo\":{\"success\":false}}"u8.ToArray(), mutex);
466449
return;
@@ -498,7 +481,7 @@ private static void SnmpQuerySwitch(WebSocket ws, Lock mutex, IPAddress ipAddres
498481
for (int k = 0; k < 8; k++) {
499482
if ((b & (1 << (7 - k))) != 0) {
500483
int portIndex = 8 * (j - startIndex) + (k + 1);
501-
if (!taggedMap.TryGetValue(vlanId, out var ports)) {
484+
if (!taggedMap.TryGetValue(vlanId, out List<int> ports)) {
502485
ports = new List<int>();
503486
taggedMap[vlanId] = ports;
504487
}
@@ -512,7 +495,7 @@ private static void SnmpQuerySwitch(WebSocket ws, Lock mutex, IPAddress ipAddres
512495

513496
foreach (KeyValuePair<short, List<int>> pair in taggedMap) {
514497
foreach (int port in pair.Value) {
515-
taggedDic.TryGetValue(port, out var existing);
498+
taggedDic.TryGetValue(port, out string existing);
516499
taggedDic[port] = string.IsNullOrEmpty(existing) ? pair.Key.ToString() : $"{existing},{pair.Key.ToString()}";
517500
}
518501
}
@@ -572,7 +555,7 @@ private static void SnmpQuerySwitch(WebSocket ws, Lock mutex, IPAddress ipAddres
572555
if (pair.Value != "6") continue; //physical ports only
573556

574557
string rawSpeed = speedDic.TryGetValue(pair.Key, out string s) ? s : "N/A";
575-
speedList.Add(speedMap.TryGetValue(rawSpeed, out string readableSpeed) ? readableSpeed : "N/A");
558+
speedList.Add(PortSpeedToString(rawSpeed));
576559

577560
untaggedList.Add(untaggedDic.TryGetValue(pair.Key, out string untaggedVlan) ? untaggedVlan : "1");
578561
taggedList.Add(taggedDic.TryGetValue(pair.Key, out string taggedVlan) ? taggedVlan : "1");

0 commit comments

Comments
 (0)