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

Commit

Permalink
* continous run
Browse files Browse the repository at this point in the history
  for http 1st gen
  • Loading branch information
festo-i40 committed Jan 8, 2024
1 parent 8d97e2f commit 3517666
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 30 deletions.
14 changes: 12 additions & 2 deletions src/AasxCsharpLibrary/AdminShellCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,25 @@ public void Remove(K key)

public List<V> this[K key] => dict[key];

public IEnumerable<List<V>> Values
public IEnumerable<List<V>> ValueLists
{
get
{
return dict.Values;
}
}

public IEnumerable<K> Keys
public IEnumerable<V> Values
{
get
{
foreach (var vl in dict.Values)
foreach (var v in vl)
yield return v;
}
}

public IEnumerable<K> Keys
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentModbus" Version="5.0.3" />
<PackageReference Include="MQTTnet" Version="4.3.3.952" />
<PackageReference Include="MQTTnet" Version="4.1.1.318" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>
8 changes: 6 additions & 2 deletions src/AasxPluginAssetInterfaceDesc/AidHttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ override public void Close()
// nothing to do, this simple http connection is stateless
}

override public void UpdateItemValue(AidIfxItemStatus item)
override public int UpdateItemValue(AidIfxItemStatus item)
{
// access
if (item?.FormData?.Href?.HasContent() != true
|| item.FormData.Htv_methodName?.HasContent() != true
|| !IsConnected())
return;
return 0;
int res = 0;

// GET?
if (item.FormData.Htv_methodName.Trim().ToLower() == "get")
Expand All @@ -77,12 +78,15 @@ override public void UpdateItemValue(AidIfxItemStatus item)
task2.Wait();
var strval = task2.Result;
item.Value = strval;
res = 1;
}
} catch (Exception ex)
{
;
}
}

return res;
}
}
}
9 changes: 8 additions & 1 deletion src/AasxPluginAssetInterfaceDesc/AidInterfaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ private void DispatcherTimer_Tick(object sender, EventArgs e)
// block
_inDispatcherTimer = true;

// ..
// call cyclic tasks
try
{
_allInterfaceStatus.UpdateValuesContinousByTick();
} catch (Exception ex)
{
;
}

// release mutex
_inDispatcherTimer = false;
Expand Down
151 changes: 145 additions & 6 deletions src/AasxPluginAssetInterfaceDesc/AidInterfaceStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
using System.Net;
using System.Text.RegularExpressions;
using System.Globalization;
using AnyUi;

namespace AasxPluginAssetInterfaceDescription
{
Expand Down Expand Up @@ -61,6 +62,11 @@ public class AidIfxItemStatus
/// Link to entity (property, action, event).
/// </summary>
public object Tag = null;

/// <summary>
/// Holds reference to the AnyUI element showing the value.
/// </summary>
public AnyUiUIElement RenderedUiElement = null;
}

public enum AidInterfaceTechnology { HTTP, Modbus, MQTT }
Expand All @@ -86,7 +92,8 @@ public class AidInterfaceStatus
/// <summary>
/// The information items (properties, actions, events)
/// </summary>
public List<AidIfxItemStatus> Items = new List<AidIfxItemStatus>();
public MultiValueDictionary<string, AidIfxItemStatus> Items =
new MultiValueDictionary<string, AidIfxItemStatus>();

/// <summary>
/// Base connect information.
Expand All @@ -96,7 +103,7 @@ public class AidInterfaceStatus
/// <summary>
/// Actual summary of the status of the interface.
/// </summary>
public string LogLine = "";
public string LogLine = "Idle.";

/// <summary>
/// Black = idle, Blue = active, Red = error.
Expand All @@ -112,6 +119,52 @@ public class AidInterfaceStatus
/// Holds the technology connection currently used.
/// </summary>
public AidBaseConnection Connection = null;

/// <summary>
/// Will get increment, when a value changed.
/// </summary>
public UInt64 ValueChanges = 0;

protected string ComputeKey(string key)
{
if (key != null)
{
if (Technology == AidInterfaceTechnology.MQTT)
{
key = key.Trim().Trim('/').ToLower();
}
}
return key;
}

/// <summary>
/// Computes a technology specific key and adds item.
/// </summary>
/// <param name="item"></param>
public void AddItem(AidIfxItemStatus item)
{
// acceess
if (item == null)
return;

// compute key
var key = ComputeKey(item?.FormData?.Href);

// now add
Items.Add(key, item);
}

/// <summary>
/// Computes key based on technology, checks if items can be found
/// and enumerates these.
/// </summary>
public IEnumerable<AidIfxItemStatus> GetItemsFor(string key)
{
key = ComputeKey(key);
if (Items.ContainsKey(key))
foreach (var item in Items[key])
yield return item;
}
}

public class AidBaseConnection
Expand All @@ -120,6 +173,8 @@ public class AidBaseConnection

public DateTime LastActive = default(DateTime);

public Action<string, string> MessageReceived = null;

