Skip to content

Commit

Permalink
feat(mavlink): Migrate NLog => ILogger
Browse files Browse the repository at this point in the history
  • Loading branch information
asvol committed Sep 7, 2024
1 parent 1a2d7cd commit 318a6b4
Show file tree
Hide file tree
Showing 127 changed files with 1,508 additions and 1,180 deletions.
2 changes: 1 addition & 1 deletion src/Asv.Mavlink.Shell/Asv.Mavlink.Shell.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<AssemblyVersion>$(ProductVersion)</AssemblyVersion>
<FileVersion>$(ProductVersion)</FileVersion>
<Version>$(ProductVersion)</Version>
Expand Down
2 changes: 1 addition & 1 deletion src/Asv.Mavlink.Test/Asv.Mavlink.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyVersion>$(ProductVersion)</AssemblyVersion>
<FileVersion>$(ProductVersion)</FileVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task Client_request_compatibility_server_respond_it()
var origin = new HashSet<AsvRsgaCustomMode>(Enum.GetValues<AsvRsgaCustomMode>());
serverEx.GetCompatibility = () => origin;
var originMode = AsvRsgaCustomMode.AsvRsgaCustomModeTxGp;
serverEx.SetMode = async (mode, cancel) =>
serverEx.SetMode = async (mode, param2, param3, param4, param5, param6, param7, cancel) =>
{
Assert.Equal(originMode,mode);
return MavResult.MavResultAccepted;
Expand Down
3 changes: 2 additions & 1 deletion src/Asv.Mavlink/Asv.Mavlink.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFramework>net8.0</TargetFramework>
<AssemblyVersion>$(ProductVersion)</AssemblyVersion>
<FileVersion>$(ProductVersion)</FileVersion>
<Version>$(ProductVersion)</Version>
Expand Down Expand Up @@ -59,6 +59,7 @@
<PackageReference Include="Asv.Common" Version="$(AsvCommonVersion)" />
<PackageReference Include="Asv.IO" Version="$(AsvCommonVersion)" />
<PackageReference Include="DynamicData" Version="$(DynamicDataVersion)" />
<PackageReference Include="ZLogger" Version="$(ZLoggerVersion)" />
<PackageReference Include="Microsoft.DotNet.ApiCompat.Task" Version="$(ApiCompatVersion)" >
<!--using api compatibility purely as a development harness-->
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/Asv.Mavlink/Devices/Adsb/Server/AdsbServerDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AdsbServerDevice(IMavlinkV2Connection connection,
{
var command = new CommandServer(connection,seq,identity,scheduler).DisposeItWith(Disposable);
Adsb = new AdsbVehicleServer(connection, identity, seq, scheduler);
Heartbeat.Set(_ => _.Type = MavType.MavTypeAdsb);
Heartbeat.Set(p => p.Type = MavType.MavTypeAdsb);
}

public IAdsbVehicleServer Adsb { get; }
Expand Down
20 changes: 12 additions & 8 deletions src/Asv.Mavlink/Devices/Base/Client/ClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;
using NLog;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;

namespace Asv.Mavlink;

Expand All @@ -23,14 +25,16 @@ public abstract class ClientDevice: DisposableOnceWithCancel, IClientDevice
private readonly RxValue<string> _name;
private bool _needToRequestAgain = true;
private int _isRequestInfoIsInProgressOrAlreadySuccess;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ILogger _loggerBase;

protected ClientDevice(IMavlinkV2Connection connection,
MavlinkClientIdentity identity,
ClientDeviceConfig config,
IPacketSequenceCalculator seq,
IScheduler? scheduler = null)
IScheduler? scheduler = null,
ILogger? logger = null)
{
_loggerBase = logger ?? NullLogger.Instance;
Connection = connection;
_config = config;
_scheduler = scheduler;
Expand All @@ -43,18 +47,18 @@ protected ClientDevice(IMavlinkV2Connection connection,
_onInit = new RxValue<InitState>(InitState.WaitConnection)
.DisposeItWith(Disposable);
Heartbeat.Link.DistinctUntilChanged()
.Where(_ => _ == LinkState.Disconnected)
.Where(s => s == LinkState.Disconnected)
.Subscribe(_ => _needToRequestAgain = true).DisposeItWith(Disposable);

if (scheduler != null)
{
Heartbeat.Link.DistinctUntilChanged().Where(_ => _needToRequestAgain).Where(_ => _ == LinkState.Connected)
Heartbeat.Link.DistinctUntilChanged().Where(_ => _needToRequestAgain).Where(s => s == LinkState.Connected)
// only one time
.Delay(TimeSpan.FromMilliseconds(100),scheduler).Subscribe(_ => TryReconnect()).DisposeItWith(Disposable);
}
else
{
Heartbeat.Link.DistinctUntilChanged().Where(_ => _needToRequestAgain).Where(_ => _ == LinkState.Connected)
Heartbeat.Link.DistinctUntilChanged().Where(_ => _needToRequestAgain).Where(s => s == LinkState.Connected)
// only one time
.Delay(TimeSpan.FromMilliseconds(100)).Subscribe(_ => TryReconnect()).DisposeItWith(Disposable);
}
Expand All @@ -79,7 +83,7 @@ private async void TryReconnect()
catch (Exception e)
{
if (IsDisposed) return; // no need to replay since the instance was already disposed
Logger.Error( $"Error to init device:{e.Message}");
_loggerBase.ZLogError(e, $"Error to init device:{e.Message}");
_onInit.OnNext(InitState.Failed);
if (_scheduler != null)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Asv.Mavlink/Devices/Base/Client/IClientDevice.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Reactive.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using Asv.Common;

Expand Down Expand Up @@ -98,7 +96,7 @@ public static class ClientDeviceHelper
public static void WaitUntilConnect(this IClientDevice client, int timeoutMs = 3000)
{
var tcs = new TaskCompletionSource();
using var subscribe = client.Heartbeat.Link.Where(_ => _ == LinkState.Connected).FirstAsync().Subscribe(_ =>
using var subscribe = client.Heartbeat.Link.Where(s => s == LinkState.Connected).FirstAsync().Subscribe(_ =>
{
tcs.TrySetResult();
});
Expand All @@ -113,7 +111,7 @@ public static void WaitUntilConnectAndInit(this IVehicleClient client, int timeo
{
client.WaitUntilConnect(timeoutMs);
var tcs = new TaskCompletionSource();
using var subscribe = client.OnInit.Where(_ => _ == InitState.Complete).FirstAsync().Subscribe(_ =>
using var subscribe = client.OnInit.Where(s => s == InitState.Complete).FirstAsync().Subscribe(_ =>
{
tcs.TrySetResult();
});
Expand Down
16 changes: 11 additions & 5 deletions src/Asv.Mavlink/Devices/Gbs/Client/GbsClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using Asv.Common;
using NLog;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;

namespace Asv.Mavlink;

Expand All @@ -17,15 +19,19 @@ public class GbsClientDeviceConfig:ClientDeviceConfig
public class GbsClientDevice : ClientDevice, IGbsClientDevice
{
private readonly GbsClientDeviceConfig _config;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly ParamsClientEx _params;
private readonly ILogger _logger;

public GbsClientDevice(IMavlinkV2Connection connection,
MavlinkClientIdentity identity,
IPacketSequenceCalculator seq,
GbsClientDeviceConfig config,
IScheduler? scheduler = null) : base(connection, identity,config, seq, scheduler)
IScheduler? scheduler = null,
ILogger? logger = null) : base(connection, identity,config, seq, scheduler, logger)
{
_config = config;
_logger = logger ?? NullLogger.Instance;
scheduler ??= Scheduler.Default;
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);
Expand All @@ -44,7 +50,7 @@ protected override async Task InternalInit()
_params.Init(MavParamHelper.ByteWiseEncoding, ArraySegment<ParamDescription>.Empty);
try
{
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
_logger.ZLogInformation($"Try to read serial number from param {_config.SerialNumberParamName}");
Params.Filter(_config.SerialNumberParamName)
.Select(serial => $"RTK GBS [{(int)serial:D5}]")
.Subscribe(EditableName)
Expand All @@ -53,7 +59,7 @@ protected override async Task InternalInit()
}
catch (Exception e)
{
_logger.Warn($"Error to get serial number:{e.Message}");
_logger.ZLogError(e, $"Error to get serial number:{e.Message}");
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/Asv.Mavlink/Devices/Radio/Client/RadioClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using Asv.Common;
using NLog;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;

namespace Asv.Mavlink;

Expand All @@ -20,13 +22,20 @@ public class RadioClientDevice : ClientDevice, IRadioClientDevice
{
private readonly RadioClientDeviceConfig _config;
private readonly ParamsClientEx _params;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly ILogger _logger;

public RadioClientDevice(IAudioCodecFactory factory, IMavlinkV2Connection connection, MavlinkClientIdentity identity, IPacketSequenceCalculator seq,RadioClientDeviceConfig config, IScheduler? scheduler = null)
: base(connection, identity, config, seq, scheduler)
public RadioClientDevice(
IAudioCodecFactory factory,
IMavlinkV2Connection connection,
MavlinkClientIdentity identity,
IPacketSequenceCalculator seq,
RadioClientDeviceConfig config,
IScheduler? scheduler = null,
ILogger? logger = null)
: base(connection, identity, config, seq, scheduler, logger)
{
_logger ??= NullLogger.Instance;
_config = config ?? throw new ArgumentNullException(nameof(config));

Command = new CommandClient(connection, identity, seq, config.Command).DisposeItWith(Disposable);
var client = new AsvRadioClient(connection, identity,seq).DisposeItWith(Disposable);
Radio = new AsvRadioClientEx(client, Heartbeat, Command).DisposeItWith(Disposable);
Expand All @@ -42,7 +51,8 @@ protected override async Task InternalInit()
_params.Init(MavParamHelper.ByteWiseEncoding, ArraySegment<ParamDescription>.Empty);
try
{
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
_logger.ZLogTrace($"Try to read serial number from param {_config.SerialNumberParamName}");

Params.Filter(_config.SerialNumberParamName)
.Select(serial => $"RADIO [{(int)serial:D5}]")
.Subscribe(EditableName)
Expand All @@ -51,7 +61,7 @@ protected override async Task InternalInit()
}
catch (Exception e)
{
_logger.Warn($"Error to get RADIO serial number:{e.Message}");
_logger.ZLogWarning($"Error to get RADIO serial number:{e.Message}");
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/Asv.Mavlink/Devices/Rfsa/Client/RfsaClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Asv.Common;
using Asv.Mavlink.Diagnostic.Client;
using Asv.Mavlink.V2.Common;
using NLog;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;

namespace Asv.Mavlink;

Expand All @@ -23,15 +25,23 @@ public class RfsaClientDeviceConfig:ClientDeviceConfig
public class RfsaClientDevice:ClientDevice, IRfsaClientDevice
{
private readonly RfsaClientDeviceConfig _config;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly ILogger _logger;
private readonly ParamsClientEx _params;
private readonly AsvChartClient _charts;
private readonly DiagnosticClient _diagnostics;

public RfsaClientDevice(IMavlinkV2Connection link, MavlinkClientIdentity identity, RfsaClientDeviceConfig config, IPacketSequenceCalculator seq, IScheduler scheduler)
:base(link,identity,config,seq,scheduler)
public RfsaClientDevice(
IMavlinkV2Connection link,
MavlinkClientIdentity identity,
RfsaClientDeviceConfig config,
IPacketSequenceCalculator seq,
IScheduler? scheduler = null,
ILogger? logger = null)
:base(link,identity,config,seq,scheduler,logger)
{
_config = config;
_logger = logger ?? NullLogger.Instance;
scheduler ??= Scheduler.Default;
Command = new CommandClient(link, identity, seq, config.Command).DisposeItWith(Disposable);
var paramBase = new ParamsClient(link, identity, seq, config.Params).DisposeItWith(Disposable);
_params = new ParamsClientEx(paramBase, config.Params).DisposeItWith(Disposable);
Expand All @@ -44,7 +54,7 @@ protected override async Task InternalInit()
_params.Init(MavParamHelper.ByteWiseEncoding, ArraySegment<ParamDescription>.Empty);
try
{
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
_logger.ZLogTrace($"Try to read serial number from param {_config.SerialNumberParamName}");
Params.Filter(_config.SerialNumberParamName)
.Select(serial => $"RFSA [{(int)serial:D5}]")
.Subscribe(EditableName)
Expand All @@ -53,7 +63,7 @@ protected override async Task InternalInit()
}
catch (Exception e)
{
_logger.Warn($"Error to get serial number:{e.Message}");
_logger.ZLogWarning($"Error to get serial number:{e.Message}");
}
}
public IParamsClientEx Params => _params;
Expand Down
7 changes: 1 addition & 6 deletions src/Asv.Mavlink/Devices/Rsga/Client/IRsgaClientDevice.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Asv.Mavlink.Diagnostic.Client;
using Asv.Mavlink.V2.AsvRsga;
using Asv.Mavlink.V2.Common;
using Asv.Mavlink.Diagnostic.Client;

namespace Asv.Mavlink;

Expand Down
24 changes: 17 additions & 7 deletions src/Asv.Mavlink/Devices/Rsga/Client/RsgaClientDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Threading.Tasks;
using Asv.Common;
using Asv.Mavlink.Diagnostic.Client;
using NLog;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;

namespace Asv.Mavlink;

Expand All @@ -21,17 +23,25 @@ public class RsgaClientDeviceConfig:ClientDeviceConfig
public class RsgaClientDevice : ClientDevice, IRsgaClientDevice
{
private readonly RsgaClientDeviceConfig _config;
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly ILogger _logger;
private readonly ParamsClientEx _params;
private readonly AsvChartClient _charts;
private readonly DiagnosticClient _diagnostics;
private readonly CommandClient _command;
private readonly AsvRsgaClientEx _rsga;

public RsgaClientDevice(IMavlinkV2Connection link, MavlinkClientIdentity identity, RsgaClientDeviceConfig config, IPacketSequenceCalculator seq, IScheduler scheduler)
:base(link,identity,config,seq,scheduler)
public RsgaClientDevice(
IMavlinkV2Connection link,
MavlinkClientIdentity identity,
RsgaClientDeviceConfig config,
IPacketSequenceCalculator seq,
IScheduler? scheduler = null,
ILogger? logger = null)
:base(link,identity,config,seq,scheduler,logger)
{
_config = config;
_logger = logger ?? NullLogger.Instance;
scheduler ??= Scheduler.Default;
_command = new CommandClient(link, identity, seq, config.Command).DisposeItWith(Disposable);
var paramBase = new ParamsClient(link, identity, seq, config.Params).DisposeItWith(Disposable);
_params = new ParamsClientEx(paramBase, config.Params).DisposeItWith(Disposable);
Expand All @@ -46,9 +56,9 @@ protected override async Task InternalInit()
_params.Init(MavParamHelper.ByteWiseEncoding, ArraySegment<ParamDescription>.Empty);
try
{
_logger.Trace($"Try to read compatibilities.");
_logger.ZLogTrace($"Try to read compatibilities.");
await Rsga.Base.GetCompatibilities().ConfigureAwait(false);
_logger.Trace($"Try to read serial number from param {_config.SerialNumberParamName}");
_logger.ZLogTrace($"Try to read serial number from param {_config.SerialNumberParamName}");
Params.Filter(_config.SerialNumberParamName)
.Select(serial => $"RSGA [{(int)serial:D5}]")
.Subscribe(EditableName)
Expand All @@ -58,7 +68,7 @@ protected override async Task InternalInit()
}
catch (Exception e)
{
_logger.Warn($"Error to get serial number:{e.Message}");
_logger.ZLogWarning($"Error to get serial number:{e.Message}");
}
}
public IParamsClientEx Params => _params;
Expand Down
Loading

0 comments on commit 318a6b4

Please sign in to comment.