Skip to content

Commit

Permalink
Reorganize tag creation of OPC-UA Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
riclolsen committed Jul 27, 2024
1 parent f961264 commit d6374bb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using Technosoftware.DaAeHdaClient;
using Technosoftware.DaAeHdaClient.Da;
Expand Down Expand Up @@ -67,8 +66,6 @@ public static void OnDataChangeEvent(object subscriptionHandle, object requestHa
isGood = isGood,
conn_number = srv.protocolConnectionNumber,
conn_name = srv.name,
common_address = "",
branch_name = "",
display_name = values[i].ItemName,
};
OPCDataQueue.Enqueue(ov);
Expand Down Expand Up @@ -201,7 +198,7 @@ public static void convertItemValue(object iv, out double value, out string valu
}
}

public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List<TsCDaItem> itemsForGroup, ref List<string> topics, ref OPCDA_connection srv, string branch_name)
public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List<TsCDaItem> itemsBrowsed, ref OPCDA_connection srv, string branch_name)
{
TsCDaBrowsePosition position = null;
TsCDaBrowseFilters filters = new TsCDaBrowseFilters();
Expand All @@ -216,10 +213,11 @@ public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List
foreach (TsCDaBrowseElement elem in elements)
{
item = new OpcItem(elem.ItemPath, elem.ItemName);
if (elem.GetType() == typeof(TsCDaBrowseElement) && elem.HasChildren && (topics.Count == 0 || topics.Contains(elem.Name)))
// srv.topics.Length==0 : read all branches
if (elem.GetType() == typeof(TsCDaBrowseElement) && elem.HasChildren && (srv.topics.Length == 0 || srv.branches.Contains(elem.Name)))
{
srv.MapItemNameToBranch[item.ItemName] = branch_name;
BrowseServer(ref daServer, item, ref itemsForGroup, ref topics, ref srv, item.ItemName);
// srv.MapItemNameToBranch[item.ItemName] = branch_name;
BrowseServer(ref daServer, item, ref itemsBrowsed, ref srv, item.ItemName);
}

if (!elem.HasChildren)
Expand All @@ -229,10 +227,9 @@ public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List
it.ClientHandle = HandleCnt;
srv.MapHandlerToItemName[it.ClientHandle.ToString()] = it.ItemName;
srv.MapHandlerToConnName[it.ClientHandle.ToString()] = daServer.ClientName;
srv.MapItemNameToBranch[it.ItemName] = branch_name;

// srv.MapItemNameToBranch[it.ItemName] = branch_name;
// MapNameToHandler[it.ItemName] = it.ClientHandle.ToString();
itemsForGroup.Add(it);
itemsBrowsed.Add(it);
}
}
if (position != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public class
// public static Dictionary<string, string> MapNameToHandler = new Dictionary<string, string>();
public Dictionary<string, string> MapHandlerToItemName = new Dictionary<string, string>();
public Dictionary<string, string> MapHandlerToConnName = new Dictionary<string, string>();
public Dictionary<string, string> MapItemNameToBranch = new Dictionary<string, string>();

// public Dictionary<string, string> MapItemNameToBranch = new Dictionary<string, string>();
public SortedSet<string> branches = new SortedSet<string>(); // branches to scan
public TsCDaServer connection = null;
public Thread thrOPCStack = null;
public int cntConnectRetries = 0;
Expand Down Expand Up @@ -175,7 +175,10 @@ public struct OPC_Value
public string conn_name;
public string common_address;
public string display_name;
public string branch_name;
public string group1;
public string group2;
public string group3;
public string ungroupedDescription;
}
public class rtFilt
{
Expand Down
53 changes: 45 additions & 8 deletions src/opcdaaehda-client-solution-net/OPC-DA-Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection.Metadata.Ecma335;
using System.Text.Json;
Expand All @@ -29,14 +30,15 @@
using Technosoftware.DaAeHdaClient;
using Technosoftware.DaAeHdaClient.Da;
using static MongoDB.Driver.WriteConcern;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace OPCDAClientDriver
{
partial class MainClass
{
public static String CopyrightMessage = "{json:scada} OPC-DA Client Driver - Copyright 2021-2024 RLO";
public static String ProtocolDriverName = "OPC-DA";
public static String DriverVersion = "0.1.0";
public static string CopyrightMessage = "{json:scada} OPC-DA Client Driver - Copyright 2021-2024 RLO";
public static string ProtocolDriverName = "OPC-DA";
public static string DriverVersion = "0.1.0";
public static bool Active = false; // indicates this driver instance is the active node in the moment
public static int DataBufferLimit = 20000; // limit to start dequeuing and discarding data from the acquisition buffer
public static int BulkWriteLimit = 1250; // limit of each bulk write to mongodb
Expand Down Expand Up @@ -194,6 +196,7 @@ public static void Main(string[] args)

foreach (OPCDA_connection srv in conns)
{
List<string> topics = new List<string>(srv.topics);
// look for existing tags in this connections, if autotag enabled missing tags will be inserted later when discovered
var results = collRtData.Find<rtData>(new BsonDocument {
{ "protocolSourceConnectionNumber", srv.protocolConnectionNumber },
Expand All @@ -202,9 +205,12 @@ public static void Main(string[] args)
{ "_id", 1 },
}).ToList();

// put in branches names found in topics from connection plus protocolSourceCommonAddress from tags
foreach (var topic in srv.topics) srv.branches.Add(topic);
srv.LastNewKeyCreated = AutoKeyMultiplier * srv.protocolConnectionNumber;
for (int i = 0; i < results.Count; i++)
{
if (results[i].protocolSourceCommonAddress.ToString() != string.Empty) srv.branches.Add(results[i].protocolSourceCommonAddress.ToString());
srv.InsertedTags.Add(results[i].tag.ToString());
srv.InsertedAddresses.Add(results[i].protocolSourceObjectAddress.ToString());
if (results[i]._id.ToDouble() > srv.LastNewKeyCreated)
Expand Down Expand Up @@ -292,7 +298,6 @@ public static void Main(string[] args)
// "opcda://localhost/Matrikon.OPC.Simulation.1"; "opcda://localhost/SampleCompany.DaSample.30";
var serverUrl = srv.endpointURLs[srv.cntConnectRetries % srv.endpointURLs.Length];
srv.cntConnectRetries++;
List<string> topics = new List<string>(srv.topics);

var opcUrl = new OpcUrl(serverUrl);
if (opcUrl.Scheme != OpcUrlScheme.DA)
Expand Down Expand Up @@ -358,11 +363,22 @@ public static void Main(string[] args)

var itemsBrowsed = new List<TsCDaItem>();
var itemsForGroup = new List<TsCDaItem>();
BrowseServer(ref daServer, null, ref itemsBrowsed, ref topics, ref srv, "root");
BrowseServer(ref daServer, null, ref itemsBrowsed, ref srv, "root");

// will read only data wanted
for (int i = 0; i < itemsBrowsed.Count; i++)
{
//itemsBrowsed[i].Deadband = 10;
//itemsBrowsed[i].DeadbandSpecified = true;
//itemsBrowsed[i].SamplingRate = 10;
//itemsBrowsed[i].SamplingRateSpecified = true;
//itemsBrowsed[i].Active = true;
//itemsBrowsed[i].ActiveSpecified = true;
//itemsBrowsed[i].EnableBuffering = false;
//itemsBrowsed[i].EnableBufferingSpecified = true;
//itemsBrowsed[i].MaxAge = 0;
//itemsBrowsed[i].MaxAgeSpecified = true;

if (srv.InsertedAddresses.Contains(itemsBrowsed[i].ItemName))
{
itemsForGroup.Add(itemsBrowsed[i]);
Expand Down Expand Up @@ -398,7 +414,25 @@ public static void Main(string[] args)
isGood = itemValues[i].Quality.QualityBits.HasFlag(TsDaQualityBits.Good) && isGood;
if (LogLevel > LogLevelDetailed)
Log($"{srv.name} - {itemValues[i].ItemName} {valueString} {itemValues[i].Quality} {itemValues[i].Value.GetType().Name}", LogLevelDetailed);


var common_address = "";
var lstDot = itemValues[i].ItemName.LastIndexOf(".");
var spl = itemValues[i].ItemName.Split(".");
string group2 = "@root", group3 = "", ungroupedDescription = itemValues[i].ItemName.Substring(lstDot+1);
if (lstDot != -1)
{
common_address = itemValues[i].ItemName.Substring(0, lstDot);
}
if (spl.Length > 1)
{
group2 = spl[0];
}
if(spl.Length > 2)
{
for (var j= 1; j < spl.Length-1; j++)
group3 += spl[j] + "/";
group3 = group3.Substring(0, group3.Length-1);
}
var ov = new OPC_Value()
{
valueJson = valueJson,
Expand All @@ -416,9 +450,12 @@ public static void Main(string[] args)
isGood = isGood,
conn_number = srv.protocolConnectionNumber,
conn_name = srv.name,
common_address = srv.MapItemNameToBranch[itemValues[i].ItemName],
common_address = common_address,
display_name = itemValues[i].ItemName,
branch_name = srv.MapItemNameToBranch[itemValues[i].ItemName],
group1 = srv.name,
group2 = group2,
group3 = group3,
ungroupedDescription = ungroupedDescription,
};

if (srv.autoCreateTags && !srv.InsertedAddresses.Contains(itemValues[i].ItemName))
Expand Down
31 changes: 16 additions & 15 deletions src/opcdaaehda-client-solution-net/OPC-DA-Client/TagsCreation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

using System;
using System.Text.RegularExpressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
Expand Down Expand Up @@ -198,11 +199,11 @@ public static rtData newRealtimeDoc(OPC_Value iv, double _id)
protocolSourceQueueSize = 10.0,
protocolSourceDiscardOldest = true,
alarmState = 2.0,
description = iv.conn_name + "~" + iv.branch_name + "~" + iv.display_name,
ungroupedDescription = iv.display_name,
group1 = iv.conn_name,
group2 = iv.branch_name,
group3 = iv.asdu,
description = iv.group1 + "~" + iv.group2 + "~" + iv.display_name,
ungroupedDescription = iv.ungroupedDescription,
group1 = iv.group1,
group2 = iv.group2,
group3 = iv.group3,
stateTextFalse = "FALSE",
stateTextTrue = "TRUE",
eventTextFalse = "FALSE",
Expand Down Expand Up @@ -274,11 +275,11 @@ public static rtData newRealtimeDoc(OPC_Value iv, double _id)
protocolSourceQueueSize = 10.0,
protocolSourceDiscardOldest = true,
alarmState = -1.0,
description = iv.conn_name + "~" + iv.branch_name + "~" + iv.display_name,
ungroupedDescription = iv.display_name,
group1 = iv.conn_name,
group2 = iv.branch_name,
group3 = iv.asdu,
description = iv.group1 + "~" + iv.group2 + "~" + iv.display_name,
ungroupedDescription = iv.ungroupedDescription,
group1 = iv.group1,
group2 = iv.group2,
group3 = iv.group3,
stateTextFalse = "",
stateTextTrue = "",
eventTextFalse = "",
Expand Down Expand Up @@ -349,11 +350,11 @@ public static rtData newRealtimeDoc(OPC_Value iv, double _id)
protocolSourceQueueSize = 10.0,
protocolSourceDiscardOldest = true,
alarmState = -1.0,
description = iv.conn_name + "~" + iv.branch_name + "~" + iv.display_name,
ungroupedDescription = iv.display_name,
group1 = iv.conn_name,
group2 = iv.branch_name,
group3 = iv.asdu,
description = iv.group1 + "~" + iv.group2 + "~" + iv.display_name,
ungroupedDescription = iv.ungroupedDescription,
group1 = iv.group1,
group2 = iv.group2,
group3 = iv.group3,
stateTextFalse = "",
stateTextTrue = "",
eventTextFalse = "",
Expand Down

0 comments on commit d6374bb

Please sign in to comment.