-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates and fixes to NMEA and Seatalk libraries (#2351)
* Updates and fixes to NMEA and Seatalk libraries Intensive real-world testing has been done on these. * Fix unit test * Avoid crash in finalizer * Handle additional exception * Add missing documentation
- Loading branch information
Showing
26 changed files
with
1,122 additions
and
64 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/devices/Common/Iot/Device/Common/PositionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Iot.Device.Common | ||
{ | ||
/// <summary> | ||
/// Extensions for positions | ||
/// </summary> | ||
public static partial class PositionExtensions | ||
{ | ||
/// <summary> | ||
/// Normalizes the longitude to +/- 180° | ||
/// </summary> | ||
public static GeographicPosition NormalizeAngleTo180(this GeographicPosition position) | ||
{ | ||
return new GeographicPosition(position.Latitude, NormalizeAngleTo180(position.Longitude), position.EllipsoidalHeight); | ||
} | ||
|
||
/// <summary> | ||
/// Normalizes the angle to +/- 180° | ||
/// </summary> | ||
public static double NormalizeAngleTo180(double angleDegree) | ||
{ | ||
angleDegree %= 360; | ||
if (angleDegree <= -180) | ||
{ | ||
angleDegree += 360; | ||
} | ||
else if (angleDegree > 180) | ||
{ | ||
angleDegree -= 360; | ||
} | ||
|
||
return angleDegree; | ||
} | ||
|
||
/// <summary> | ||
/// Normalizes the longitude to [0..360°) | ||
/// </summary> | ||
public static GeographicPosition NormalizeAngleTo360(this GeographicPosition position) | ||
{ | ||
return new GeographicPosition(position.Latitude, NormalizeAngleTo360(position.Longitude), position.EllipsoidalHeight); | ||
} | ||
|
||
/// <summary> | ||
/// Normalizes an angle to [0..360°) | ||
/// </summary> | ||
public static double NormalizeAngleTo360(double angleDegree) | ||
{ | ||
angleDegree %= 360; | ||
if (angleDegree < 0) | ||
{ | ||
angleDegree += 360; | ||
} | ||
|
||
return angleDegree; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Iot.Device.Common | ||
{ | ||
/// <summary> | ||
/// A simple logger that creates textual log files. Created via <see cref="SimpleFileLoggerFactory"/> | ||
/// </summary> | ||
public sealed class SimpleFileLogger : ILogger | ||
{ | ||
private readonly string _category; | ||
private TextWriter _writer; | ||
|
||
/// <summary> | ||
/// Creates a new logger | ||
/// </summary> | ||
/// <param name="category">Logger category name</param> | ||
/// <param name="writer">The text writer for logging.</param> | ||
/// <remarks> | ||
/// The <paramref name="writer"/> must be a thread-safe file writer! | ||
/// </remarks> | ||
public SimpleFileLogger(string category, TextWriter writer) | ||
{ | ||
_category = category; | ||
_writer = writer; | ||
Enabled = true; | ||
} | ||
|
||
/// <summary> | ||
/// Used by the factory to terminate all its loggers | ||
/// </summary> | ||
internal bool Enabled | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
/// <summary> | ||
/// Does nothing and returns an empty IDisposable | ||
/// </summary> | ||
/// <typeparam name="TState">Current logger state</typeparam> | ||
/// <param name="state">State argument</param> | ||
/// <returns>An empty <see cref="IDisposable"/></returns> | ||
public IDisposable BeginScope<TState>(TState state) | ||
where TState : notnull | ||
{ | ||
return new LogDispatcher.ScopeDisposable(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return Enabled; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
if (Enabled) | ||
{ | ||
string msg = formatter(state, exception); | ||
var time = DateTime.Now; | ||
_writer.WriteLine($"{time.ToShortDateString()} {time.ToLongTimeString()} - {_category} - {logLevel} - {msg}"); | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/devices/Common/Iot/Device/Common/SimpleFileLoggerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Iot.Device.Common | ||
{ | ||
/// <summary> | ||
/// Provides a very simple console logger that does not require a reference to Microsoft.Extensions.Logging.dll | ||
/// </summary> | ||
public class SimpleFileLoggerFactory : ILoggerFactory, IDisposable | ||
{ | ||
private TextWriter? _writer; | ||
private List<SimpleFileLogger> _createdLoggers; | ||
|
||
/// <summary> | ||
/// Create a logger factory that creates loggers to logs to the specified file | ||
/// </summary> | ||
/// <param name="fileName">File name to log to (full path)</param> | ||
public SimpleFileLoggerFactory(string fileName) | ||
{ | ||
_writer = TextWriter.Synchronized(new StreamWriter(fileName, true, Encoding.UTF8)); | ||
_createdLoggers = new List<SimpleFileLogger>(); | ||
} | ||
|
||
/// <summary> | ||
/// The console logger is built-in here | ||
/// </summary> | ||
/// <param name="provider">Argument is ignored</param> | ||
public void AddProvider(ILoggerProvider provider) | ||
{ | ||
} | ||
|
||
/// <inheritdoc/> | ||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
if (_writer == null) | ||
{ | ||
return NullLogger.Instance; | ||
} | ||
|
||
var newLogger = new SimpleFileLogger(categoryName, _writer); | ||
_createdLoggers.Add(newLogger); | ||
return newLogger; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
foreach (var d in _createdLoggers) | ||
{ | ||
d.Enabled = false; | ||
} | ||
|
||
_createdLoggers.Clear(); | ||
|
||
if (_writer != null) | ||
{ | ||
_writer.Close(); | ||
_writer.Dispose(); | ||
_writer = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.