Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Chris Cameron
2017-07-28 12:08:08 -04:00
11 changed files with 228 additions and 80 deletions

View File

@@ -1,39 +0,0 @@
using System;
using ICD.Common.Properties;
namespace ICD.Common.Attributes.Properties
{
/// <summary>
/// Provides information on a settings property.
/// </summary>
[PublicAPI]
public sealed class SettingsProperty : Attribute
{
public enum ePropertyType
{
[PublicAPI] Default,
[PublicAPI] Hidden,
[PublicAPI] PortId,
[PublicAPI] DeviceId,
[PublicAPI] Ipid,
[PublicAPI] Enum
}
private readonly ePropertyType m_PropertyType;
/// <summary>
/// Gets the property type.
/// </summary>
[PublicAPI]
public ePropertyType PropertyType { get { return m_PropertyType; } }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="propertyType"></param>
public SettingsProperty(ePropertyType propertyType)
{
m_PropertyType = propertyType;
}
}
}

View File

@@ -53,12 +53,12 @@ namespace ICD.Common.Utils.Extensions
/// <param name="extends"></param> /// <param name="extends"></param>
/// <param name="item">Outputs the first item in the sequence.</param> /// <param name="item">Outputs the first item in the sequence.</param>
/// <returns></returns> /// <returns></returns>
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, out T item) public static bool TryFirst<T>(this IEnumerable<T> extends, out T item)
{ {
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");
return extends.TryFirstOrDefault(i => true, out item); return extends.TryFirst(i => true, out item);
} }
/// <summary> /// <summary>
@@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions
/// <param name="predicate"></param> /// <param name="predicate"></param>
/// <param name="item">Outputs the first item in the sequence.</param> /// <param name="item">Outputs the first item in the sequence.</param>
/// <returns></returns> /// <returns></returns>
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item) public static bool TryFirst<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
{ {
if (extends == null) if (extends == null)
throw new ArgumentNullException("extends"); throw new ArgumentNullException("extends");

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using ICD.Common.Properties;
namespace ICD.Common.Utils.Extensions
{
/// <summary>
/// Extension methods for working with Lists.
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Adds the item into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="item"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, T item)
{
if (extends == null)
throw new ArgumentNullException("extends");
extends.AddSorted(item, Comparer<T>.Default);
}
/// <summary>
/// Adds the item into a sorted list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="item"></param>
/// <param name="comparer"></param>
[PublicAPI]
public static void AddSorted<T>(this List<T> extends, T item, IComparer<T> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (comparer == null)
throw new ArgumentNullException("comparer");
if (extends.Count == 0)
{
extends.Add(item);
return;
}
if (comparer.Compare(extends[extends.Count - 1], item) <= 0)
{
extends.Add(item);
return;
}
if (comparer.Compare(extends[0], item) >= 0)
{
extends.Insert(0, item);
return;
}
int index = extends.BinarySearch(item, comparer);
if (index < 0)
index = ~index;
extends.Insert(index, item);
}
}
}

View File

