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

Commit

Permalink
* progress
Browse files Browse the repository at this point in the history
  • Loading branch information
festo-i40 committed Jan 8, 2024
1 parent 07dabe1 commit a3fce14
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/AasxPluginAssetInterfaceDesc/AidInterfaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ private async void DispatcherTimer_TickAsync(object sender, EventArgs e)
// block
_inDispatcherTimer = true;

// call cyclic tasks
// call cyclic tasks
try
{
await _allInterfaceStatus.UpdateValuesContinousByTickAsyc();
// synchronous
await _allInterfaceStatus.UpdateValuesContinousByTickAsyc();
} catch (Exception ex)
{
;
Expand Down
39 changes: 38 additions & 1 deletion src/AasxPluginAssetInterfaceDesc/AidInterfaceStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ protected string ComputeKey(string key)
return key;
}

public void SetLogLine (StoredPrint.Color color, string line)
{
LogColor = color;
LogLine = line;
}

/// <summary>
/// Computes a technology specific key and adds item.
/// </summary>
Expand Down Expand Up @@ -299,6 +305,7 @@ public void UpdateValuesSingleShot()
// access allowed
if (ContinousRun)
return;
SetAllLogIdle();

// for all
foreach (var tech in AdminShellUtil.GetEnumValues<AidInterfaceTechnology>())
Expand All @@ -324,9 +331,23 @@ public void UpdateValuesSingleShot()
continue;
ifc.Connection = conn;

// go thru all items
// go thru all items (sync)
foreach (var item in ifc.Items.Values)
conn.UpdateItemValue(item);

// go thru all items (async)
var task = Task.Run(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));
});
});
task.Wait();
}
}

Expand All @@ -338,6 +359,12 @@ public void UpdateValuesSingleShot()
}
}

protected void SetAllLogIdle()
{
foreach (var ifc in InterfaceStatus)
ifc.SetLogLine(StoredPrint.Color.Black, "Idle.");
}

/// <summary>
/// Will connect to each target, leave the connection open, will enable
/// cyclic updates.
Expand All @@ -346,6 +373,7 @@ public void StartContinousRun()
{
// off
ContinousRun = false;
SetAllLogIdle();

// for all
foreach (var tech in AdminShellUtil.GetEnumValues<AidInterfaceTechnology>())
Expand All @@ -359,7 +387,10 @@ public void StartContinousRun()
{
// get a connection
if (ifc.EndpointBase?.HasContent() != true)
{
ifc.SetLogLine(StoredPrint.Color.Red, "Endpoint is not specified.");
continue;
}

// find connection by factory
AidBaseConnection conn = GetOrCreate(tech, ifc.EndpointBase);
Expand All @@ -368,8 +399,12 @@ public void StartContinousRun()

// open it
if (!conn.Open())
{
ifc.SetLogLine(StoredPrint.Color.Red, $"Endpoint connot be opened: {ifc.EndpointBase}.");
continue;
}
ifc.Connection = conn;
ifc.SetLogLine(StoredPrint.Color.Blue, "Connection established.");

// start subscriptions ..
conn.MessageReceived = (topic, msg) =>
Expand All @@ -389,6 +424,7 @@ public void StartContinousRun()
}
};
conn.PrepareContinousRun(ifc.Items.Values);
ifc.SetLogLine(StoredPrint.Color.Blue, "Connection established and prepared.");
}
}

Expand All @@ -403,6 +439,7 @@ public void StopContinousRun()
{
// off
ContinousRun = false;
SetAllLogIdle();

// close all connections
foreach (var ifc in InterfaceStatus)
Expand Down
3 changes: 2 additions & 1 deletion src/AasxPluginAssetInterfaceDesc/AidModbusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ override public void Close()
}
}

// Note: the async version of ReadHoldingRegisters seems not to work properly?
override public int UpdateItemValue(AidIfxItemStatus item)
{
// access
Expand All @@ -88,7 +89,7 @@ override public int UpdateItemValue(AidIfxItemStatus item)
if (item.FormData.Modbus_function.Trim().ToLower() == "readholdingregisters")
{
// readHoldingRegisters
id = Client.ReadHoldingRegisters<byte>(99, address, 2 * quantity).ToArray();
id = (Client.ReadHoldingRegisters<byte>(99, address, 2 * quantity)).ToArray();
// time
LastActive = DateTime.Now;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void Start(
assembly: Assembly.GetExecutingAssembly()));
_dictTechnologyToBitmap.Add(AidInterfaceTechnology.MQTT,
AnyUiGdiHelper.CreateAnyUiBitmapFromResource(
"AasxPluginAssetInterfaceDesc.Resources.logo-mqqt.png",
"AasxPluginAssetInterfaceDesc.Resources.logo-mqtt.png",
assembly: Assembly.GetExecutingAssembly()));
}

Expand Down

0 comments on commit a3fce14

Please sign in to comment.