virtual public bool Open()
{
return false;
Expand All @@ -134,8 +189,13 @@ virtual public void Close()
{
}

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

virtual public void PrepareContinousRun(IEnumerable<AidIfxItemStatus> items)
Expand Down Expand Up @@ -166,7 +226,7 @@ public T GetOrCreate(string target)
/// </summary>
public class AidAllInterfaceStatus
{
public bool[] UseTech = { false, false, true };
public bool[] UseTech = { true, false, false };

/// <summary>
/// Will hold connections steady and continously update values, either by
Expand Down Expand Up @@ -206,6 +266,17 @@ protected AidBaseConnection GetOrCreate(AidInterfaceTechnology tech, string endp
return conn;
}

/// <summary>
/// Will get increment, when a value changed.
/// </summary>
public UInt64 SumValueChanges()
{
UInt64 sum = 0;
foreach (var ifc in InterfaceStatus)
sum += ifc.ValueChanges;
return sum;
}

/// <summary>
/// Will connect to each target once, get values and will disconnect again.
/// </summary>
Expand Down Expand Up @@ -240,7 +311,7 @@ public void UpdateValuesSingleShot()
ifc.Connection = conn;

// go thru all items
foreach (var item in ifc.Items)
foreach (var item in ifc.Items.Values)
conn.UpdateItemValue(item);
}
}
Expand All @@ -259,6 +330,9 @@ public void UpdateValuesSingleShot()
/// </summary>
public void StartContinousRun()
{
// off
ContinousRun = false;

// for all
foreach (var tech in AdminShellUtil.GetEnumValues<AidInterfaceTechnology>())
{
Expand All @@ -284,7 +358,72 @@ public void StartContinousRun()
ifc.Connection = conn;

// start subscriptions ..
conn.PrepareContinousRun(ifc.Items);
conn.MessageReceived = (topic, msg) =>
{
foreach (var ifc2 in InterfaceStatus)
foreach (var item in ifc2.GetItemsFor(topic))
{
// note value
item.Value = msg;
// note value change
ifc2.ValueChanges++;
// remember last use
if (ifc2.Connection != null)
ifc2.Connection.LastActive = DateTime.Now;
}
};
conn.PrepareContinousRun(ifc.Items.Values);
}
}

// now switch ON!
ContinousRun = true;
}

/// <summary>
/// Will stop continous run and close all connections.
/// </summary>
public void StopContinousRun()
{
// off
ContinousRun = false;

// close all connections
foreach (var ifc in InterfaceStatus)
{
if (ifc.Connection?.IsConnected() == true)
ifc.Connection.Close();
}
}

/// <summary>
/// In continous run, will fetch values for polling based technologies (HTTP, Modbus, ..).
/// </summary>
public void UpdateValuesContinousByTick()
{
// access allowed
if (!ContinousRun)
return;

// for all
foreach (var tech in AdminShellUtil.GetEnumValues<AidInterfaceTechnology>())
{
// use?
if (!UseTech[(int)tech])
continue;

// find all interfaces with that technology
foreach (var ifc in InterfaceStatus.Where((i) => i.Technology == tech))
{
// get a connection
if (ifc?.Connection?.IsConnected() != true)
continue;

// go thru all items
foreach (var item in ifc.Items.Values)
ifc.ValueChanges += (UInt64) ifc.Connection.UpdateItemValue(item);
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/AasxPluginAssetInterfaceDesc/AidModbusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,22 @@ override public void Close()
}
}

override public void UpdateItemValue(AidIfxItemStatus item)
override public int UpdateItemValue(AidIfxItemStatus item)
{
// access
if (item?.FormData?.Href?.HasContent() != true
|| item.FormData.Modbus_function?.HasContent() != true)
return;
return 0;
int res = 0;

// decode address + quantity
// (assumption: 1 quantity = 2 bytes)
var match = Regex.Match(item.FormData.Href, @"^(\d{1,5})(\?quantity=(\d+))?$");
if (!match.Success)
return;
return 0;

if (!int.TryParse(match.Groups[1].ToString(), out var address))
return;
return 0;
if (!int.TryParse(match.Groups[3].ToString(), out var quantity))
quantity = 1;
quantity = Math.Max(0, Math.Min(0xffff, quantity));
Expand All @@ -94,7 +95,7 @@ override public void UpdateItemValue(AidIfxItemStatus item)

// success with reading?
if (id == null || id.Length < 1)
return;
return 0;

// swapping (od = out data)
// https://doc.iobroker.net/#de/adapters/adapterref/iobroker.modbus/README.md?wp
Expand Down Expand Up @@ -188,6 +189,9 @@ override public void UpdateItemValue(AidIfxItemStatus item)

// save in item
item.Value = strval;

// ok
return 1;
}
}
}
8 changes: 5 additions & 3 deletions src/AasxPluginAssetInterfaceDesc/AidMqttConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ private async Task Client_ApplicationMessageReceivedAsync(MqttApplicationMessage
return;

// payload?
var payload = Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment);
// var payload = Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment);
var payload = arg.ApplicationMessage.ConvertPayloadToString();
if (payload?.HasContent() != true)
return;

// refer further..
;
MessageReceived?.Invoke(topic, payload);

// ok
await Task.Yield();
Expand All @@ -113,10 +114,11 @@ override public void Close()
}
}

override public void UpdateItemValue(AidIfxItemStatus item)
override public int UpdateItemValue(AidIfxItemStatus item)
{
// Cannot do anything. MQTT is pure publish/ subscribe.
// Unable to ask for a status value.
return 0;
}

override public void PrepareContinousRun(IEnumerable<AidIfxItemStatus> items)
Expand Down
Loading

0 comments on commit 3517666

Please sign in to comment.