@@ -71,7 +71,6 @@
<ItemGroup> <ItemGroup>
<Compile Include="Attributes\AbstractIcdAttribute.cs" /> <Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Attributes\KrangPluginAttribute.cs" /> <Compile Include="Attributes\KrangPluginAttribute.cs" />
<Compile Include="Attributes\Properties\SettingsProperty.cs" />
<Compile Include="Attributes\Rpc\RpcAttribute.cs" /> <Compile Include="Attributes\Rpc\RpcAttribute.cs" />
<Compile Include="EventArguments\BoolEventArgs.cs" /> <Compile Include="EventArguments\BoolEventArgs.cs" />
<Compile Include="EventArguments\CharEventArgs.cs" /> <Compile Include="EventArguments\CharEventArgs.cs" />
@@ -85,6 +84,7 @@
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" /> <Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
<None Include="ObfuscationSettings.cs" /> <None Include="ObfuscationSettings.cs" />
<Compile Include="Extensions\ByteExtensions.cs" /> <Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="Properties\Annotations.cs" /> <Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\Logging\ILoggerService.cs" /> <Compile Include="Services\Logging\ILoggerService.cs" />

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using ICD.Common.Properties; using ICD.Common.Properties;
#if SIMPLSHARP #if SIMPLSHARP
using Crestron.SimplSharp; using Crestron.SimplSharp;
@@ -34,7 +35,8 @@ namespace ICD.Common.Utils
[PublicAPI] [PublicAPI]
public static void ConsoleCommandResponse(string message, params object[] args) public static void ConsoleCommandResponse(string message, params object[] args)
{ {
message = string.Format(message, args); if(args != null && args.Any())
message = string.Format(message, args);
#if SIMPLSHARP #if SIMPLSHARP
try try

View File

@@ -147,29 +147,29 @@ namespace ICD.Common.Utils
{ {
Dictionary<string, string> output = new Dictionary<string, string>(); Dictionary<string, string> output = new Dictionary<string, string>();
try string progInfo = string.Empty;
string command = string.Format("progcomments:{0}", ProgramNumber);
if (!IcdConsole.SendControlSystemCommand(command, ref progInfo))
{ {
string progInfo = string.Empty; ServiceProvider.GetService<ILoggerService>().AddEntry(eSeverity.Warning, "Failed to parse prog comments");
string command = string.Format("progcomments:{0}", ProgramNumber); return output;
if (!IcdConsole.SendControlSystemCommand(command, ref progInfo))
{
ServiceProvider.TryGetService<ILoggerService>().AddEntry(eSeverity.Warning, "Failed to parse prog comments");
return output;
}
foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"}))
{
string[] pair = line.Split(':', 2).ToArray();
string key = pair[0].Trim();
string value = pair[1].Trim();
output[key] = value;
}
} }
catch (Exception e)
foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"}))
{ {
ServiceProvider.TryGetService<ILoggerService>() string[] pair = line.Split(':', 2).ToArray();
.AddEntry(eSeverity.Error, e, "Failed to parse prog comments - {0}", e.Message);
if (pair.Length < 2)
{
ServiceProvider.GetService<ILoggerService>()
.AddEntry(eSeverity.Warning, "Failed to parse prog comments line - {0}", line);
continue;
}
string key = pair[0].Trim();
string value = pair[1].Trim();
output[key] = value;
} }
return output; return output;

View File

@@ -516,5 +516,17 @@ namespace ICD.Common.Utils
return builder.ToString(); return builder.ToString();
} }
/// <summary>
/// Shim of value.ToString()
/// Returns null if input value is null.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[PublicAPI, CanBeNull]
public static string Trim(string value)
{
return value == null ? null : value.ToUpper();
}
} }
} }

View File

@@ -28,7 +28,7 @@ namespace ICD.Common.Utils.Xml
} }
catch (XmlException e) catch (XmlException e)
{ {
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); throw new IcdXmlException(e);
} }
} }

View File

@@ -1,4 +1,9 @@
using System; using System;
#if SIMPLSHARP
using Crestron.SimplSharp;
#else
using System.Xml;
#endif
namespace ICD.Common.Utils.Xml namespace ICD.Common.Utils.Xml
{ {
@@ -20,5 +25,14 @@ namespace ICD.Common.Utils.Xml
m_LineNumber = lineNumber; m_LineNumber = lineNumber;
m_LinePosition = linePosition; m_LinePosition = linePosition;
} }
/// <summary>
/// Constructor.
/// </summary>
/// <param name="inner"></param>
public IcdXmlException(XmlException inner)
: this(inner.Message, inner, inner.LineNumber, inner.LinePosition)
{
}
} }
} }

View File

