private async Task <MqttMessagePayload> ParseMQTTMessageToPayload(byte[] byteArray, string topic) { var resString = Encoding.Default.GetString(byteArray); var res = Task.Run(() => { dynamic s = JsonConvert.DeserializeObject(resString); JObject nestedObject = s.state.reported; var nestedTopics = nestedObject.Children() .Select(x => new { Value = (JProperty)x, Key = ((JProperty)x).Name, Path = ((JProperty)x).Path, ObjectNested = (x.Children().Count() == 1 && x.First.Children().Count() > 0), Type = Type.GetType($"Nulah.Roomba.Models.Responses.{( (JProperty)x ).Name}", false, true) }); var messageGroup = "[Grouped] " + string.Join(",", nestedTopics.Select(x => x.Key)); var timestamp = DateTime.UtcNow; _logger.Debug(resString, messageGroup); if (_logger.IsDebugEnabled) { nestedTopics.Select(x => { if (x.Key == "langs") { var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }; settings.Converters.Add(new LangsConverter()); return(new { Topic = x.Key, Path = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), obj = JsonConvert.DeserializeObject($"{{{x.Value.ToString()}}}", typeof(Langs), settings) }); } return(new { Topic = x.Key, Path = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), obj = (x.ObjectNested) ? JsonConvert.DeserializeObject(x.Value.First.ToString(Formatting.None), x.Type) : JsonConvert.DeserializeObject(x.Value.Parent.ToString(Formatting.None), x.Type) }); }).ToList().ForEach(ptfl => _logger.Debug(ptfl.Path, ptfl.Topic)); } // Add to MqttMessage and figure out a way to bundle all the messages with it var nestedPath = nestedObject.First.Path; IEnumerable <MqttMessage> ms; if (nestedPath == "state.reported.pose") { ms = Regex.Matches(resString, _poseRegex) .Cast <Match>() .Select(x => new MqttMessage { Topic = "state.reported.pose", Type = typeof(Pose), Raw = x.Value, Payload = JsonConvert.DeserializeObject <Pose>($"{x.Value}"), TimeStamp = timestamp }); } else { ms = nestedTopics.Select(x => { if (x.Key == "langs") { var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }; settings.Converters.Add(new LangsConverter()); return(new MqttMessage { Topic = $"state.reported.langs", Raw = $"{{{x.Value.ToString()}}}", Type = typeof(Langs), Payload = JsonConvert.DeserializeObject($"{{{x.Value.ToString()}}}", typeof(Langs), settings), TimeStamp = timestamp }); } return(new MqttMessage { Topic = $"state.reported.{x.Key}", Type = x.Type, Raw = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), Payload = (x.ObjectNested) ? JsonConvert.DeserializeObject(x.Value.First.ToString(Formatting.None), x.Type) : JsonConvert.DeserializeObject(x.Value.Parent.ToString(Formatting.None), x.Type), TimeStamp = timestamp }); }); } var mqttres = new MqttMessagePayload { Messages = ms.ToArray() }; return(mqttres); }); return(await res); }
private async Task <MqttMessagePayload> ParseMQTTMessageToPayload(byte[] byteArray, string topic) { var resString = Encoding.Default.GetString(byteArray); Task <MqttMessagePayload> res = Task.Run(() => { dynamic s = JsonConvert.DeserializeObject(resString); JObject nestedObject = s.state.reported; var nestedTopics = nestedObject.Children() .Select(x => new { Value = (JProperty)x, Key = ((JProperty)x).Name, Path = ((JProperty)x).Path, ObjectNested = (x.Children().Count() == 1 && x.First.Children().Count() > 0), Type = Type.GetType($"Nulah.Roomba.Models.Responses.{( (JProperty)x ).Name}", false, true) }); var messageGroup = "[Grouped] " + string.Join(",", nestedTopics.Select(x => x.Key)); DateTime timestamp = StaticHelpers.GetUtcNow(); _logger.Append(resString, messageGroup); var parsedTopicsForLog = nestedTopics.Select(x => { if (x.Key == "langs") { JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }; settings.Converters.Add(new LangsConverter()); return(new { Topic = x.Key, Path = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), obj = JsonConvert.DeserializeObject($"{{{x.Value.ToString()}}}", typeof(Langs), settings) }); } return(new { Topic = x.Key, Path = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), obj = (x.ObjectNested) ? JsonConvert.DeserializeObject(x.Value.First.ToString(Formatting.None), x.Type) : JsonConvert.DeserializeObject(x.Value.Parent.ToString(Formatting.None), x.Type) }); }); /* * List<string> messagesToLog = new List<string>(); * * messagesToLog.Add(logMessage); */ foreach (var ptfl in parsedTopicsForLog) { _logger.Append(ptfl.Path, ptfl.Topic); /* * logMessage = $"->\t{ptfl.Topic}\t{ptfl.Path}{Environment.NewLine}"; * messagesToLog.Add(logMessage); */ } /* * byte[] logBytes = Encoding.UTF8.GetBytes(string.Join("", messagesToLog)); * * Task.Run(async () => { * using(var ss = File.Open(Path.Combine(LogFileLocation, logFileName), FileMode.OpenOrCreate)) { * ss.Seek(0, SeekOrigin.End); * await ss.WriteAsync(logBytes, 0, logBytes.Length); * } * }); */ // Add to MqttMessage and figure out a way to bundle all the messages with it DateTime eventTime = DateTime.UtcNow; var nestedPath = nestedObject.First.Path; IEnumerable <MqttMessage> ms; if (nestedPath == "state.reported.pose") { ms = Regex.Matches(resString, _poseRegex) .Cast <Match>() .Select(x => new MqttMessage { Topic = "state.reported.pose", Type = typeof(Pose), Raw = x.Value, Payload = JsonConvert.DeserializeObject <Pose>($"{x.Value}"), TimeStamp = timestamp }); } else { ms = nestedTopics.Select(x => { if (x.Key == "langs") { JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects }; settings.Converters.Add(new LangsConverter()); return(new MqttMessage { Topic = $"state.reported.langs", Raw = $"{{{x.Value.ToString()}}}", Type = typeof(Langs), Payload = JsonConvert.DeserializeObject($"{{{x.Value.ToString()}}}", typeof(Langs), settings), TimeStamp = timestamp }); } return(new MqttMessage { Topic = $"state.reported.{x.Key}", Type = x.Type, Raw = (x.ObjectNested) ? x.Value.First.ToString(Formatting.None) : x.Value.ToString(Formatting.None), Payload = (x.ObjectNested) ? JsonConvert.DeserializeObject(x.Value.First.ToString(Formatting.None), x.Type) : JsonConvert.DeserializeObject(x.Value.Parent.ToString(Formatting.None), x.Type), TimeStamp = timestamp }); }); } MqttMessagePayload mqttres = new MqttMessagePayload { Messages = ms.ToArray() }; return(mqttres); }); return(await res); }