Use file scoped namespaces

This commit is contained in:
Peter Butzhammer
2024-02-07 09:54:18 +01:00
parent bacfbf05e2
commit e6cea1e04f
24 changed files with 1131 additions and 1191 deletions

View File

@@ -1,5 +1,4 @@
using System; using NUnit.Framework;
using NUnit.Framework;
using Sharp7.Rx.Interfaces; using Sharp7.Rx.Interfaces;
using Shouldly; using Shouldly;

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic; using DeepEqual.Syntax;
using DeepEqual.Syntax;
using NUnit.Framework; using NUnit.Framework;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;

View File

@@ -1,14 +1,12 @@
using System; using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reactive; using System.Reactive;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects; using System.Reactive.Subjects;
namespace Sharp7.Rx.Basics namespace Sharp7.Rx.Basics;
internal class ConcurrentSubjectDictionary<TKey, TValue> : IDisposable
{ {
internal class ConcurrentSubjectDictionary<TKey, TValue> : IDisposable
{
private readonly object dictionaryLock = new object(); private readonly object dictionaryLock = new object();
private readonly Func<TKey, TValue> valueFactory; private readonly Func<TKey, TValue> valueFactory;
private ConcurrentDictionary<TKey, SubjectWithRefCounter> dictionary; private ConcurrentDictionary<TKey, SubjectWithRefCounter> dictionary;
@@ -124,5 +122,4 @@ namespace Sharp7.Rx.Basics
public int Counter { get; set; } public int Counter { get; set; }
public ISubject<TValue> Subject { get; set; } public ISubject<TValue> Subject { get; set; }
} }
}
} }

View File

@@ -1,9 +1,7 @@
using System; namespace Sharp7.Rx.Basics;
namespace Sharp7.Rx.Basics internal class DisposableItem<TValue> : IDisposable
{ {
internal class DisposableItem<TValue> : IDisposable
{
private readonly Action disposeAction; private readonly Action disposeAction;
bool disposed; bool disposed;
@@ -33,5 +31,4 @@ namespace Sharp7.Rx.Basics
disposed = true; disposed = true;
} }
}
} }

View File

@@ -1,18 +1,12 @@
using System; namespace Sharp7.Rx.Basics;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Sharp7.Rx.Basics /// <summary>
/// Provides a task scheduler that ensures a maximum concurrency level while
/// running on top of the ThreadPool.
/// from http://msdn.microsoft.com/en-us/library/ee789351.aspx
/// </summary>
internal class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
{ {
/// <summary>
/// Provides a task scheduler that ensures a maximum concurrency level while
/// running on top of the ThreadPool.
/// from http://msdn.microsoft.com/en-us/library/ee789351.aspx
/// </summary>
internal class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
{
/// <summary>Whether the current thread is processing work items.</summary> /// <summary>Whether the current thread is processing work items.</summary>
[ThreadStatic] private static bool currentThreadIsProcessingItems; [ThreadStatic] private static bool currentThreadIsProcessingItems;
@@ -142,5 +136,4 @@ namespace Sharp7.Rx.Basics
} }
}, null); }, null);
} }
}
} }

View File

@@ -1,11 +1,10 @@
using System; using System.Collections.Concurrent;
using System.Collections.Concurrent;
using Sharp7.Rx.Interfaces; using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx namespace Sharp7.Rx;
internal class CacheVariableNameParser : IS7VariableNameParser
{ {
internal class CacheVariableNameParser : IS7VariableNameParser
{
private static readonly ConcurrentDictionary<string, S7VariableAddress> addressCache = new ConcurrentDictionary<string, S7VariableAddress>(StringComparer.OrdinalIgnoreCase); private static readonly ConcurrentDictionary<string, S7VariableAddress> addressCache = new ConcurrentDictionary<string, S7VariableAddress>(StringComparer.OrdinalIgnoreCase);
private readonly IS7VariableNameParser inner; private readonly IS7VariableNameParser inner;
@@ -16,5 +15,4 @@ namespace Sharp7.Rx
} }
public S7VariableAddress Parse(string input) => addressCache.GetOrAdd(input, inner.Parse); public S7VariableAddress Parse(string input) => addressCache.GetOrAdd(input, inner.Parse);
}
} }

View File

