Added methods to pause/resume the auto polling

This commit is contained in:
Felix Weiß
2022-09-23 16:38:14 +02:00
parent 88a453355c
commit c7a6559f97
3 changed files with 162 additions and 84 deletions

View File

@@ -32,7 +32,7 @@ class Program {
Task.Factory.StartNew(async () => { Task.Factory.StartNew(async () => {
//attaching the logger //attaching the logger
Logger.LogLevel = LogLevel.Verbose; Logger.LogLevel = LogLevel.Critical;
Logger.OnNewLogMessage((date, msg) => { Logger.OnNewLogMessage((date, msg) => {
Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}"); Console.WriteLine($"{date.ToString("HH:mm:ss")} {msg}");
}); });
@@ -44,6 +44,13 @@ class Program {
//attaching the register collection and an automatic poller //attaching the register collection and an automatic poller
interf.WithRegisterCollection(registers).WithPoller(); interf.WithRegisterCollection(registers).WithPoller();
_ = Task.Factory.StartNew(async () => {
while (true) {
Console.Title = $"Polling Paused: {interf.PollingPaused}, Speed UP: {interf.BytesPerSecondUpstream} B/s, Speed DOWN: {interf.BytesPerSecondDownstream} B/s";
await Task.Delay(1000);
}
});
await interf.ConnectAsync( await interf.ConnectAsync(
(plcinf) => { (plcinf) => {
@@ -79,14 +86,28 @@ class Program {
//set the current second to the PLCs TIME register //set the current second to the PLCs TIME register
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second)); interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
//test pausing poller
bool pollerPaused = false;
while(true) { while(true) {
Console.WriteLine($"Speed UP: {interf.BytesPerSecondUpstream} B/s"); await Task.Delay(5000);
Console.WriteLine($"Speed DOWN: {interf.BytesPerSecondDownstream} B/s");
await Task.Delay(1000); pollerPaused = !pollerPaused;
if(pollerPaused) {
Console.WriteLine("Pausing poller");
await interf.PausePollingAsync();
Console.WriteLine("Paused poller");
} else {
interf.ResumePolling();
Console.WriteLine("Resumed poller");
} }
}
}); });
} }

View File

@@ -14,15 +14,61 @@ namespace MewtocolNet {
/// </summary> /// </summary>
public partial class MewtocolInterface { public partial class MewtocolInterface {
/// <summary>
/// True if the auto poller is currently paused
/// </summary>
public bool PollingPaused => pollerIsPaused;
internal event Action PolledCycle; internal event Action PolledCycle;
internal bool ContinousReaderRunning;
internal volatile bool pollerTaskRunning;
internal volatile bool pollerTaskStopped;
internal volatile bool pollerIsPaused;
internal bool usePoller = false; internal bool usePoller = false;
#region Register Polling #region Register Polling
/// <summary>
/// Kills the poller completely
/// </summary>
internal void KillPoller () { internal void KillPoller () {
ContinousReaderRunning = false; pollerTaskRunning = false;
pollerTaskStopped = true;
}
/// <summary>
/// Pauses the polling and waits for the last message to be sent
/// </summary>
/// <returns></returns>
public async Task PausePollingAsync () {
if (!pollerTaskRunning)
return;
pollerTaskRunning = false;
while (!pollerIsPaused) {
if (pollerIsPaused)
break;
await Task.Delay(10);
}
pollerTaskRunning = false;
}
/// <summary>
/// Resumes the polling
/// </summary>
public void ResumePolling () {
pollerTaskRunning = true;
} }
@@ -31,32 +77,37 @@ namespace MewtocolNet {
/// </summary> /// </summary>
internal void AttachPoller () { internal void AttachPoller () {
if (ContinousReaderRunning) if (pollerTaskRunning)
return; return;
Task.Factory.StartNew(async () => { Task.Factory.StartNew(async () => {
Logger.Log("Poller is attaching", LogLevel.Info, this); Logger.Log("Poller is attaching", LogLevel.Info, this);
int it = 0; int iteration = 0;
ContinousReaderRunning = true;
while (ContinousReaderRunning) { pollerTaskStopped = false;
pollerTaskRunning = true;
pollerIsPaused = false;
if (it >= Registers.Count + 1) { while (!pollerTaskStopped) {
it = 0;
while (pollerTaskRunning) {
if (iteration >= Registers.Count + 1) {
iteration = 0;
//invoke cycle polled event //invoke cycle polled event
InvokePolledCycleDone(); InvokePolledCycleDone();
continue; continue;
} }
if (it >= Registers.Count) { if (iteration >= Registers.Count) {
await GetPLCInfoAsync(); await GetPLCInfoAsync();
it++; iteration++;
continue; continue;
} }
var reg = Registers[it]; var reg = Registers[iteration];
if (reg is NRegister<short> shortReg) { if (reg is NRegister<short> shortReg) {
var lastVal = shortReg.Value; var lastVal = shortReg.Value;
@@ -115,12 +166,18 @@ namespace MewtocolNet {
} }
} }
it++; iteration++;
await Task.Delay(PollerDelayMs); await Task.Delay(PollerDelayMs);
} }
pollerIsPaused = !pollerTaskRunning;
}
pollerIsPaused = false;
}); });
} }

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MewtocolNet</PackageId> <PackageId>MewtocolNet</PackageId>
<Version>0.5.5</Version> <Version>0.5.6</Version>
<Authors>Felix Weiss</Authors> <Authors>Felix Weiss</Authors>
<Company>Womed</Company> <Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>