Skip to content

Commit

Permalink
Commands: add logging to validation and allow regex matching
Browse files Browse the repository at this point in the history
fixes #288
  • Loading branch information
dasgarner committed Jun 13, 2023
1 parent db7027d commit b00d38d
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions Action/Command.cs
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2022 Xibo Signage Ltd
* Copyright (C) 2023 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
Expand All @@ -21,7 +21,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Shapes;

namespace XiboClient.Action
{
Expand Down Expand Up @@ -52,6 +54,36 @@ public bool IsValidationRequired()
return !string.IsNullOrEmpty(Validation);
}

/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private bool IsValid(string value)
{
LogMessage.Audit("Command", "IsValid", "Testing if " + Code + " is valid, output to test is [" + value + "]");

// Do we need to validate?
if (IsValidationRequired())
{
// Is the validation string a regex.
try
{
Match match = Regex.Match(value, Validation);
return match.Success;
}
catch
{
// Fallback to a string comparison
return value.Contains(Validation);
}
}
else
{
return true;
}
}

/// <summary>
/// Run the Command
/// </summary>
Expand All @@ -67,14 +99,7 @@ public bool Run()
Rs232Command rs232 = new Rs232Command(this);
string line = rs232.Run();

if (IsValidationRequired())
{
return line == Validation;
}
else
{
return true;
}
return IsValid(line);
}
else if (CommandString == "SoftRestart")
{
Expand All @@ -90,14 +115,7 @@ public bool Run()
HttpCommand command = new HttpCommand(this);
var httpStatus = command.RunAsync();

if (IsValidationRequired())
{
return httpStatus.Result + "" == Validation;
}
else
{
return true;
}
return IsValid(httpStatus.Result + "");
}
else
{
Expand All @@ -111,25 +129,18 @@ public bool Run()
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + CommandString;
startInfo.UseShellExecute = false;

if (IsValidationRequired())
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardOutput = true;

process.StartInfo = startInfo;
process.Start();

if (IsValidationRequired())
string line = "";
while (!process.StandardOutput.EndOfStream)
{
string line = "";
while (!process.StandardOutput.EndOfStream)
{
line += process.StandardOutput.ReadLine();
}

return line == Validation;
line += process.StandardOutput.ReadLine();
}
else
return true;

return IsValid(line);
}
}
}
Expand Down

0 comments on commit b00d38d

Please sign in to comment.