Esempio n. 1
0
        public async Task <string> Process(string message)
        {
            HubMessage msg = null;
            Dictionary <MeterMessage, string> data = new Dictionary <MeterMessage, string>();

            try
            {
                msg = JsonConvert.DeserializeObject <HubMessage>(message);
            }
            catch (Exception ex)
            {
                return("Необработанное сообщение. " +
                       GetHubId(new string[] { "{", "," }, message.Split("Messages").First()) +
                       "\n. Exception: " + ex.Message);
            }

            await _decisionMaker.UsingConnectionAsync(async (conn) =>
            {
                foreach (var meterMessage in msg.Messages)
                {
                    if (meterMessage.Body.State == 1)
                    {
                        var table = await _decisionMaker.MakeDecision(conn, meterMessage.Id);
                        if (!string.IsNullOrEmpty(table))
                        {
                            data.Add(meterMessage, table);
                        }
                    }
                }

                var newData = data.OrderBy(p => p.Value).GroupBy(x => x.Value);

                var sb = new StringBuilder();
                foreach (var pairs in newData)
                {
                    sb.Append(CreateMiltiInsertQuery(pairs.Key, pairs));
                }

                await _decisionMaker.Insert(conn, sb.ToString());
            });

            return("Количество обработанных сообщений: " + data.Count);
        }