Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
* first steps in async
Browse files Browse the repository at this point in the history
  • Loading branch information
festo-i40 committed Jan 8, 2024
1 parent 3517666 commit 07dabe1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
10 changes: 3 additions & 7 deletions src/AasxPluginAssetInterfaceDesc/AidHttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ override public void Close()
// nothing to do, this simple http connection is stateless
}

override public int UpdateItemValue(AidIfxItemStatus item)
override public async Task<int> UpdateItemValueAsync(AidIfxItemStatus item)
{
// access
if (item?.FormData?.Href?.HasContent() != true
Expand All @@ -67,16 +67,12 @@ override public int UpdateItemValue(AidIfxItemStatus item)
var url = new Uri(TargetUri, item.FormData.Href);

// get response (synchronously)
var task = Task.Run(() => Client.GetAsync(url));
task.Wait();
var response = task.Result;
var response = await Client.GetAsync(url);

// ok?
if (response.IsSuccessStatusCode)
{
var task2 = Task.Run(() => response.Content.ReadAsStringAsync());
task2.Wait();
var strval = task2.Result;
var strval = await response.Content.ReadAsStringAsync();
item.Value = strval;
res = 1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/AasxPluginAssetInterfaceDesc/AidInterfaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AasxPluginAssetInterfaceDescription
{
Expand All @@ -44,7 +45,7 @@ public void StartOperation(LogInstance log, AidAllInterfaceStatus allInterfaceSt
_allInterfaceStatus = allInterfaceStatus;

_dispatcherTimer = new System.Timers.Timer(_timerTickMs);
_dispatcherTimer.Elapsed += DispatcherTimer_Tick;
_dispatcherTimer.Elapsed += DispatcherTimer_TickAsync;
_dispatcherTimer.Enabled = true;
_dispatcherTimer.Start();
}
Expand All @@ -55,7 +56,7 @@ public void StartOperation(LogInstance log, AidAllInterfaceStatus allInterfaceSt

private bool _inDispatcherTimer = false;

private void DispatcherTimer_Tick(object sender, EventArgs e)
private async void DispatcherTimer_TickAsync(object sender, EventArgs e)
{
// access
if (_allInterfaceStatus == null || _inDispatcherTimer)
Expand All @@ -67,15 +68,14 @@ private void DispatcherTimer_Tick(object sender, EventArgs e)
// call cyclic tasks
try
{
_allInterfaceStatus.UpdateValuesContinousByTick();
await _allInterfaceStatus.UpdateValuesContinousByTickAsyc();
} catch (Exception ex)
{
;
}

// release mutex
_inDispatcherTimer = false;

}
}
}
30 changes: 27 additions & 3 deletions src/AasxPluginAssetInterfaceDesc/AidInterfaceStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,28 @@ virtual public void Close()
}

/// <summary>
/// Tris to update the value (by polling).
/// Tries to update the value (by polling).
/// </summary>
/// <returns>Number of values changed</returns>
virtual public int UpdateItemValue(AidIfxItemStatus item)
{
return 0;
}

/// <summary>
/// Tries to update the value (by polling).
/// </summary>
/// <returns>Number of values changed</returns>
virtual public async Task<int> UpdateItemValueAsync(AidIfxItemStatus item)
{
await Task.Yield();
return 0;
}

// <summary>
/// Tries to update the value (by polling). Async opion is preferred.
/// </summary>
/// <returns>Number of values changed</returns>
virtual public void PrepareContinousRun(IEnumerable<AidIfxItemStatus> items)
{

Expand Down Expand Up @@ -401,7 +415,7 @@ public void StopContinousRun()
/// <summary>
/// In continous run, will fetch values for polling based technologies (HTTP, Modbus, ..).
/// </summary>
public void UpdateValuesContinousByTick()
public async Task UpdateValuesContinousByTickAsyc()
{
// access allowed
if (!ContinousRun)
Expand All @@ -421,9 +435,19 @@ public void UpdateValuesContinousByTick()
if (ifc?.Connection?.IsConnected() != true)
continue;

// go thru all items
// go thru all items (sync)
foreach (var item in ifc.Items.Values)
ifc.ValueChanges += (UInt64) ifc.Connection.UpdateItemValue(item);

// go thru all items (async)
// see: https://www.hanselman.com/blog/parallelforeachasync-in-net-6
await Parallel.ForEachAsync(
ifc.Items.Values,
new ParallelOptions() { MaxDegreeOfParallelism = 10 },
async (item, token) =>
{
ifc.ValueChanges += (UInt64) (await ifc.Connection.UpdateItemValueAsync(item));
});
}
}
}
Expand Down

0 comments on commit 07dabe1

Please sign in to comment.