@@ -1,10 +1,9 @@
namespace Sharp7.Rx.Enums namespace Sharp7.Rx.Enums;
public enum ConnectionState
{ {
public enum ConnectionState
{
Initial, Initial,
Connected, Connected,
DisconnectedByUser, DisconnectedByUser,
ConnectionLost ConnectionLost
}
} }

View File

@@ -1,10 +1,9 @@
namespace Sharp7.Rx.Enums namespace Sharp7.Rx.Enums;
internal enum CpuType
{ {
internal enum CpuType
{
S7_300, S7_300,
S7_400, S7_400,
S7_1200, S7_1200,
S7_1500 S7_1500
}
} }

View File

@@ -1,7 +1,7 @@
namespace Sharp7.Rx.Enums namespace Sharp7.Rx.Enums;
internal enum DbType
{ {
internal enum DbType
{
Bit, Bit,
String, String,
Byte, Byte,
@@ -9,5 +9,4 @@
Integer, Integer,
DInteger, DInteger,
ULong ULong
}
} }

View File

@@ -1,10 +1,9 @@
namespace Sharp7.Rx.Enums namespace Sharp7.Rx.Enums;
internal enum Operand : byte
{ {
internal enum Operand : byte
{
Input = 69, Input = 69,
Output = 65, Output = 65,
Marker = 77, Marker = 77,
Db = 68, Db = 68,
}
} }

View File

@@ -1,8 +1,7 @@
namespace Sharp7.Rx.Enums namespace Sharp7.Rx.Enums;
public enum TransmissionMode
{ {
public enum TransmissionMode
{
Cyclic = 3, Cyclic = 3,
OnChange = 4, OnChange = 4,
}
} }

View File

@@ -1,13 +1,11 @@
using System; using System.Reactive.Disposables;
using System.Reactive.Disposables;
namespace Sharp7.Rx.Extensions namespace Sharp7.Rx.Extensions;
internal static class DisposableExtensions
{ {
internal static class DisposableExtensions
{
public static void AddDisposableTo(this IDisposable disposable, CompositeDisposable compositeDisposable) public static void AddDisposableTo(this IDisposable disposable, CompositeDisposable compositeDisposable)
{ {
compositeDisposable.Add(disposable); compositeDisposable.Add(disposable);
} }
}
} }

View File

