private void OnMessageReceived(Nmea.NmeaMessage msg) { var args = new NmeaMessageReceivedEventArgs(msg); var multi = msg as IMultiPartMessage; if (multi != null) { args.IsMultipart = true; if (MultiPartMessageCache.ContainsKey(msg.MessageType)) { var dic = MultiPartMessageCache[msg.MessageType]; if (dic.ContainsKey(multi.MessageNumber - 1) && !dic.ContainsKey(multi.MessageNumber)) { dic[multi.MessageNumber] = msg; } else //Something is out of order. Clear cache { MultiPartMessageCache.Remove(msg.MessageType); } } else if (multi.MessageNumber == 1) { MultiPartMessageCache[msg.MessageType] = new Dictionary <int, Nmea.NmeaMessage>(multi.TotalMessages); MultiPartMessageCache[msg.MessageType][1] = msg; } if (MultiPartMessageCache.ContainsKey(msg.MessageType)) { var dic = MultiPartMessageCache[msg.MessageType]; if (dic.Count == multi.TotalMessages) //We have a full list { MultiPartMessageCache.Remove(msg.MessageType); args.MessageParts = dic.Values.ToArray(); } } } if (MessageReceived != null) { MessageReceived(this, args); } }
private void OnMessageReceived(Nmea.NmeaMessage msg) { if (msg == null) { return; } Nmea.NmeaMessage[] messageParts = null; if (msg is IMultiPartMessage multi) { string messageType = msg.MessageType.Substring(2); //We don't care about the two first characters. Ie GPGSV, GLGSV, GAGSV etc are all part of the same multi-part message if (MultiPartMessageCache.ContainsKey(messageType)) { var dic = MultiPartMessageCache[messageType]; if (dic.ContainsKey(multi.MessageNumber - 1) && !dic.ContainsKey(multi.MessageNumber)) { dic[multi.MessageNumber] = msg; } else //Something is out of order. Clear cache { MultiPartMessageCache.Remove(messageType); } } else if (multi.MessageNumber == 1) { MultiPartMessageCache[messageType] = new Dictionary <int, Nmea.NmeaMessage>(multi.TotalMessages); MultiPartMessageCache[messageType][1] = msg; } if (MultiPartMessageCache.ContainsKey(messageType)) { var dic = MultiPartMessageCache[messageType]; if (dic.Count == multi.TotalMessages) //We have a full list { MultiPartMessageCache.Remove(messageType); messageParts = dic.Values.ToArray(); } } } MessageReceived?.Invoke(this, new NmeaMessageReceivedEventArgs(msg, messageParts)); }