/// <summary>
        /// Serializes the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns>A string representation of the command.</returns>
        public static String Serialize(WatchoutCommand command)
        {
            String str = String.Format("[{0}]{1}", command.ID, command.Name);

            foreach (var prop in command.GetType().GetProperties().Where(pi => !pi.GetCustomAttributes(typeof(WatchoutIgnoreAttribute), true).Any()))
            {
                var o = prop.GetValue(command);

                if (o != null)
                {
                    String value = String.Empty;

                    if (prop.PropertyType == typeof(String))
                    {
                        value = "\"" + o.ToString() + "\"";
                    }
                    else if (prop.PropertyType == typeof(bool))
                    {
                        value = o.ToString().ToLower();
                    }
                    else if (prop.PropertyType == typeof(TimeSpan))
                    {
                        value = "\"" + ((TimeSpan)o).ToString(@"hh\:mm\:ss\.fff") + "\"";
                    }
                    else
                    {
                        value = o.ToString();
                    }

                    str += " " + value;
                }
            }

            return(str + "\r\n");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sending thread loop.
        /// </summary>
        private void SendingMethod()
        {
            while (IsConnected)
            {
                if (_commandQueue.Count > 0)
                {
                    WatchoutCommand command = null;

                    if (_commandQueue.TryDequeue(out command))
                    {
                        String commandText = CommandFormatter.Serialize(command);
                        Debug.WriteLine("Sending: " + commandText);

                        try
                        {
                            var    stream       = _tcpClient.GetStream();
                            byte[] commandBytes = Encoding.UTF8.GetBytes(commandText);
                            stream.Write(commandBytes, 0, commandBytes.Length);
                        }
                        catch { }
                    }
                }

                Thread.Sleep(30);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends the specified <see cref="WatchoutCommand" /> asynchronously while waiting for a response.
        /// </summary>
        /// <typeparam name="T">The <see cref="WatchoutFeedback" /> type.</typeparam>
        /// <param name="command">The command.</param>
        /// <returns>
        /// Returns the proper feedback.
        /// </returns>
        public async Task <T> SendCommand <T>(WatchoutCommand command) where T : WatchoutFeedback
        {
            Task <T> task = new Task <T>(() =>
            {
                SendCommand(command);

                DateTime sentDate = DateTime.Now;

                while (!_availableFeedbacks.Exists(x => x.ID == command.ID) && DateTime.Now < sentDate.AddSeconds(CommandsTimeout))
                {
                    Thread.Sleep(30);
                }

                var feedback = _availableFeedbacks.SingleOrDefault(x => x.ID == command.ID) as T;

                if (feedback == null)
                {
                    WatchoutFeedback falseInstnce = Activator.CreateInstance <T>();
                    falseInstnce.CommandName      = command.Name;
                    falseInstnce.ID    = command.ID;
                    falseInstnce.Error = new ErrorFeedback()
                    {
                        ErrorKind = ErrorKind.NETWORK_ERRORS, Explanation = "Could not contact the Watchout server."
                    };
                    return(falseInstnce as T);
                }
                else
                {
                    feedback.CommandName = command.Name;
                }

                _availableFeedbacks.Remove(feedback);

                if (feedback.GetType() == typeof(ErrorFeedback))
                {
                    WatchoutFeedback falseInstnce = Activator.CreateInstance <T>();
                    falseInstnce.ID    = feedback.ID;
                    falseInstnce.Error = feedback as ErrorFeedback;
                    return(falseInstnce as T);
                }

                return(feedback);
            });

            task.Start();
            await task;

            return(task.Result);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sends the specified <see cref="WatchoutCommand" />.
 /// </summary>
 /// <param name="command">The command.</param>
 public void SendCommand(WatchoutCommand command)
 {
     _commandQueue.Enqueue(command);
 }