Skip to content

Commit

Permalink
OPC-DA Client write commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
riclolsen committed Jul 27, 2024
1 parent 62bcfc9 commit 2ca7478
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,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> itemsBrowsed, ref OPCDA_connection srv, string branch_name)
public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List<TsCDaItem> itemsBrowsed, ref OPCDA_connection srv)
{
TsCDaBrowsePosition position = null;
TsCDaBrowseFilters filters = new TsCDaBrowseFilters();
Expand All @@ -216,8 +216,7 @@ public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List
// 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 itemsBrowsed, ref srv, item.ItemName);
BrowseServer(ref daServer, item, ref itemsBrowsed, ref srv);
}

if (!elem.HasChildren)
Expand All @@ -227,7 +226,6 @@ 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;
// MapNameToHandler[it.ItemName] = it.ClientHandle.ToString();
itemsBrowsed.Add(it);
}
Expand Down
100 changes: 30 additions & 70 deletions src/opcdaaehda-client-solution-net/OPC-DA-Client/MongoCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading;
using Technosoftware.DaAeHdaClient;
using Technosoftware.DaAeHdaClient.Da;
using static MongoDB.Libmongocrypt.CryptContext;

namespace OPCDAClientDriver
{
Expand Down Expand Up @@ -131,109 +134,65 @@ await collection

break;
}
/*

if (
srv.connection.session.Connected &&
srv.connection.IsConnected &&
srv.commandsEnabled
)
{
WriteValueCollection nodesToWrite = new WriteValueCollection();
WriteValue WriteVal = new WriteValue();
WriteVal.NodeId =
new NodeId(System
.Convert
.ToString(change.FullDocument.protocolSourceObjectAddress));
WriteVal.AttributeId = Attributes.Value;
WriteVal.Value = new DataValue();
var item = new OpcItem(change.FullDocument.protocolSourceObjectAddress.ToString());
var daItem = new TsCDaItem(item);
var itVal = new TsCDaItemValueResult(item);
switch (change.FullDocument.protocolSourceASDU.ToString().ToLower())
{
case "boolean":
WriteVal.Value.Value = System.Convert.ToBoolean(System
.Convert.ToDouble(change.FullDocument.value) != 0.0);
itVal.Value = Convert.ToBoolean(Convert.ToDouble(change.FullDocument.value) != 0.0);
break;
case "sbyte":
WriteVal.Value.Value = System
.Convert
.ToSByte(change.FullDocument.value);
itVal.Value = Convert.ToSByte(change.FullDocument.value);
break;
case "byte":
WriteVal.Value.Value = System
.Convert
.ToByte(change.FullDocument.value);
itVal.Value = Convert.ToByte(change.FullDocument.value);
break;
case "int16":
WriteVal.Value.Value = System
.Convert
.ToInt16(change.FullDocument.value);
itVal.Value = Convert.ToInt16(change.FullDocument.value);
break;
case "uint16":
WriteVal.Value.Value = System
.Convert
.ToUInt16(change.FullDocument.value);
itVal.Value = Convert.ToUInt16(change.FullDocument.value);
break;
case "int32":
WriteVal.Value.Value = System
.Convert
.ToInt16(change.FullDocument.value);
itVal.Value = Convert.ToInt32(change.FullDocument.value);
break;
case "uint32":
WriteVal.Value.Value = System
.Convert
.ToUInt16(change.FullDocument.value);
itVal.Value = Convert.ToUInt32(change.FullDocument.value);
break;
case "int64":
WriteVal.Value.Value = System
.Convert
.ToInt16(change.FullDocument.value);
itVal.Value = Convert.ToInt64(change.FullDocument.value);
break;
case "uint64":
WriteVal.Value.Value = System
.Convert
.ToUInt16(change.FullDocument.value);
itVal.Value = Convert.ToUInt64(change.FullDocument.value);
break;
case "float":
WriteVal.Value.Value = System
.Convert
.ToSingle(change.FullDocument.value);
itVal.Value = Convert.ToSingle(change.FullDocument.value);
break;
case "double":
WriteVal.Value.Value = System
.Convert
.ToDouble(change.FullDocument.value);
itVal.Value = Convert.ToDouble(change.FullDocument.value);
break;
case "datetime":
WriteVal.Value.Value = System
.Convert
.ToDateTime(change.FullDocument.value);
itVal.Value = Convert.ToDateTime(change.FullDocument.value);
break;
case "string":
WriteVal.Value.Value = System
.Convert
.ToString(change.FullDocument.value);
itVal.Value = Convert.ToString(change.FullDocument.value);
break;
}
nodesToWrite.Add(WriteVal);
// Write the node attributes
StatusCodeCollection results = null;
DiagnosticInfoCollection diagnosticInfos;
Log("MongoDB CMD CS - " + srv.name + " - Writing node...");
// Call Write Service
srv.connection.session.Write(null,
nodesToWrite,
out results,
out diagnosticInfos);
Log($"MongoDB CMD CS - {srv.name} - Writing node: {itVal.ItemName} value: {itVal.Value}" );
var results = srv.connection.Write(new TsCDaItemValue[] { itVal });
var okres = false;
var resultDescription = "";
if (results.Count > 0)
var resultDescription = "Error!";
if (results != null && results.Length > 0)
{
resultDescription = results[0].ToString();
if (StatusCode.IsGood(results[0]))
resultDescription = results[0].Result.Description();
if (results[0].Result.IsOk())
okres = true;
}

Expand All @@ -244,7 +203,7 @@ await collection
change
.FullDocument
.protocolSourceObjectAddress +
" - Command delivered - " + results[0].ToString());
" - Command delivered - " + resultDescription);

// update as delivered
var filter =
Expand All @@ -266,9 +225,11 @@ await collection
await collection
.UpdateOneAsync(filter,
update);

}
else
{

// update as canceled (not connected)
Log("MongoDB CMD CS - " +
srv.name +
Expand Down Expand Up @@ -302,7 +263,6 @@ await collection
.UpdateOneAsync(filter,
update);
}
*/
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ static public async void ProcessMongo(JSONSCADAConfig jsConfig)
var filt =
new rtFilt
{
protocolSourceConnectionNumber =
iv.conn_number,
protocolSourceConnectionNumber = iv.conn_number,
protocolSourceObjectAddress = iv.address,
origin = "supervised"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static void Main(string[] args)

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

// will read only data wanted
for (int i = 0; i < itemsBrowsed.Count; i++)
Expand Down

0 comments on commit 2ca7478

Please sign in to comment.