Files
PlantBox/PlantBox.Shared/Communication/CommandPacket.cs
2019-03-23 11:37:52 +01:00

61 lines
2.0 KiB
C#

using PlantBox.Shared.Communication.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PlantBox.Shared.Communication
{
/// <summary>
/// Represent a Packet as described in the Wiki > Protocol
/// </summary>
public class CommandPacket
{
/// <summary>
/// A valid packet command, see the Wiki > Commands
/// </summary>
public Command Command { get; }
/// <summary>
/// A valid id
/// </summary>
public ulong ID { get; }
/// <summary>
/// Any arguments, see the Wiki page of a command for more infos
/// </summary>
public string[] Arguments { get; }
/// <summary>
/// Create a <see cref="CommandPacket"/>
/// </summary>
/// <param name="command"></param>
/// <param name="id"></param>
/// <param name="arguments"></param>
public CommandPacket(Command command, ulong id, string[] arguments)
{
Command = command;
ID = id;
Arguments = arguments ?? Array.Empty<string>();
}
/// <summary>
/// Create a <see cref="CommandPacket"/>
/// </summary>
/// <param name="command"></param>
/// <param name="arguments"></param>
public CommandPacket(Command command, params string[] arguments)
{
Command = command;
ID = ulong.Parse(arguments[0]);
Arguments = arguments != null && arguments.Length > 1 ? arguments.Skip(1).ToArray() : Array.Empty<string>();
}
/// <summary>
/// Convert a <see cref="CommandPacket"/> to a valid text representation of a packet, see Wiki > Protocol
/// </summary>
/// <returns>A valid packet text representation, ready to be sent</returns>
public override string ToString()
{
return $"{Command.ToString().ToUpperInvariant()}\n{ID}{(Arguments.Length > 0 ? $";{string.Join(";", Arguments)}" : string.Empty)}\n";
}
}
}