Skip to content

Commit

Permalink
feat(sdr): update calibration table handling in AsvSdr
Browse files Browse the repository at this point in the history
Refactored code to handle calibration tables in AsvSdr. Modifications include better error handling procedures and updating variable names for more clarity. Try-catch block has been applied for better exception management.
  • Loading branch information
asvol committed Nov 21, 2023
1 parent f1544b6 commit 9385c58
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 82 deletions.
135 changes: 91 additions & 44 deletions src/Asv.Mavlink.Test/Microservices/AsvSdr/AsvSdrExTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
#nullable enable
using System;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
Expand Down Expand Up @@ -804,53 +805,77 @@ public async Task Client_read_calibration_table_list()
var (client, server) = await SetUpConnection();
var date = DateTime.Now;
server.Base.Set(_ => _.CalibTableCount = 2);
server.ReadCalibrationTableInfo = index =>
{
return index switch
{
0 => new CalibrationTableInfo { Name = "Table 1", Size = 10, Updated = date, },
1 => new CalibrationTableInfo { Name = "Table 2", Size = 20, Updated = date, },
_ => throw new Exception("Invalid table index")
};
server.TryReadCalibrationTableInfo = (ushort index, out string? name,out ushort? size, out CalibrationTableMetadata? metadata) =>
{
switch (index)
{
case 0:
size = 10;
name = "Table 1";
metadata = new CalibrationTableMetadata(updated: date);
return true;
case 1:
size = 20;
name = "Table 2";
metadata = new CalibrationTableMetadata(updated: date);
return true;
default:
size = null;
name = null;
metadata = null;
return false;
}
};
var status = await client.Base.Status.FirstAsync();
await client.ReadCalibrationTableList();
var sub = client.CalibrationTables.Bind(out var list).Subscribe();
Assert.Equal(2, list.Count);
Assert.Equal("Table 1", list[0].Name);
Assert.Equal("Table 2", list[1].Name);
Assert.Equal(10, list[0].RemoteRowCount.Value);
Assert.Equal(20, list[1].RemoteRowCount.Value);
Assert.Equal(10, list[0].RemoteSize.Value);
Assert.Equal(20, list[1].RemoteSize.Value);
Assert.Equal(0, list[0].Index);
Assert.Equal(1, list[1].Index);
Assert.True(date - list[0].Updated.Value < TimeSpan.FromMilliseconds(100));
Assert.True(date - list[0].Metadata.Value.Updated < TimeSpan.FromMilliseconds(100));
}
[Fact]
public async Task Client_download_calibration_table()
{
var (client, server) = await SetUpConnection();
var date = DateTime.Now;
server.Base.Set(_ => _.CalibTableCount = 2);
server.ReadCalibrationTableInfo = index =>
{
return index switch
{
0 => new CalibrationTableInfo { Name = "Table 1", Size = 10, Updated = date, },
1 => new CalibrationTableInfo { Name = "Table 2", Size = 20, Updated = date, },
_ => throw new Exception("Invalid table index")
};
server.TryReadCalibrationTableInfo = (ushort index, out string? name,out ushort? size, out CalibrationTableMetadata? metadata) =>
{
switch (index)
{
case 0:
size = 10;
name = "Table 1";
metadata = new CalibrationTableMetadata(updated: date);
return true;
case 1:
size = 20;
name = "Table 2";
metadata = new CalibrationTableMetadata(updated: date);
return true;
default:
size = null;
name = null;
metadata = null;
return false;
}
};
var status = await client.Base.Status.FirstAsync();
await client.ReadCalibrationTableList();
var sub = client.CalibrationTables.Bind(out var list).Subscribe();
Assert.Equal(2, list.Count);
Assert.Equal("Table 1", list[0].Name);
Assert.Equal("Table 2", list[1].Name);
Assert.Equal(10, list[0].RemoteRowCount.Value);
Assert.Equal(20, list[1].RemoteRowCount.Value);
Assert.Equal(10, list[0].RemoteSize.Value);
Assert.Equal(20, list[1].RemoteSize.Value);
Assert.Equal(0, list[0].Index);
Assert.Equal(1, list[1].Index);
Assert.True(date - list[0].Updated.Value < TimeSpan.FromSeconds(1));
Assert.True(date - list[0].Metadata.Value.Updated < TimeSpan.FromSeconds(1));
}

[Theory]
Expand All @@ -860,31 +885,46 @@ public async Task Client_download_calibration_table()
public async Task Client_read_calibration_table_rows(ushort count)
{
var (client, server) = await SetUpConnection();
var name = "Table 1";
var originName = "Table 1";
var date = DateTime.Now;
server.Base.Set(_ => _.CalibTableCount = 1);
server.ReadCalibrationTableInfo = index =>
server.TryReadCalibrationTableInfo = (ushort index, out string? name,out ushort? size, out CalibrationTableMetadata? metadata) =>
{
return index switch
if (index == 0)
{
0 => new CalibrationTableInfo { Name = name, Size = count, Updated = date, },
_ => throw new Exception("Invalid table index")
};
size = count;
name = originName;
metadata = new CalibrationTableMetadata(updated: date);
return true;
}
else
{
name = null;
size = null;
metadata = null;
return false;
}
};
server.ReadCalibrationTableRow = (tableIndex, row) =>
server.TryReadCalibrationTableRow = (ushort tableIndex, ushort rowIndex, out CalibrationTableRow? row) =>
{
if (tableIndex != 0) throw new Exception("Invalid table index");
return new CalibrationTableRow(row,row*0.1f,row*20,row*10);
if (tableIndex != 0)
{
row = null;
return false;
}
row = new CalibrationTableRow(rowIndex,rowIndex*0.1f,rowIndex*20,rowIndex*10);
return true;

};
var status = await client.Base.Status.FirstAsync();
await client.ReadCalibrationTableList();
var sub = client.CalibrationTables.Bind(out var list).Subscribe();
var table = await client.GetCalibrationTable(name);
var table = await client.GetCalibrationTable(originName);
Assert.NotNull(table);
Assert.Equal(name, table.Name);
Assert.Equal(count, table.RemoteRowCount.Value);
Assert.Equal(originName, table.Name);
Assert.Equal(count, table.RemoteSize.Value);
Assert.Equal(0, table.Index);
Assert.True(date - table.Updated.Value < TimeSpan.FromMilliseconds(100));
Assert.True(date - table.Metadata.Value.Updated < TimeSpan.FromMilliseconds(100));
var items = await table.Download();
Assert.Equal(count, items.Length);
for (int i = 0; i < count; i++)
Expand All @@ -902,23 +942,30 @@ public async Task Client_read_calibration_table_rows(ushort count)
public async Task Client_upload_and_download_with_server_error()
{
var (client, server) = await SetUpConnection();
var name = "Table 1";
var originName = "Table 1";
var date = DateTime.Now;

server.Base.Set(_ => _.CalibTableCount = 1);
server.ReadCalibrationTableInfo = index =>
server.TryReadCalibrationTableInfo = (ushort index, out string? name,out ushort? size, out CalibrationTableMetadata? metadata) =>
{
return index switch
if (index == 0)
{
0 => new CalibrationTableInfo { Name = name, Size = 10, Updated = date, },
_ => throw new Exception("Invalid table index")
};
size = 10;
name = originName;
metadata = new CalibrationTableMetadata(updated: date);
return true;
}

name = null;
size = null;
metadata = null;
return false;
};
server.WriteCalibrationTable = (tableIndex, items) =>
{
throw new Exception("FATAL");
};
server.ReadCalibrationTableRow = (tableIndex, row) =>
server.TryReadCalibrationTableRow = (ushort tableIndex, ushort rowIndex, out CalibrationTableRow? row) =>
{
throw new Exception("FATAL");
};
Expand All @@ -929,7 +976,7 @@ public async Task Client_upload_and_download_with_server_error()
var status = await client.Base.Status.FirstAsync();
await client.ReadCalibrationTableList();
var sub = client.CalibrationTables.Bind(out var list).Subscribe();
var table = await client.GetCalibrationTable(name);
var table = await client.GetCalibrationTable(originName);
Assert.NotNull(table);
await Assert.ThrowsAsync<AsvSdrException>(async () =>
{
Expand Down
39 changes: 33 additions & 6 deletions src/Asv.Mavlink/Microservices/AsvSdr/CalibrationTableRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,42 @@

namespace Asv.Mavlink;

public class CalibrationTableInfo
public class CalibrationTablePod
{
public string Name { get; set; }
public CalibrationTableMetadata Metadata { get; set; }
public CalibrationTableRow[] Rows { get; set; }
}


public class CalibrationTableMetadata
{
public CalibrationTableMetadata()
{

}
public CalibrationTableMetadata(AsvSdrCalibTablePayload result)
{
Updated = MavlinkTypesHelper.FromUnixTimeUs(result.CreatedUnixUs);
}
public CalibrationTableMetadata(DateTime updated)
{
Updated = updated;
}
public DateTime Updated { get; set; }
public ushort Size { get; set; }

public void Fill(AsvSdrCalibTablePayload payload)
{
payload.CreatedUnixUs = MavlinkTypesHelper.ToUnixTimeUs(Updated);
}
}

public class CalibrationTableRow
{
public CalibrationTableRow()
{

}
public CalibrationTableRow(AsvSdrCalibTableRowPayload result)
{
FrequencyHz = result.RefFreq;
Expand All @@ -27,10 +54,10 @@ public CalibrationTableRow(ulong frequencyHz, float refPower, float refValue, fl
RefValue = refValue;
MeasuredValue = measuredValue;
}
public ulong FrequencyHz { get; }
public float RefPower { get; }
public float RefValue { get; }
public float MeasuredValue { get; }
public ulong FrequencyHz { get; set; }
public float RefPower { get;set; }
public float RefValue { get;set; }
public float MeasuredValue { get; set; }

public void Fill(AsvSdrCalibTableRowPayload args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ namespace Asv.Mavlink;
public class AsvSdrClientCalibrationTable:DisposableOnceWithCancel
{
private readonly IAsvSdrClient _ifc;
private readonly RxValue<ushort> _remoteRowCount;
private readonly RxValue<ushort> _remoteSize;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly TimeSpan _deviceUploadTimeout;
private readonly RxValue<DateTime> _updated;
private readonly RxValue<CalibrationTableMetadata> _metadata;

public AsvSdrClientCalibrationTable(AsvSdrCalibTablePayload payload, IAsvSdrClient ifc, TimeSpan deviceUploadTimeout)
{
_ifc = ifc;
_deviceUploadTimeout = deviceUploadTimeout;
_remoteRowCount = new RxValue<ushort>(payload.RowCount).DisposeItWith(Disposable);
_updated = new RxValue<DateTime>(MavlinkTypesHelper.FromUnixTimeUs(payload.CreatedUnixUs)).DisposeItWith(Disposable);
_remoteSize = new RxValue<ushort>(payload.RowCount).DisposeItWith(Disposable);
_metadata = new RxValue<CalibrationTableMetadata>(new CalibrationTableMetadata(payload)).DisposeItWith(Disposable);
Name = MavlinkTypesHelper.GetString(payload.TableName);
Index = payload.TableIndex;
}
public string Name { get; }
public ushort Index { get; }

public IRxValue<ushort> RemoteRowCount => _remoteRowCount;
public IRxEditableValue<DateTime> Updated => _updated;
public IRxValue<ushort> RemoteSize => _remoteSize;
public IRxEditableValue<CalibrationTableMetadata> Metadata => _metadata;

internal void Update(AsvSdrCalibTablePayload payload)
{
var name = MavlinkTypesHelper.GetString(payload.TableName);
if (payload.TableIndex!= Index) throw new ArgumentException($"Invalid table index. Expected: {Index}, actual: {payload.TableIndex}");
if (name != Name) throw new ArgumentException($"Invalid table name. Expected: {Name}, actual: {name}");
_updated.OnNext(MavlinkTypesHelper.FromUnixTimeUs(payload.CreatedUnixUs));
_remoteRowCount.OnNext(payload.RowCount);
_metadata.OnNext(new CalibrationTableMetadata(payload));
_remoteSize.OnNext(payload.RowCount);
}

public async Task<CalibrationTableRow[]> Download(IProgress<double> progress = null,CancellationToken cancel = default)
Expand All @@ -47,7 +47,7 @@ public async Task<CalibrationTableRow[]> Download(IProgress<double> progress = n
progress.Report(0);
var info = await _ifc.ReadCalibrationTable(Index, cancel).ConfigureAwait(false);
var count = info.RowCount;
_remoteRowCount.OnNext(count);
_remoteSize.OnNext(count);
var array = new CalibrationTableRow[count];
for (ushort i = 0; i < count; i++)
{
Expand Down
Loading

0 comments on commit 9385c58

Please sign in to comment.