Files
2025-03-13 12:04:22 -07:00

76 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Raytheon.Instruments.LSPS
{
internal class LspsResponse
{
public enum CommandType
{
GET,
SET
}
public CommandType commandType_ = CommandType.GET;
public readonly bool successful_ = true;
// this text_ can be an error message for a SET command or can be a value from a GET command
public readonly string text_ = String.Empty;
public LspsResponse(string response)
{
string str = String.Empty;
Match regexMatch;
regexMatch = Regex.Match(response, @"result=([^\s]+).(.+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
if (regexMatch.Success)
{
commandType_ = CommandType.SET;
if (regexMatch.Groups[1].Value == "0")
{
successful_ = false;
}
text_ = regexMatch.Groups[2].Value;
}
else if (Regex.IsMatch(response, @"error:.+", RegexOptions.IgnoreCase | RegexOptions.Singleline))
{
commandType_ = CommandType.SET;
successful_ = false;
text_ = response;
}
// process response for SET command
text_ = Regex.Replace(text_, @"error:.(.+)", "$1", RegexOptions.IgnoreCase | RegexOptions.Singleline);
text_ = Regex.Replace(text_, @"([^\r]+).+", "$1", RegexOptions.IgnoreCase | RegexOptions.Singleline);
// process response for GET command
if (commandType_ != CommandType.SET)
{
MatchCollection matches = Regex.Matches(response, @"[^\s=]+=[^\s=,]+", RegexOptions.IgnoreCase | RegexOptions.Singleline);
// if there are multiple data points, we return all the data in a string delimited by comma
if (matches.Count > 1)
{
text_ = String.Empty;
foreach (Match match in matches)
{
if (text_.Length > 0)
{
text_ += ",";
}
text_ += Regex.Replace(match.Value, @"[^=]+=([^=]+)", "$1", RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
}
else
{
text_ = Regex.Replace(response, @"[^=]+=([^\r]+).+", "$1", RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
}
}
}
}