Skip to content

Commit

Permalink
feat(mavlink): fixes packet handler and viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
asv-soft-u03 committed Oct 8, 2024
1 parent 0eee586 commit 4624c85
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
65 changes: 28 additions & 37 deletions src/Asv.Mavlink.Shell/Commands/PacketViewerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class PacketViewerCommand
private string _consoleSearch;
private string _consoleSize = "10";
private readonly SourceList<KeyValuePair<DateTime,string>> _packetsSource = new();
private ReadOnlyObservableCollection<KeyValuePair<DateTime,string>> _packets;
private readonly ConcurrentQueue<KeyValuePair<DateTime,string>> _outputCollection = new();
private PacketPrinter _printer;

[Command("packetviewer")]
Expand All @@ -47,7 +45,8 @@ public void Run(string connection)
.Border(TableBorder.None);
_table.AddRow(_headerTable);
_table.AddRow("");
_packetTable = new Table().AddColumns("Time", "Packet").Expand().Title("[aqua]Packets[/]");

_packetTable = new Table().AddColumns("Time", "Type", "Source","Size", "Sequence", "Message").Expand().Title("[aqua]Packets[/]");
AnsiConsole.Status().Start("Create router...", ctx =>
{
ctx.Spinner(Spinner.Known.Aesthetic);
Expand All @@ -61,43 +60,25 @@ public void Run(string connection)
});
_router.WrapToV2ExtensionEnabled = true;
});
_packetsSource.LimitSizeTo(1000).Subscribe();
_packetsSource.Connect().Sort(SortExpressionComparer<KeyValuePair<DateTime,string>>.Descending(_ => _.Key)).Bind(out _packets)
.Subscribe(_ => SetPacketsInQueue(_packets.First()));
_router.Buffer(TimeSpan.FromSeconds(1)).Select(GetPacketAndAddToCollection)
.Subscribe(_packetsSource.AddRange);
_router.Buffer(TimeSpan.FromSeconds(1))
.Subscribe(_ =>
{
GetPacketAndUpdateTable(_);
});
_actionsThread = new Thread(InterceptConsoleActions);
_actionsThread.Start();
AnsiConsole.Live(_table).AutoClear(true).StartAsync(async ctx =>
{
while (_isCancel is false)
{
await Task.Delay(TimeSpan.FromMilliseconds(35));
UpdateTable();
if (_table is null) continue;
ctx.Refresh();
}
});
}

private void UpdateTable()
{
if (_isPause) return;
var dequeue = _outputCollection.TryDequeue(out var packetModel);
if (dequeue)
{
_packetTable.InsertRow(0, $"{packetModel.Key}", $"{packetModel.Value}");
_table.UpdateCell(1, 0, _packetTable);
}

var parsed = int.TryParse(_consoleSize, out var size);
if (parsed is false) return;
if (_packetTable.Rows.Count <= size) return;
for (var i = size; i < _packetTable.Rows.Count; i++)
{
_packetTable.RemoveRow(i);
}
}


private void UpdateSearchCellInActive() => _headerTable.UpdateCell(0, 0, $"Search: {_consoleSearch}");
private void UpdateSearchCellActive() => _headerTable.UpdateCell(0, 0, $"[aqua]Search:[/] {_consoleSearch}");
Expand Down Expand Up @@ -216,24 +197,34 @@ private void InterceptConsoleActions()
}
}

private void SetPacketsInQueue(KeyValuePair<DateTime,string> packet)
{
_outputCollection.Enqueue(packet);
}

private List<KeyValuePair<DateTime,string>> GetPacketAndAddToCollection(IList<IPacketV2<IPayload>> pkt)
private void GetPacketAndUpdateTable(IList<IPacketV2<IPayload>> pkt)
{
var result = new List<KeyValuePair<DateTime, string>>();

if (_isPause) return;
var result = new List<IPacketV2<IPayload>>();
var filtered = pkt.Where(_ => _.Name.Contains(_consoleSearch, StringComparison.InvariantCultureIgnoreCase));
if (_consoleSearch is null)
{
result.AddRange(pkt.Select(item => _printer.Print(item)).Select(prnt => new KeyValuePair<DateTime, string>(DateTime.Now, prnt)));

result.AddRange(pkt);
}
else
{
result.AddRange(filtered.Select(item => _printer.Print(item)).Select(prnt => new KeyValuePair<DateTime, string>(DateTime.Now, prnt)));
result.AddRange(pkt.Where(_=>_.Name.Contains(_consoleSearch)));
}

return result;
foreach (var packet in result)
{
var msg = _printer.Print(packet);
_packetTable.InsertRow(0, $@"{DateTime.Now}", $"{packet.Name}", $"{packet.ComponentId},{packet.SystemId}", $"{packet.GetByteSize()}", Markup.Escape($"{packet.Sequence}"), Markup.Escape($"{msg}"));
_table.UpdateCell(1, 0, _packetTable);
}
var parsed = int.TryParse(_consoleSize, out var size);
if (parsed is false) return;
if (_packetTable.Rows.Count <= size) return;
for (var i = size; i < _packetTable.Rows.Count; i++)
{
_packetTable.RemoveRow(i);
}
}
}
2 changes: 1 addition & 1 deletion src/Asv.Mavlink/PacketPrinter/DefaultPacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public string Print(IPacketV2<IPayload> packet, PacketFormatting formatting = Pa
if (packet == null) throw new ArgumentException("Incoming packet was not initialized!");
if (!canConvert) throw new ArgumentException("Converter can not convert incoming packet!");

return $"Type: {packet.Name}, Source: {packet.SystemId}:{packet.ComponentId}, Size:{packet.GetByteSize()}, Sequence: {packet.Sequence:000} Message:{ConvertPacket(packet, formatting)}";
return $"{ConvertPacket(packet, formatting)}";
}
private static string ConvertPacket(IPacketV2<IPayload> packet, PacketFormatting formatting = PacketFormatting.None)
{
Expand Down

0 comments on commit 4624c85

Please sign in to comment.