Skip to content

Commit

Permalink
Merge branch 'development' into hotfix/pdu-bridge-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorin authored Aug 21, 2023
2 parents 4d2ce83 + d95692b commit 44296cb
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PepperDash.Core;
using Crestron.SimplSharp;
using PepperDash.Essentials.Core;

namespace PepperDash.Essentials.Core.DeviceInfo
{
public static class NetworkDeviceHelpers
{
/// <summary>
/// Event raised when ArpTable changes
/// </summary>
public static event ArpTableEventHandler ArpTableUpdated;

/// <summary>
/// Delegate called by ArpTableUpdated
/// </summary>
/// <param name="args">contains the entire ARP table and a bool to note if there was an error in retrieving the data</param>
public delegate void ArpTableEventHandler(ArpTableEventArgs args);

private static readonly char NewLineSplitter = CrestronEnvironment.NewLine.ToCharArray().First();
private static readonly string NewLine = CrestronEnvironment.NewLine;

private static readonly CCriticalSection Lock = new CCriticalSection();

/// <summary>
/// Last resolved ARP table - it is recommended to refresh the arp before using this.
/// </summary>
public static List<ArpEntry> ArpTable { get; private set; }

/// <summary>
/// Force recheck of ARP table
/// </summary>
public static void RefreshArp()
{
var error = false;
try
{
Lock.Enter();
var consoleResponse = string.Empty;
if (!CrestronConsole.SendControlSystemCommand("showarptable", ref consoleResponse)) return;
if (string.IsNullOrEmpty(consoleResponse))
{
error = true;
return;
}
ArpTable.Clear();

Debug.Console(2, "ConsoleResponse of 'showarptable' : {0}{1}", NewLine, consoleResponse);

var myLines =
consoleResponse.Split(NewLineSplitter)
.ToList()
.Where(o => (o.Contains(':') && !o.Contains("Type", StringComparison.OrdinalIgnoreCase)))
.ToList();
foreach (var line in myLines)
{
var item = line;
var seperator = item.Contains('\t') ? '\t' : ' ';
var dataPoints = item.Split(seperator);
if (dataPoints == null || dataPoints.Length < 2) continue;
var ipAddress = SanitizeIpAddress(dataPoints.First().TrimAll());
var macAddress = dataPoints.Last();
ArpTable.Add(new ArpEntry(ipAddress, macAddress));
}
}
catch (Exception ex)
{
Debug.Console(0, "Exception in \"RefreshArp\" : {0}", ex.Message);
error = true;
}
finally
{
Lock.Leave();
OnArpTableUpdated(new ArpTableEventArgs(ArpTable, error));
}
}


private static void OnArpTableUpdated(ArpTableEventArgs args)
{
if (args == null) return;
var handler = ArpTableUpdated;
if (handler == null) return;
handler.Invoke(args);
}

static NetworkDeviceHelpers()
{
ArpTable = new List<ArpEntry>();
}

/// <summary>
/// Removes leading zeros, leading whitespace, and trailing whitespace from an IPAddress string
/// </summary>
/// <param name="ipAddressIn">Ip Address to Santitize</param>
/// <returns>Sanitized Ip Address</returns>
public static string SanitizeIpAddress(string ipAddressIn)
{
try
{
var ipAddress = IPAddress.Parse(ipAddressIn.TrimStart('0'));
return ipAddress.ToString();
}
catch (Exception ex)
{
Debug.Console(0, "Unable to Santize Ip : {0}", ex.Message);
return ipAddressIn;
}
}

/// <summary>
/// Resolves a hostname by IP Address using DNS
/// </summary>
/// <param name="ipAddress">IP Address to resolve from</param>
/// <returns>Resolved Hostname - on failure to determine hostname, will return IP Address</returns>
public static string ResolveHostnameFromIp(string ipAddress)
{
try
{
var santitizedIp = SanitizeIpAddress(ipAddress);
var hostEntry = Dns.GetHostEntry(santitizedIp);
return hostEntry == null ? ipAddress : hostEntry.HostName;
}
catch (Exception ex)
{
Debug.Console(0, "Exception Resolving Hostname from IP Address : {0}", ex.Message);
return ipAddress;
}
}

/// <summary>
/// Resolves an IP Address by hostname using DNS
/// </summary>
/// <param name="hostName">Hostname to resolve from</param>
/// <returns>Resolved IP Address - on a failure to determine IP Address, will return hostname</returns>
public static string ResolveIpFromHostname(string hostName)
{
try
{
var hostEntry = Dns.GetHostEntry(hostName);
return hostEntry == null ? hostName : hostEntry.AddressList.First().ToString();
}
catch (Exception ex)
{
Debug.Console(0, "Exception Resolving IP Address from Hostname : {0}", ex.Message);
return hostName;
}
}

}

/// <summary>
/// Object to hold data about an arp entry
/// </summary>
public class ArpEntry
{
public readonly IPAddress IpAddress;
public readonly string MacAddress;

/// <summary>
/// Constructs new ArpEntry object
/// </summary>
/// <param name="ipAddress">string formatted as ipv4 address</param>
/// <param name="macAddress">mac address string - format is unimportant</param>
public ArpEntry(string ipAddress, string macAddress)
{
if (string.IsNullOrEmpty(ipAddress))
{
throw new ArgumentException("\"ipAddress\" cannot be null or empty");
}
if (string.IsNullOrEmpty(macAddress))
{
throw new ArgumentException("\"macAddress\" cannot be null or empty");
}
IpAddress = IPAddress.Parse(ipAddress.TrimStart().TrimStart('0').TrimEnd());
MacAddress = macAddress;
}
}

/// <summary>
/// Arguments passed by the ArpTableUpdated event
/// </summary>
public class ArpTableEventArgs : EventArgs
{
/// <summary>
/// The retrieved ARP Table
/// </summary>
public readonly List<ArpEntry> ArpTable;
/// <summary>
/// True if there was a problem retrieving the ARP Table
/// </summary>
public readonly bool Error;

/// <summary>
/// Constructor for ArpTableEventArgs
/// </summary>
/// <param name="arpTable">The entirety of the retrieved ARP table</param>
/// <param name="error">True of an error was encountered updating the ARP table</param>
public ArpTableEventArgs(List<ArpEntry> arpTable, bool error)
{
ArpTable = arpTable;
Error = error;
}

/// <summary>
/// Constructor for ArpTableEventArgs - assumes no error encountered in retrieving ARP Table
/// </summary>
/// <param name="arpTable">The entirety of the retrieved ARP table</param>
public ArpTableEventArgs(List<ArpEntry> arpTable)
{
ArpTable = arpTable;
Error = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -8,17 +9,70 @@ namespace PepperDash.Essentials.Core
{
public static class StringExtensions
{
/// <summary>
/// Returns null if a string is empty, otherwise returns the string
/// </summary>
/// <param name="s">string input</param>
/// <returns>null if the string is emtpy, otherwise returns the string</returns>
public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}

/// <summary>
/// Returns null if a string is empty or made of only whitespace characters, otherwise returns the string
/// </summary>
/// <param name="s">string input</param>
/// <returns>null if the string is wempty or made of only whitespace characters, otherwise returns the string</returns>
public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrEmpty(s.Trim()) ? null : s;
}

/// <summary>
/// Returns a replacement string if the input string is empty or made of only whitespace characters, otherwise returns the input string
/// </summary>
/// <param name="s">input string</param>
/// <param name="newString">string to replace with if input string is empty or whitespace</param>
/// <returns>returns newString if s is null, emtpy, or made of whitespace characters, otherwise returns s</returns>
public static string ReplaceIfNullOrEmpty(this string s, string newString)
{
return string.IsNullOrEmpty(s) ? newString : s;
}

/// <summary>
/// Overload for Contains that allows setting an explicit String Comparison
/// </summary>
/// <param name="source">Source String</param>
/// <param name="toCheck">String to check in Source String</param>
/// <param name="comp">Comparison parameters</param>
/// <returns>true of string contains "toCheck"</returns>
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
if (string.IsNullOrEmpty(source)) return false;
return source.IndexOf(toCheck, comp) >= 0;
}

/// <summary>
/// Performs TrimStart() and TrimEnd() on source string
/// </summary>
/// <param name="source">String to Trim</param>
/// <returns>Trimmed String</returns>
public static string TrimAll(this string source)
{
return string.IsNullOrEmpty(source) ? string.Empty : source.TrimStart().TrimEnd();
}

/// <summary>
/// Performs TrimStart(chars char[]) and TrimEnd(chars char[]) on source string.
/// </summary>
/// <param name="source">String to Trim</param>
/// <param name="chars">Char Array to trim from string</param>
/// <returns>Trimmed String</returns>
public static string TrimAll(this string source, char[] chars)
{
return string.IsNullOrEmpty(source) ? string.Empty : source.TrimStart(chars).TrimEnd(chars);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
<Compile Include="Crestron IO\Relay\GenericRelayDevice.cs" />
<Compile Include="Crestron IO\Relay\ISwitchedOutput.cs" />
<Compile Include="Crestron IO\StatusSign\StatusSignController.cs" />
<Compile Include="Device Info\NetworkDeviceHelpers.cs" />
<Compile Include="Devices\PowerInterfaces.cs" />
<Compile Include="Web\RequestHandlers\AppDebugRequestHandler.cs" />
<Compile Include="Web\RequestHandlers\GetFeedbacksForDeviceRequestHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum eRoutingSignalType
public enum eRoutingPortConnectionType
{
None, BackplaneOnly, DisplayPort, Dvi, Hdmi, Rgb, Vga, LineAudio, DigitalAudio, Sdi,
Composite, Component, DmCat, DmMmFiber, DmSmFiber, Speaker, Streaming
Composite, Component, DmCat, DmMmFiber, DmSmFiber, Speaker, Streaming, UsbC, HdBaseT
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,45 @@ public class RoutingPortNames
/// MediaPlayer
/// </summary>
public const string MediaPlayer = "mediaPlayer";
}
/// <summary>
/// UsbCIn
/// </summary>
public const string UsbCIn = "usbCIn";
/// <summary>
/// UsbCIn1
/// </summary>
public const string UsbCIn1 = "usbCIn1";
/// <summary>
/// UsbCIn2
/// </summary>
public const string UsbCIn2 = "usbCIn2";
/// <summary>
/// UsbCIn3
/// </summary>
public const string UsbCIn3 = "usbCIn3";
/// <summary>
/// UsbCOut
/// </summary>
public const string UsbCOut = "usbCOut";
/// <summary>
/// UsbCOut1
/// </summary>
public const string UsbCOut1 = "usbCOut1";
/// <summary>
/// UsbCOut2
/// </summary>
public const string UsbCOut2 = "usbCOut2";
/// <summary>
/// UsbCOut3
/// </summary>
public const string UsbCOut3 = "usbCOut3";
/// <summary>
/// HdBaseTIn
/// </summary>
public const string HdBaseTIn = "hdBaseTIn";
/// <summary>
/// HdBaseTOut
/// </summary>
public const string HdBaseTOut = "hdBaseTOut";
}
}

0 comments on commit 44296cb

Please sign in to comment.