Files
I150/srcs/RobotNet10/RobotApp/Communication/Sick.Tim781s.Colab/Commands/MethodCommand.cs
2026-07-03 16:37:12 +07:00

56 lines
1.6 KiB
C#

using System.Text;
namespace Sick.Tim781s.Colab.Commands;
/// <summary>
/// Command to call a method on the scanner (sMN - Method by Name)
/// </summary>
public sealed class MethodCommand : CommandBase
{
public MethodCommand(string methodName, params string[] parameters)
: base($"sMN {methodName} {string.Join(" ", parameters)}")
{
MethodName = methodName;
Parameters = parameters;
}
public string MethodName { get; }
public string[] Parameters { get; }
public string? ReplyValue { get; private set; }
public override bool ProcessReply(ReadOnlyMemory<byte> replyData)
{
// Parse reply: "sAN <methodName> <result>" or "sMA <methodName> <result>"
var replyString = Encoding.ASCII.GetString(replyData.Span);
// Check if reply starts with "sAN" (Answer) or "sMA" (Method Answer)
if (!replyString.StartsWith("sAN", StringComparison.Ordinal) &&
!replyString.StartsWith("sMA", StringComparison.Ordinal))
{
WasSuccessful = false;
return false;
}
// Extract result after method name
var parts = replyString.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2 && parts[1] == MethodName)
{
if (parts.Length > 2)
{
ReplyValue = string.Join(" ", parts.Skip(2));
}
else
{
ReplyValue = string.Empty;
}
WasSuccessful = true;
return true;
}
WasSuccessful = false;
return false;
}
}