@@ -1,13 +1,12 @@
using System; using System.Reactive.Concurrency;
using System.Reactive.Concurrency;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Sharp7.Rx.Extensions namespace Sharp7.Rx.Extensions;
internal static class ObservableExtensions
{ {
internal static class ObservableExtensions
{
public static IObservable<T> DisposeMany<T>(this IObservable<T> source) public static IObservable<T> DisposeMany<T>(this IObservable<T> source)
{ {
return Observable.Create<T>(obs => return Observable.Create<T>(obs =>
@@ -79,5 +78,4 @@ namespace Sharp7.Rx.Extensions
var deferedObs = Observable.Defer(() => ((++attempt == 1) ? source : source.DelaySubscription(retryDelay, scheduler))); var deferedObs = Observable.Defer(() => ((++attempt == 1) ? source : source.DelaySubscription(retryDelay, scheduler)));
return retryCount > 0 ? reDoCount(deferedObs, retryCount) : reDo(deferedObs); return retryCount > 0 ? reDoCount(deferedObs, retryCount) : reDo(deferedObs);
} }
}
} }

View File

@@ -1,16 +1,14 @@
using System; using System.Reactive;
using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Threading.Tasks; using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces; using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx.Extensions namespace Sharp7.Rx.Extensions;
public static class PlcExtensions
{ {
public static class PlcExtensions
{
public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData, bool initialTransfer) public static IObservable<TReturn> CreateDatatransferWithHandshake<TReturn>(this IPlc plc, string triggerAddress, string ackTriggerAddress, Func<IPlc, Task<TReturn>> readData, bool initialTransfer)
{ {
return Observable.Create<TReturn>(async observer => return Observable.Create<TReturn>(async observer =>
@@ -70,5 +68,4 @@ namespace Sharp7.Rx.Extensions
await plc.SetValue(ackTriggerAddress, true); await plc.SetValue(ackTriggerAddress, true);
} }
} }
}
} }

View File

@@ -1,18 +1,15 @@
using System; using JetBrains.Annotations;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
namespace Sharp7.Rx.Interfaces namespace Sharp7.Rx.Interfaces;
[NoReorder]
public interface IPlc : IDisposable
{ {
[NoReorder]
public interface IPlc : IDisposable
{
IObservable<TValue> CreateNotification<TValue>(string variableName, TransmissionMode transmissionMode); IObservable<TValue> CreateNotification<TValue>(string variableName, TransmissionMode transmissionMode);
Task SetValue<TValue>(string variableName, TValue value); Task SetValue<TValue>(string variableName, TValue value);
Task<TValue> GetValue<TValue>(string variableName); Task<TValue> GetValue<TValue>(string variableName);
IObservable<ConnectionState> ConnectionState { get; } IObservable<ConnectionState> ConnectionState { get; }
ILogger Logger { get; } ILogger Logger { get; }
}
} }

View File

@@ -1,15 +1,11 @@
using System; using JetBrains.Annotations;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
namespace Sharp7.Rx.Interfaces namespace Sharp7.Rx.Interfaces;
[NoReorder]
internal interface IS7Connector : IDisposable
{ {
[NoReorder]
internal interface IS7Connector : IDisposable
{
IObservable<ConnectionState> ConnectionState { get; } IObservable<ConnectionState> ConnectionState { get; }
Task InitializeAsync(); Task InitializeAsync();
@@ -22,5 +18,4 @@ namespace Sharp7.Rx.Interfaces
Task<ushort> WriteBytes(Operand operand, ushort startByteAdress, byte[] data, ushort dBNr, CancellationToken token); Task<ushort> WriteBytes(Operand operand, ushort startByteAdress, byte[] data, ushort dBNr, CancellationToken token);
Task<Dictionary<string, byte[]>> ExecuteMultiVarRequest(IReadOnlyList<string> variableNames); Task<Dictionary<string, byte[]>> ExecuteMultiVarRequest(IReadOnlyList<string> variableNames);
}
} }

View File

@@ -1,7 +1,6 @@
namespace Sharp7.Rx.Interfaces namespace Sharp7.Rx.Interfaces;
internal interface IS7VariableNameParser
{ {
internal interface IS7VariableNameParser
{
S7VariableAddress Parse(string input); S7VariableAddress Parse(string input);
}
} }

View File

@@ -1,9 +1,7 @@
using System.Collections.Generic; namespace Sharp7.Rx;
namespace Sharp7.Rx public static class S7ErrorCodes
{ {
public static class S7ErrorCodes
{
/// <summary> /// <summary>
/// This list is not exhaustive and should be considered work in progress. /// This list is not exhaustive and should be considered work in progress.
/// </summary> /// </summary>
@@ -23,5 +21,4 @@ namespace Sharp7.Rx
{ {
return !notDisconnectedErrorCodes.Contains(errorCode); return !notDisconnectedErrorCodes.Contains(errorCode);
} }
}
} }

View File

@@ -1,13 +1,12 @@
using System; using System.Buffers.Binary;
using System.Buffers.Binary;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
namespace Sharp7.Rx namespace Sharp7.Rx;
internal static class S7ValueConverter
{ {
internal static class S7ValueConverter
{
public static TValue ConvertToType<TValue>(byte[] buffer, S7VariableAddress address) public static TValue ConvertToType<TValue>(byte[] buffer, S7VariableAddress address)
{ {
if (typeof(TValue) == typeof(bool)) if (typeof(TValue) == typeof(bool))
@@ -81,5 +80,4 @@ namespace Sharp7.Rx
[FieldOffset(0)] public uint UInt32; [FieldOffset(0)] public uint UInt32;
[FieldOffset(0)] public float Single; [FieldOffset(0)] public float Single;
} }
}
} }

View File

@@ -1,16 +1,15 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
namespace Sharp7.Rx namespace Sharp7.Rx;
[NoReorder]
internal class S7VariableAddress
{ {
[NoReorder]
internal class S7VariableAddress
{
public Operand Operand { get; set; } public Operand Operand { get; set; }
public ushort DbNr { get; set; } public ushort DbNr { get; set; }
public ushort Start { get; set; } public ushort Start { get; set; }
public ushort Length { get; set; } public ushort Length { get; set; }
public byte Bit { get; set; } public byte Bit { get; set; }
public DbType Type { get; set; } public DbType Type { get; set; }
}
} }

View File

@@ -1,14 +1,12 @@
using System; using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
using Sharp7.Rx.Interfaces; using Sharp7.Rx.Interfaces;
namespace Sharp7.Rx namespace Sharp7.Rx;
internal class S7VariableNameParser : IS7VariableNameParser
{ {
internal class S7VariableNameParser : IS7VariableNameParser
{
private static readonly Regex regex = new Regex(@"^(?<operand>db{1})(?<dbNr>\d{1,4})\.?(?<type>dbx|x|s|string|b|dbb|d|int|dbw|w|dint|dul|dulint|dulong|){1}(?<start>\d+)(\.(?<bitOrLength>\d+))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant); private static readonly Regex regex = new Regex(@"^(?<operand>db{1})(?<dbNr>\d{1,4})\.?(?<type>dbx|x|s|string|b|dbb|d|int|dbw|w|dint|dul|dulint|dulong|){1}(?<start>\d+)(\.(?<bitOrLength>\d+))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
private static readonly IReadOnlyDictionary<string, DbType> types = new Dictionary<string, DbType>(StringComparer.OrdinalIgnoreCase) private static readonly IReadOnlyDictionary<string, DbType> types = new Dictionary<string, DbType>(StringComparer.OrdinalIgnoreCase)
@@ -80,5 +78,4 @@ namespace Sharp7.Rx
return null; return null;
} }
}
} }

