Skip to content

Commit

Permalink
Added poller delay
Browse files Browse the repository at this point in the history
- added downstream and upstream speeds
- refactored SerialQueue
- counted version up to 0.5.5
  • Loading branch information
Sandoun committed Sep 21, 2022
1 parent 45a9fa0 commit 6f8f891
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
8 changes: 8 additions & 0 deletions Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ await interf.ConnectAsync(
//set the current second to the PLCs TIME register
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));

while(true) {

Console.WriteLine($"Speed UP: {interf.BytesPerSecondUpstream} B/s");
Console.WriteLine($"Speed DOWN: {interf.BytesPerSecondDownstream} B/s");

await Task.Delay(1000);
}

});

}
Expand Down
2 changes: 2 additions & 0 deletions MewtocolNet/Mewtocol/DynamicInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ internal void AttachPoller () {

it++;

await Task.Delay(PollerDelayMs);

}

});
Expand Down
77 changes: 77 additions & 0 deletions MewtocolNet/Mewtocol/MewtocolInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public int ConnectTimeout {
set { connectTimeout = value; }
}

private int pollerDelayMs = 0;
/// <summary>
/// Delay for each poller cycle in milliseconds, default = 0
/// </summary>
public int PollerDelayMs {
get { return pollerDelayMs; }
set { pollerDelayMs = value; }
}

/// <summary>
/// The host ip endpoint, leave it null to use an automatic interface
/// </summary>
Expand Down Expand Up @@ -101,6 +110,9 @@ private set {
private int stationNumber;
private int cycleTimeMs = 25;

private int bytesTotalCountedUpstream = 0;
private int bytesTotalCountedDownstream = 0;

/// <summary>
/// The current IP of the PLC connection
/// </summary>
Expand All @@ -125,13 +137,40 @@ private set {
}
}

private int bytesPerSecondUpstream = 0;
/// <summary>
/// The current transmission speed in bytes per second
/// </summary>
public int BytesPerSecondUpstream {
get { return bytesPerSecondUpstream; }
private set {
bytesPerSecondUpstream = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondUpstream)));
}
}

private int bytesPerSecondDownstream = 0;
/// <summary>
/// The current transmission speed in bytes per second
/// </summary>
public int BytesPerSecondDownstream {
get { return bytesPerSecondDownstream; }
private set {
bytesPerSecondDownstream = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BytesPerSecondDownstream)));
}
}

internal NetworkStream stream;
internal TcpClient client;
internal readonly SerialQueue queue = new SerialQueue();
private int RecBufferSize = 128;
internal int SendExceptionsInRow = 0;
internal bool ImportantTaskRunning = false;

private Stopwatch speedStopwatchUpstr;
private Stopwatch speedStopwatchDownstr;

#region Initialization

/// <summary>
Expand Down Expand Up @@ -736,13 +775,30 @@ private async Task<string> SendSingleBlock (string _blockString) {

var message = _blockString.ToHexASCIIBytes();

//time measuring
if(speedStopwatchUpstr == null) {
speedStopwatchUpstr = Stopwatch.StartNew();
}

if(speedStopwatchUpstr.Elapsed.TotalSeconds >= 1) {
speedStopwatchUpstr.Restart();
bytesTotalCountedUpstream = 0;
}

//send request
using (var sendStream = new MemoryStream(message)) {
await sendStream.CopyToAsync(stream);
Logger.Log($"[--------------------------------]", LogLevel.Critical, this);
Logger.Log($"--> OUT MSG: {_blockString}", LogLevel.Critical, this);
}

//calc upstream speed
bytesTotalCountedUpstream += message.Length;

var perSecUpstream = (double)((bytesTotalCountedUpstream / speedStopwatchUpstr.Elapsed.TotalMilliseconds) * 1000);
if (perSecUpstream <= 10000)
BytesPerSecondUpstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);

//await result
StringBuilder response = new StringBuilder();
try {
Expand All @@ -755,6 +811,17 @@ private async Task<string> SendSingleBlock (string _blockString) {
while (!endLineCode && !startMsgCode) {

do {

//time measuring
if (speedStopwatchDownstr == null) {
speedStopwatchDownstr = Stopwatch.StartNew();
}

if (speedStopwatchDownstr.Elapsed.TotalSeconds >= 1) {
speedStopwatchDownstr.Restart();
bytesTotalCountedDownstream = 0;
}

int bytes = await stream.ReadAsync(responseBuffer, 0, responseBuffer.Length);

endLineCode = responseBuffer.Any(x => x == 0x0D);
Expand All @@ -777,8 +844,18 @@ private async Task<string> SendSingleBlock (string _blockString) {
}

if(!string.IsNullOrEmpty(response.ToString())) {

Logger.Log($"<-- IN MSG: {response}", LogLevel.Critical, this);

bytesTotalCountedDownstream += Encoding.ASCII.GetByteCount(response.ToString());

var perSecDownstream = (double)((bytesTotalCountedDownstream / speedStopwatchDownstr.Elapsed.TotalMilliseconds) * 1000);

if(perSecUpstream <= 10000)
BytesPerSecondDownstream = (int)Math.Round(perSecUpstream, MidpointRounding.AwayFromZero);

return response.ToString();

} else {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion MewtocolNet/MewtocolNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MewtocolNet</PackageId>
<Version>0.5.2</Version>
<Version>0.5.5</Version>
<Authors>Felix Weiss</Authors>
<Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
41 changes: 0 additions & 41 deletions MewtocolNet/Queue/SerialQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,6 @@ internal class SerialQueue {
readonly object _locker = new object();
readonly WeakReference<Task> _lastTask = new WeakReference<Task>(null);

internal Task Enqueue (Action action) {
return Enqueue<bool>(() => {
action();
return true;
});
}

internal Task<T> Enqueue<T> (Func<T> function) {
lock (_locker) {
Task lastTask;
Task<T> resultTask;

if (_lastTask.TryGetTarget(out lastTask)) {
resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously);
} else {
resultTask = Task.Run(function);
}

_lastTask.SetTarget(resultTask);

return resultTask;
}
}

internal Task Enqueue (Func<Task> asyncAction) {
lock (_locker) {
Task lastTask;
Task resultTask;

if (_lastTask.TryGetTarget(out lastTask)) {
resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap();
} else {
resultTask = Task.Run(asyncAction);
}

_lastTask.SetTarget(resultTask);

return resultTask;
}
}

internal Task<T> Enqueue<T> (Func<Task<T>> asyncFunction) {
lock (_locker) {
Task lastTask;
Expand Down

0 comments on commit 6f8f891

Please sign in to comment.