示例#1
0
        /// <summary>
        /// Processes a message and sends the responses.
        /// </summary>
        /// <param name="ea">Event received via RabbitMQ.</param>
        private void processMessage(BasicDeliverEventArgs ea)
        {
            string response = null;

            var body       = ea.Body.ToArray();
            var props      = ea.BasicProperties;
            var replyProps = channel.CreateBasicProperties();

            replyProps.CorrelationId = props.CorrelationId;

            var message  = Encoding.UTF8.GetString(body);
            var petition = (Petition)JsonConvert.DeserializeObject(message, jsonSettings);

            try
            {
                switch (petition.Type)
                {
                case PetitionType.GET_LIST:
                    var result = new Result
                    {
                        Type = ResultType.GET_LIST,
                        Data = dbAccess.GetDevices()
                    };
                    response = JsonConvert.SerializeObject(result, jsonSettings);
                    break;

                case PetitionType.ADD_DEVICE:
                    if (dbAccess.SaveDevice((IDevice)petition.Data))
                    {
                        response = JsonConvert.SerializeObject(new Result {
                            Type = ResultType.ADD_DEVICE, Data = "true"
                        }, jsonSettings);
                    }
                    else
                    {
                        response = JsonConvert.SerializeObject(new Result {
                            Type = ResultType.ADD_DEVICE, Data = "false"
                        }, jsonSettings);
                    }
                    break;
                }
            }
            finally
            {
                var responseBytes = Encoding.UTF8.GetBytes(response);
                channel.BasicPublish("", props.ReplyTo, replyProps, responseBytes);
                channel.BasicAck(ea.DeliveryTag, false);
            }
        }