#region CmdMessenger - MIT - (c) 2013 Thijs Elenbaas. /* CmdMessenger - library that provides command based messaging Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Copyright 2013 - Thijs Elenbaas */ #endregion using System; using System.Collections.Generic; using System.Globalization; namespace CommandMessenger { /// A command to be send by CmdMessenger public class SendCommand : Command { private readonly List _lazyArguments = new List(); /// Indicates if we want to wait for an acknowledge command. /// true if request acknowledge, false if not. public bool ReqAc { get; set; } /// Gets or sets the acknowledge command ID. /// the acknowledge command ID. public int AckCmdId { get; set; } /// Gets or sets the time we want to wait for the acknowledge command. /// The timeout on waiting for an acknowledge public int Timeout { get; set; } /// Constructor. /// the command ID. public SendCommand(int cmdId) { Init(cmdId, false, 0, 0); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, string argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The arguments. public SendCommand(int cmdId, string[] arguments) { Init(cmdId, false, 0, 0); AddArguments(arguments); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, float argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, double argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, UInt16 argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, Int16 argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, UInt32 argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, Int32 argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. public SendCommand(int cmdId, bool argument) { Init(cmdId, false, 0, 0); AddArgument(argument); } /// Constructor. /// Command ID /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, string argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The arguments. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, string[] arguments, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArguments(arguments); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, float argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, double argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, Int16 argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, UInt16 argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, Int32 argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Constructor. /// Command ID /// The argument. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge public SendCommand(int cmdId, UInt32 argument, int ackCmdId, int timeout) { Init(cmdId, true, ackCmdId, timeout); AddArgument(argument); } /// Initializes this object. /// Command ID /// true to request ac. /// Acknowledge command ID. /// The timeout on waiting for an acknowledge private void Init(int cmdId, bool reqAc, int ackCmdId, int timeout) { ReqAc = reqAc; CmdId = cmdId; AckCmdId = ackCmdId; Timeout = timeout; } // ***** String based **** / /// Adds a command argument. /// The argument. public void AddArgument(string argument) { if (argument != null) _lazyArguments.Add(() => CmdArgs.Add(argument)); } /// Adds command arguments. /// The arguments. public void AddArguments(string[] arguments) { if (arguments != null) _lazyArguments.Add(() => CmdArgs.AddRange(arguments)); } /// Adds a command argument. /// The argument. public void AddArgument(float argument) { _lazyArguments.Add(() => CmdArgs.Add(argument.ToString("R", CultureInfo.InvariantCulture))); } /// Adds a command argument. /// The argument. public void AddArgument(Double argument) { _lazyArguments.Add(() => { if (CommunicationManager.BoardType == BoardType.Bit16) { // Not completely sure if this is needed for plain text sending. var floatArg = (float) argument; CmdArgs.Add(floatArg.ToString("R", CultureInfo.InvariantCulture)); } else { CmdArgs.Add(argument.ToString("R", CultureInfo.InvariantCulture)); } }); } /// Adds a command argument. /// The argument. public void AddArgument(Int16 argument) { _lazyArguments.Add(() => CmdArgs.Add(argument.ToString(CultureInfo.InvariantCulture))); } /// Adds a command argument. /// The argument. public void AddArgument(UInt16 argument) { _lazyArguments.Add(() => CmdArgs.Add(argument.ToString(CultureInfo.InvariantCulture))); } /// Adds a command argument. /// The argument. public void AddArgument(Int32 argument) { // Make sure the other side can read this: on a 16 processor, read as Long _lazyArguments.Add(() => CmdArgs.Add(argument.ToString(CultureInfo.InvariantCulture))); } /// Adds a command argument. /// The argument. public void AddArgument(UInt32 argument) { // Make sure the other side can read this: on a 16 processor, read as Long _lazyArguments.Add(() => CmdArgs.Add(argument.ToString(CultureInfo.InvariantCulture))); } /// Adds a command argument. /// The argument. public void AddArgument(bool argument) { AddArgument((Int16) (argument ? 1 : 0)); } // ***** Binary **** / /// Adds a binary command argument. /// The argument. public void AddBinArgument(string argument) { _lazyArguments.Add(() => CmdArgs.Add(Escaping.Escape(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(float argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(Double argument) { _lazyArguments.Add(() => CmdArgs.Add(CommunicationManager.BoardType == BoardType.Bit16 ? BinaryConverter.ToString((float)argument) : BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(Int16 argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(UInt16 argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(Int32 argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(UInt32 argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument))); } /// Adds a binary command argument. /// The argument. public void AddBinArgument(bool argument) { _lazyArguments.Add(() => CmdArgs.Add(BinaryConverter.ToString(argument ? (byte) 1 : (byte) 0))); } internal void InitArguments() { CmdArgs.Clear(); foreach (var action in _lazyArguments) { action.Invoke(); } } } }