Compare commits

..

1 Commits

3 changed files with 105 additions and 53 deletions

View File

@@ -3,7 +3,6 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Reflection;
using Newtonsoft.Json;
@@ -39,53 +38,6 @@ namespace PepperDash.Essentials.Core
}
}
private const string ParamsPattern = @"(?<=\[).*?(?=])";
private const string DevKeySpacePattern = @"(?<=\().*?(?=\))";
private const string MethodStringPattern = @"(?<=\)\s).*?(?=\s)";
private static readonly Regex ParamsRegex = new Regex(ParamsPattern);
private static readonly Regex DevKeySpaceRegex = new Regex(DevKeySpacePattern);
private static readonly Regex MethodStringRegex = new Regex(MethodStringPattern);
public static void BuildJsonForDeviceAction(string cmd)
{
if (String.IsNullOrEmpty(cmd))
{
CrestronConsole.ConsoleCommandResponse(
"Please provide some data matching the format '<deviceKey> <methodName> [parameters]' The parameters support strings, numbers, and booleans, and are optional. If your device key has spaces in it, wrap it in parenthesis");
}
try
{
var paramsMatch = ParamsRegex.Match(cmd);
var devKeySpaceMatch = DevKeySpaceRegex.Match(cmd);
var methodStringMatch = MethodStringRegex.Match(cmd);
var deviceString = devKeySpaceMatch.Value ?? string.Empty;
var methodString = methodStringMatch.Value ?? string.Empty;
var cmdParts = cmd.Split(' ');
if (cmdParts.Length <= 1)
{
CrestronConsole.ConsoleCommandResponse("Malformed 'deveasy' command - not enough arguments");
return;
}
deviceString = String.IsNullOrEmpty(deviceString) ? cmdParts[0] : deviceString;
methodString = String.IsNullOrEmpty(methodString) ? cmdParts[1] : methodString;
var action = new DeviceActionWrapper()
{
DeviceKey = deviceString,
MethodName = methodString,
Params = (paramsMatch != null) ? new object[] { paramsMatch.Value } : new object[] { }
};
DoDeviceAction(action);
}
catch (Exception ex)
{
CrestronConsole.ConsoleCommandResponse("Malformed 'deveasy' command - consult command help.");
}
}
/// <summary>

View File

@@ -39,13 +39,8 @@ namespace PepperDash.Essentials.Core
ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(ListDevices, "devlist", "Lists current managed devices",
ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.BuildJsonForDeviceAction, "deveasy",
"Easier way to build devjson commands",
ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(DeviceJsonApi.DoDeviceActionWithJson, "devjson", "",
ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetProperties(s)), "devprops", "", ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetMethods(s)), "devmethods", "", ConsoleAccessLevelEnum.AccessOperator);
CrestronConsole.AddNewConsoleCommand(s => CrestronConsole.ConsoleCommandResponse(DeviceJsonApi.GetApiMethods(s)), "apimethods", "", ConsoleAccessLevelEnum.AccessOperator);

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Crestron.SimplSharp;
namespace PepperDash.Essentials.Core
@@ -20,5 +21,109 @@ namespace PepperDash.Essentials.Core
{
return string.IsNullOrEmpty(s) ? newString : s;
}
/// <summary>
/// Formats string to meet specified parameters
/// </summary>
/// <param name="inputString">string to be formatted</param>
/// <param name="outputStringLength">length of output string</param>
/// <param name="padCharacter">character to pad string with to reach desired output length</param>
/// <param name="justification">Justification of input string with regards to the overall string</param>
/// <param name="separateInput">If true, seperate input string from pad characters with a single space</param>
/// <returns></returns>
public static string AutoPadAndJustify(this string inputString, int outputStringLength, char padCharacter,
AutoPadJustification justification, bool separateInput)
{
var returnString = inputString;
var justifiedAndSeparateLength = (
separateInput
? (justification == AutoPadJustification.Center ? 2 : 1)
: 0);
if (outputStringLength <= inputString.Length + justifiedAndSeparateLength) return returnString;
var fillLength = outputStringLength - inputString.Length - justifiedAndSeparateLength;
switch (justification)
{
case (AutoPadJustification.Left):
{
returnString =
inputString +
new string(' ', separateInput ? 1 : 0) +
new string(padCharacter, fillLength);
break;
}
case (AutoPadJustification.Right):
{
returnString =
new string(padCharacter, fillLength) +
new string(' ', separateInput ? 1 : 0) +
inputString;
break;
}
case (AutoPadJustification.Center):
{
var halfFill = fillLength / 2;
returnString =
new string(padCharacter, halfFill + (fillLength % 2)) +
new string(' ', separateInput ? 1 : 0) +
inputString +
new string(' ', separateInput ? 1 : 0) +
new string(padCharacter, halfFill);
break;
}
}
return returnString;
}
/// <summary>
/// Formats string to meet specified parameters
/// </summary>
/// <param name="inputString">string to be formatted</param>
/// <param name="options">String formatting options</param>
/// <returns></returns>
public static string AutoPadAndJustify(this string inputString, AutoPadJustificationOptions options)
{
if (options == null)
return inputString;
var outputStringLength = options.OutputStringLength;
var padCharacter = options.PadCharacter;
var justification = options.Justification;
var separateInput = options.SeparateInput;
return AutoPadAndJustify(inputString, outputStringLength, padCharacter, justification,
separateInput);
}
}
public enum AutoPadJustification
{
Center,
Left,
Right
}
/// <summary>
/// Options for setting AutoPadJustification
/// </summary>
public class AutoPadJustificationOptions
{
/// <summary>
/// Text Justification for the string, relative to the length
/// </summary>
public AutoPadJustification Justification { get; set; }
/// <summary>
/// If true, separate input string from pad characters by a single ' ' character
/// </summary>
public bool SeparateInput { get; set; }
/// <summary>
/// Pad character to be inserted
/// </summary>
public char PadCharacter { get; set; }
/// <summary>
/// Total length of the output string
/// </summary>
public int OutputStringLength { get; set; }
}
}