View File

@@ -1,10 +1,9 @@
namespace Sharp7.Rx.Settings namespace Sharp7.Rx.Settings;
internal class PlcConnectionSettings
{ {
internal class PlcConnectionSettings
{
public int CpuMpiAddress { get; set; } public int CpuMpiAddress { get; set; }
public string IpAddress { get; set; } public string IpAddress { get; set; }
public int Port { get; set; } public int Port { get; set; }
public int RackNumber { get; set; } public int RackNumber { get; set; }
}
} }

View File

@@ -1,11 +1,6 @@
using System; using System.Reactive.Disposables;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Sharp7.Rx.Basics; using Sharp7.Rx.Basics;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
@@ -14,10 +9,10 @@ using Sharp7.Rx.Interfaces;
using Sharp7.Rx.Resources; using Sharp7.Rx.Resources;
using Sharp7.Rx.Settings; using Sharp7.Rx.Settings;
namespace Sharp7.Rx namespace Sharp7.Rx;
internal class Sharp7Connector : IS7Connector
{ {
internal class Sharp7Connector : IS7Connector
{
private readonly BehaviorSubject<ConnectionState> connectionStateSubject = new BehaviorSubject<ConnectionState>(Enums.ConnectionState.Initial); private readonly BehaviorSubject<ConnectionState> connectionStateSubject = new BehaviorSubject<ConnectionState>(Enums.ConnectionState.Initial);
private readonly int cpuSlotNr; private readonly int cpuSlotNr;
@@ -275,5 +270,4 @@ namespace Sharp7.Rx
{ {
Dispose(false); Dispose(false);
} }
}
} }

View File

@@ -1,13 +1,8 @@
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive; using System.Reactive;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Sharp7.Rx.Basics; using Sharp7.Rx.Basics;
using Sharp7.Rx.Enums; using Sharp7.Rx.Enums;
@@ -15,10 +10,10 @@ using Sharp7.Rx.Extensions;
using Sharp7.Rx.Interfaces; using Sharp7.Rx.Interfaces;
using Sharp7.Rx.Settings; using Sharp7.Rx.Settings;
namespace Sharp7.Rx namespace Sharp7.Rx;
public class Sharp7Plc : IPlc
{ {
public class Sharp7Plc : IPlc
{
protected readonly CompositeDisposable Disposables = new CompositeDisposable(); protected readonly CompositeDisposable Disposables = new CompositeDisposable();
private readonly ConcurrentSubjectDictionary<string, byte[]> multiVariableSubscriptions = new ConcurrentSubjectDictionary<string, byte[]>(StringComparer.InvariantCultureIgnoreCase); private readonly ConcurrentSubjectDictionary<string, byte[]> multiVariableSubscriptions = new ConcurrentSubjectDictionary<string, byte[]>(StringComparer.InvariantCultureIgnoreCase);
private readonly List<long> performanceCoutner = new List<long>(1000); private readonly List<long> performanceCoutner = new List<long>(1000);
@@ -280,5 +275,4 @@ namespace Sharp7.Rx
{ {
Dispose(false); Dispose(false);
} }
}
} }