Skip to content

Commit

Permalink
fix(params): add helper methods for ParamsClientEx.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
asvol committed Nov 14, 2023
1 parent e8ac11d commit 3d770ea
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
29 changes: 25 additions & 4 deletions src/Asv.Mavlink/Devices/Gbs/Client/GbsClientDevice.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
#nullable enable
using System;
using System.Reactive.Concurrency;
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;
using NLog;

namespace Asv.Mavlink;

public class GbsClientDeviceConfig:ClientDeviceConfig
{
public CommandProtocolConfig Command { get; set; } = new();
public ParamsClientExConfig Params { get; set; } = new();
public string SerialNumberParamName { get; set; } = "BRD_SERIAL_NUM";
}
public class GbsClientDevice : ClientDevice, IGbsClientDevice
{
private readonly GbsClientDeviceConfig _config;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly ParamsClientEx _params;
public GbsClientDevice(IMavlinkV2Connection connection,
MavlinkClientIdentity identity,
IPacketSequenceCalculator seq,
GbsClientDeviceConfig config,
IScheduler? scheduler = null) : base(connection, identity,config, seq, scheduler)
{
_config = config;
Command = new CommandClient(connection, identity, seq, config.Command).DisposeItWith(Disposable);
var gbs = new AsvGbsClient(connection,identity,seq,scheduler).DisposeItWith(Disposable);
Gbs = new AsvGbsExClient(gbs,Heartbeat,Command).DisposeItWith(Disposable);
var paramBase = new ParamsClient(connection, identity, seq, config.Params).DisposeItWith(Disposable);
Params = new ParamsClientEx(paramBase, config.Params).DisposeItWith(Disposable);
_params = new ParamsClientEx(paramBase, config.Params).DisposeItWith(Disposable);
}

public IParamsClientEx Params { get; }
public IParamsClientEx Params => _params;
public ICommandClient Command { get; }
public IAsvGbsExClient Gbs { get; }
protected override Task InternalInit()
{
_params.Init(MavParamHelper.ByteWiseEncoding, ArraySegment<ParamDescription>.Empty);
return Task.CompletedTask;
}

protected override Task<string> GetCustomName(CancellationToken cancel)
protected override async Task<string> GetCustomName(CancellationToken cancel)
{
return Task.FromResult("GBS");
try
{
if (_config.SerialNumberParamName.IsNullOrWhiteSpace() == false)
{
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
var serialNumber = (int)await Params.ReadOnce(_config.SerialNumberParamName, cancel).ConfigureAwait(false);
return $"RTK GBS [{serialNumber:D5}]";
}
}
catch (Exception e)
{
_logger.Error($"Error to get GBS serial number:{e.Message}");
}
return "RTL GBS";
}

public override DeviceClass Class => DeviceClass.GbsRtk;
Expand Down
22 changes: 20 additions & 2 deletions src/Asv.Mavlink/Devices/Sdr/Client/SdrClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;
using NLog;

namespace Asv.Mavlink;

Expand All @@ -15,13 +16,17 @@ public class SdrClientDeviceConfig:ClientDeviceConfig
public MissionClientExConfig MissionsEx { get; set; } = new();
public ParameterClientConfig Params { get; set; } = new();
public ParamsClientExConfig ParamsEx { get; set; } = new();
public string SerialNumberParamName { get; set; } = "BRD_SERIAL_NUM";
}
public class SdrClientDevice : ClientDevice, ISdrClientDevice
{
private readonly SdrClientDeviceConfig _config;
private readonly ParamsClientEx _params;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

public SdrClientDevice(IMavlinkV2Connection connection, MavlinkClientIdentity identity, SdrClientDeviceConfig config, IPacketSequenceCalculator seq, IScheduler? scheduler = null) : base(connection, identity, config, seq, scheduler)
{
_config = config;
Command = new CommandClient(connection, identity, seq, config.Command).DisposeItWith(Disposable);
Sdr = new AsvSdrClientEx(new AsvSdrClient(connection, identity, seq), Heartbeat, Command,config.SdrEx).DisposeItWith(Disposable);
Missions = new MissionClientEx(new MissionClient(connection, identity, seq,config.Missions), config.MissionsEx).DisposeItWith(Disposable);
Expand All @@ -33,9 +38,22 @@ protected override Task InternalInit()
return Task.CompletedTask;
}

protected override Task<string> GetCustomName(CancellationToken cancel)
protected override async Task<string> GetCustomName(CancellationToken cancel)
{
return Task.FromResult("SdrPayload");
try
{
if (_config.SerialNumberParamName.IsNullOrWhiteSpace() == false)
{
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
var serialNumber = (int)await Params.ReadOnce(_config.SerialNumberParamName, cancel).ConfigureAwait(false);
return $"SDR [{serialNumber:D5}]";
}
}
catch (Exception e)
{
_logger.Warn($"Error to get SDR serial number:{e.Message}");
}
return "SDR Payload";
}
public override DeviceClass Class => DeviceClass.SdrPayload;
public IAsvSdrClientEx Sdr { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Asv.Mavlink;

public class ArduCopterClient:ArduVehicle
{
public static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
public ArduCopterClient(IMavlinkV2Connection connection, MavlinkClientIdentity identity, VehicleClientConfig config, IPacketSequenceCalculator seq, IScheduler? scheduler = null)
: base(connection, identity, config, seq, scheduler)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Asv.Mavlink/Microservices/Params/Client/Ex/IParamsClientEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;
Expand All @@ -8,6 +9,10 @@ namespace Asv.Mavlink;

public interface IParamsClientEx
{
IParamsClient Base { get; }
IObservable<(string, MavParamValue)> OnValueChanged { get; }


bool IsInit { get; set; }
/// <summary>
/// True if params synced with remote device and local cache
Expand Down Expand Up @@ -47,5 +52,11 @@ public interface IParamsClientEx
/// <param name="cancel"></param>
/// <returns></returns>
Task<MavParamValue> WriteOnce(string name, MavParamValue value, CancellationToken cancel = default);

public IObservable<MavParamValue> Filter(string name)
{
MavParamHelper.CheckParamName(name);
return OnValueChanged.Where(x=>x.Item1.Equals(name, StringComparison.InvariantCultureIgnoreCase)).Select(x=>x.Item2);
}
}

12 changes: 12 additions & 0 deletions src/Asv.Mavlink/Microservices/Params/Client/Ex/ParamsClientEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;
Expand All @@ -25,6 +26,7 @@ public class ParamsClientEx : DisposableOnceWithCancel, IParamsClientEx
private ImmutableDictionary<string,ParamDescription> _descriptions;
private readonly RxValue<ushort?> _remoteCount;
private readonly RxValue<ushort> _localCount;
private readonly Subject<(string, MavParamValue)> _onValueChanged;

public ParamsClientEx(IParamsClient client, ParamsClientExConfig config)
{
Expand All @@ -38,8 +40,10 @@ public ParamsClientEx(IParamsClient client, ParamsClientExConfig config)
_remoteCount = new RxValue<ushort?>(null).DisposeItWith(Disposable);
_localCount = new RxValue<ushort>(0).DisposeItWith(Disposable);
_paramsSource.CountChanged.Select(_=>(ushort)_).Subscribe(_localCount).DisposeItWith(Disposable);
_onValueChanged = new Subject<(string, MavParamValue)>().DisposeItWith(Disposable);
}

public IObservable<(string, MavParamValue)> OnValueChanged => _onValueChanged;
public bool IsInit { get; set; }

public void Init(IMavParamEncoding converter, IEnumerable<ParamDescription> existDescription)
Expand All @@ -63,6 +67,10 @@ private void OnUpdate(IList<ParamValuePayload> items)
if (exist.HasValue)
{
exist.Value.Update(value);
if (_onValueChanged.HasObservers)
{
_onValueChanged.OnNext((name, _converter.ConvertFromMavlinkUnion(value.ParamValue, value.ParamType)));
}
}
else
{
Expand All @@ -73,6 +81,10 @@ private void OnUpdate(IList<ParamValuePayload> items)
var newItem = new ParamItem(Base, _converter, desc, value);
_.AddOrUpdate(newItem);
newItem.IsSynced.Subscribe(OnSyncedChanged);
if (_onValueChanged.HasObservers)
{
_onValueChanged.OnNext((name, _converter.ConvertFromMavlinkUnion(value.ParamValue, value.ParamType)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void CheckParamName(string name)
throw new ArgumentException(
$"Param name '{name}' not match regex '{ParamNameRegexString}')");
}

public static IMavParamEncoding GetEncoding(MavParamEncodingType type)
{
return type switch
Expand Down

0 comments on commit 3d770ea

Please sign in to comment.