@@ -50,26 +50,54 @@ namespace ICD.Common.Utils.Xml
public bool MoveToNextAttribute() public bool MoveToNextAttribute()
{ {
return m_Reader.MoveToNextAttribute(); try
{
return m_Reader.MoveToNextAttribute();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public void MoveToElement() public void MoveToElement()
{ {
m_Reader.MoveToElement(); try
{
m_Reader.MoveToElement();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public string GetAttribute(string name) public string GetAttribute(string name)
{ {
return m_Reader.GetAttribute(name); try
{
return m_Reader.GetAttribute(name);
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public string ReadString() public string ReadString()
{ {
try
{
#if SIMPLSHARP #if SIMPLSHARP
return m_Reader.ReadString(); return m_Reader.ReadString();
#else #else
return m_Reader.ReadElementContentAsString(); return m_Reader.ReadElementContentAsString();
#endif #endif
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public bool Read() public bool Read()
@@ -80,47 +108,96 @@ namespace ICD.Common.Utils.Xml
} }
catch (XmlException e) catch (XmlException e)
{ {
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); throw new IcdXmlException(e);
} }
} }
public void Dispose() public void Dispose()
{ {
try
{
#if SIMPLSHARP #if SIMPLSHARP
m_Reader.Dispose(true); m_Reader.Dispose(true);
#else #else
m_Reader.Dispose(); m_Reader.Dispose();
#endif #endif
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public void Skip() public void Skip()
{ {
m_Reader.Skip(); try
{
m_Reader.Skip();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public string ReadElementContentAsString() public string ReadElementContentAsString()
{ {
return m_Reader.ReadElementContentAsString(); try
{
return m_Reader.ReadElementContentAsString();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public string ReadOuterXml() public string ReadOuterXml()
{ {
return m_Reader.ReadOuterXml(); try
{
return m_Reader.ReadOuterXml();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public string ReadInnerXml() public string ReadInnerXml()
{ {
return m_Reader.ReadInnerXml(); try
{
return m_Reader.ReadInnerXml();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public long ReadElementContentAsLong() public long ReadElementContentAsLong()
{ {
return m_Reader.ReadElementContentAsLong(); try
{
return m_Reader.ReadElementContentAsLong();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
public float ReadElementContentAsFloat() public float ReadElementContentAsFloat()
{ {
return m_Reader.ReadElementContentAsFloat(); try
{
return m_Reader.ReadElementContentAsFloat();
}
catch (XmlException e)
{
throw new IcdXmlException(e);
}
} }
#endregion #endregion

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using ICD.Common.EventArguments; using ICD.Common.EventArguments;
using ICD.Common.Properties; using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
using ICD.Common.Utils.IO; using ICD.Common.Utils.IO;
namespace ICD.Common.Utils.Xml namespace ICD.Common.Utils.Xml
@@ -67,7 +68,11 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static IcdXmlAttribute GetAttribute(string xml, string name) public static IcdXmlAttribute GetAttribute(string xml, string name)
{ {
return GetAttributes(xml).First(a => a.Name == name); IcdXmlAttribute output;
if (GetAttributes(xml).TryFirst(a => a.Name == name, out output))
return output;
throw new KeyNotFoundException(string.Format("No attribute with name {0}", name));
} }
/// <summary> /// <summary>
@@ -130,6 +135,9 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback) public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback)
{ {
if (callback == null)
throw new ArgumentNullException("callback");
Recurse(xml, new Stack<string>(), callback); Recurse(xml, new Stack<string>(), callback);
} }
@@ -141,6 +149,12 @@ namespace ICD.Common.Utils.Xml
/// <param name="callback"></param> /// <param name="callback"></param>
private static void Recurse(string xml, Stack<string> path, Action<XmlRecursionEventArgs> callback) private static void Recurse(string xml, Stack<string> path, Action<XmlRecursionEventArgs> callback)
{ {
if (path == null)
throw new ArgumentNullException("path");
if (callback == null)
throw new ArgumentNullException("callback");
IcdXmlReader childReader; IcdXmlReader childReader;
try try