Skip to content

Commit

Permalink
Fix LogViewer bug to do with managing multi_id data streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettchris committed Apr 6, 2017
1 parent f32725f commit b90ba55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
4 changes: 4 additions & 0 deletions LogViewer/LogViewer/Model/LogItemSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public class LogItemSchema

public string Type { get; set; }

// in case the Name is not unique (as is the case with multi_id formats in px4 logs)
// the IDataLog implementor can use this field instead.
public int Id { get; set; }

public List<LogItemSchema> ChildItems { get; set; }

public bool HasChildren
Expand Down
63 changes: 34 additions & 29 deletions LogViewer/LogViewer/Model/Px4ULog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public enum FieldType
Char,
Struct
};


public class MessageField
{
Expand All @@ -52,8 +52,8 @@ public MessageField(string definition)
{
string[] parts = definition.Split(' ');
typeName = parts[0];
int i = typeName.IndexOf('[');

int i = typeName.IndexOf('[');
if (i > 0)
{
// an array
Expand All @@ -63,7 +63,7 @@ public MessageField(string definition)
{
string s = typeName.Substring(i, j - i);
arraySize = int.Parse(s);
typeName = typeName.Substring(0, i-1);
typeName = typeName.Substring(0, i - 1);
}
else
{
Expand Down Expand Up @@ -153,7 +153,11 @@ public MessageFormat(string fmt)
{
if (!string.IsNullOrWhiteSpace(fs))
{
fields.Add(new MessageField(fs));
var field = new MessageField(fs);
if (!field.name.StartsWith("_padding"))
{
fields.Add(field);
}
}
}
}
Expand All @@ -167,7 +171,7 @@ internal void Resolve(Dictionary<string, MessageFormat> formats)
}
}
};

class MessageLogging : Message
{
byte logLevel;
Expand All @@ -183,20 +187,18 @@ public MessageLogging(byte logLevel, long timestamp, string msg)

}

class MessageData: Message
class MessageData : Message
{
internal MessageFormat format;
internal int multiId;
internal MessageSubscription subscription;
UInt16 msgId;
byte[] value;
Dictionary<string, object> values;

public MessageData(UInt16 msgId, byte[] value, MessageFormat fmt, int multiId)
public MessageData(UInt16 msgId, byte[] value, MessageSubscription s)
{
this.msgId = msgId;
this.value = value;
this.multiId = multiId;
this.format = fmt;
this.subscription = s;
}

internal DataValue GetValue(MessageField field)
Expand Down Expand Up @@ -231,7 +233,7 @@ private void ParseValues()
values = new Dictionary<string, object>();
BinaryReader reader = new BinaryReader(new MemoryStream(value));

foreach (var field in format.fields)
foreach (var field in subscription.format.fields)
{
object value = null;
if (field.arraySize > 0)
Expand All @@ -245,8 +247,8 @@ private void ParseValues()
}
else
{
value = ReadField(reader, field);
}
value = ReadField(reader, field);
}
values[field.name] = value;
}
}
Expand Down Expand Up @@ -327,7 +329,7 @@ public MessageParameter(string key, byte[] value)


}

class MessageSync : Message
{
byte[] magic;
Expand Down Expand Up @@ -356,9 +358,11 @@ class MessageSubscription
{
public MessageFormat format;
public int multiId;
public int id;

public MessageSubscription(MessageFormat fmt, int multiId)
public MessageSubscription(int id, MessageFormat fmt, int multiId)
{
this.id = id;
this.format = fmt;
this.multiId = multiId;
}
Expand Down Expand Up @@ -410,7 +414,8 @@ public DateTime StartTime
public IEnumerable<DataValue> GetDataValues(LogItemSchema schema, DateTime startTime, TimeSpan duration)
{
List<LogItemSchema> path = new List<Model.LogItemSchema>();
while (schema != null) {
while (schema != null)
{
path.Insert(0, schema);
schema = schema.Parent;
}
Expand All @@ -421,13 +426,13 @@ public IEnumerable<DataValue> GetDataValues(LogItemSchema schema, DateTime start
if (m is MessageData)
{
MessageData data = (MessageData)m;
if (data.format.name == root.Name)
if (data.subscription.id == root.Id)
{
// matching root schema, so drill down if necessary.
for (int i = 1, n = path.Count; i < n; i++)
{
LogItemSchema child = path[i];
foreach (var field in data.format.fields)
foreach (var field in data.subscription.format.fields)
{
if (field.name == child.Name)
{
Expand Down Expand Up @@ -497,7 +502,7 @@ await Task.Run(() =>
}
this.reader = null;
}

CreateSchema();
});
this.duration = lastTime - startTime;
Expand All @@ -507,27 +512,27 @@ await Task.Run(() =>
private void CreateSchema()
{
LogItemSchema schema = new LogItemSchema() { Name = "Px4ULog", Type = "Root", ChildItems = new List<Model.LogItemSchema>() };

// only need to show formats that we actually have subscriptions on.
foreach (var sub in this.subscriptions.Values)
{
var element = CreateSchemaItem(sub.format);
// we can have "multi_id" subscriptions on the same format.
var element = CreateSchemaItem(sub, sub.format);
schema.ChildItems.Add(element);
}

this.schema = schema;
}

LogItemSchema CreateSchemaItem(MessageFormat fmt)
LogItemSchema CreateSchemaItem(MessageSubscription sub, MessageFormat fmt)
{
LogItemSchema element = new LogItemSchema() { Name = fmt.name, Parent = schema };
LogItemSchema element = new LogItemSchema() { Name = fmt.name, Parent = schema, Id = sub.id };
foreach (var f in fmt.fields)
{
LogItemSchema column = new LogItemSchema() { Name = f.name, Parent = element, Type = f.typeName + (f.arraySize > 0 ? "[" + f.arraySize + "]" : "") };
if (f.type == FieldType.Struct)
{
// nested
var child = CreateSchemaItem(f.structType);
var child = CreateSchemaItem(sub, f.structType);
column.ChildItems = child.ChildItems;
}
if (element.ChildItems == null)
Expand Down Expand Up @@ -619,7 +624,7 @@ public MessageData ReadDataMessage(ushort len)

MessageSubscription s = null;
subscriptions.TryGetValue(msgId, out s);
MessageData data = new MessageData(msgId, value, s.format, s.multiId);
MessageData data = new MessageData(msgId, value, s);

return data;
}
Expand Down Expand Up @@ -657,9 +662,9 @@ public Message ReadAddLoggedMessage(ushort len)
{
byte multi_id = reader.ReadByte();
UInt16 msgId = reader.ReadUInt16();
string msgName = reader.ReadAsciiString(len - 3);
string msgName = reader.ReadAsciiString(len - 3);
MessageFormat fmt = formats[msgName];
subscriptions[msgId] = new MessageSubscription(fmt, multi_id);
subscriptions[msgId] = new MessageSubscription(msgId, fmt, multi_id);
return null;
}

Expand Down

0 comments on commit b90ba55

Please sign in to comment.