diff --git a/src/OPC-DA-Client/AsduReceiveHandler.cs b/src/OPC-DA-Client/AsduReceiveHandler.cs
index b127965c..cbd76946 100644
--- a/src/OPC-DA-Client/AsduReceiveHandler.cs
+++ b/src/OPC-DA-Client/AsduReceiveHandler.cs
@@ -16,6 +16,7 @@
* along with this program. If not, see .
*/
+using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text.Json;
@@ -28,14 +29,14 @@ partial class MainClass
{
public static void OnDataChangeEvent(object subscriptionHandle, object requestHandle, TsCDaItemValueResult[] values, ref OPCDA_connection srv)
{
- string connName= srv.name;
+ string connName = srv.name;
Log(string.Format($"{connName} - DataChange: {values.Length} -----------------------------------------------------------"), 1);
//if (requestHandle != null)
//{
// Log("DataChange() for requestHandle :" + requestHandle.GetHashCode().ToString());
//}
for (var i = 0; i < values.Length; i++)
- {
+ {
if (values[i].Result.IsSuccess() && values[i].Value != null)
{
string strHandle = string.Format("{0}", values[i].ClientHandle);
@@ -61,7 +62,7 @@ public static void OnDataChangeEvent(object subscriptionHandle, object requestHa
valueString = valueString,
cot = 3,
serverTimestamp = DateTime.Now,
- sourceTimestamp = values[i].Timestamp,
+ sourceTimestamp = values[i].Timestamp.AddHours(srv.hoursShift),
hasSourceTimestamp = true,
isGood = isGood,
conn_number = srv.protocolConnectionNumber,
@@ -115,7 +116,21 @@ public static void convertItemValue(object iv, out double value, out string valu
{
if (iv.GetType().IsArray)
{
- valueJson = JsonSerializer.Serialize(iv);
+ if (iv.GetType().Name == "Byte[]")
+ {
+ var bytes = (byte[])iv;
+ valueJson = "[";
+ for (int i = 0; i < bytes.Length; i++)
+ {
+ valueJson += "" + bytes[i] + ",";
+ }
+ if (bytes.Length > 0) valueJson = valueJson.Substring(0, valueJson.Length - 1);
+ valueJson += "]";
+
+ // valueJson = JsonSerializer.Serialize(iv);
+ }
+ else
+ valueJson = JsonSerializer.Serialize(iv);
valueString = valueJson;
}
else
@@ -238,5 +253,83 @@ public static void BrowseServer(ref TsCDaServer daServer, OpcItem item, ref List
} while (position != null);
}
}
+
+ public static void processValueResults(ref OPCDA_connection srv, ref TsCDaItemValueResult[] itemValues, ref IMongoCollection collRtData, bool firstExecution)
+ {
+ for (var i = 0; i < itemValues.Length; i++)
+ {
+ if (itemValues[i].Result.IsError())
+ {
+ Log($"{srv.name} - Item {itemValues[i].ItemPath} {itemValues[i].ItemName} could not be read");
+ }
+ else
+ {
+ double value = 0;
+ string valueJson = string.Empty;
+ string valueString = string.Empty;
+ bool isGood = true;
+ bool isDigital = false;
+ convertItemValue(itemValues[i].Value, out value, out valueString, out valueJson, out isGood, out isDigital);
+ 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,
+ selfPublish = true,
+ address = itemValues[i].ItemName,
+ asdu = itemValues[i].Value.GetType().Name,
+ isDigital = isDigital,
+ isArray = itemValues[i].Value.GetType().IsArray,
+ value = value,
+ valueString = valueString,
+ cot = 20,
+ serverTimestamp = DateTime.Now,
+ sourceTimestamp = DateTime.MinValue,
+ hasSourceTimestamp = false,
+ isGood = isGood,
+ conn_number = srv.protocolConnectionNumber,
+ conn_name = srv.name,
+ common_address = common_address,
+ display_name = itemValues[i].ItemName,
+ group1 = srv.name,
+ group2 = group2,
+ group3 = group3,
+ ungroupedDescription = ungroupedDescription,
+ };
+
+ if (firstExecution && srv.autoCreateTags && !srv.InsertedAddresses.Contains(itemValues[i].ItemName))
+ {
+ var id = srv.LastNewKeyCreated + 1;
+ srv.LastNewKeyCreated = id;
+
+ // will enqueue to insert the new tag into mongo DB
+ var rtDtIns = newRealtimeDoc(ov, id);
+ AutoCreateTag(in rtDtIns, in collRtData, in srv);
+ }
+ OPCDataQueue.Enqueue(ov);
+ }
+ }
+
+ }
}
}
\ No newline at end of file
diff --git a/src/OPC-DA-Client/Common_srv_cli.cs b/src/OPC-DA-Client/Common_srv_cli.cs
index ac57bce8..2acb70c6 100644
--- a/src/OPC-DA-Client/Common_srv_cli.cs
+++ b/src/OPC-DA-Client/Common_srv_cli.cs
@@ -73,46 +73,28 @@ public class
OPCDA_connection // protocol connection to RTU
{
public ObjectId Id { get; set; }
- [BsonDefaultValue("")]
- public string protocolDriver { get; set; }
- [BsonDefaultValue(1)]
- public int protocolDriverInstanceNumber { get; set; }
- [BsonDefaultValue(1)]
- public int protocolConnectionNumber { get; set; }
- [BsonDefaultValue("NO NAME")]
- public string name { get; set; }
- [BsonDefaultValue("SERVER NOT DESCRIPTED")]
- public string description { get; set; }
- [BsonDefaultValue(true)]
- public bool enabled { get; set; }
- [BsonDefaultValue(true)]
- public bool commandsEnabled { get; set; }
+ public string protocolDriver { get; set; } = string.Empty;
+ public int protocolDriverInstanceNumber { get; set; } = 1;
+ public int protocolConnectionNumber { get; set; } = 1;
+ public string name { get; set; } = "NO NAME";
+ public string description { get; set; } = "CONNECTION NOT DESCRIPTED";
+ public bool enabled { get; set; } = true;
+ public bool commandsEnabled { get; set; } = true;
public string[] endpointURLs { get; set; }
public string[] topics { get; set; }
- [BsonDefaultValue(true)]
- public bool autoCreateTags { get; set; }
- [BsonDefaultValue(2.5)]
- public double autoCreateTagPublishingInterval { get; set; }
- [BsonDefaultValue(0.0)]
- public double autoCreateTagSamplingInterval { get; set; }
- [BsonDefaultValue(5.0)]
- public double autoCreateTagQueueSize { get; set; }
- [BsonDefaultValue(20000.0)]
- public double timeoutMs { get; set; }
- [BsonDefaultValue("")]
- public string username { get; set; }
- [BsonDefaultValue("")]
- public string password { get; set; }
- [BsonDefaultValue(false)]
- public bool useSecurity { get; set; }
- //[BsonDefaultValue(false)]
- //public bool chainValidation { get; set; }
- //[BsonDefaultValue("")]
- //public string rootCertFilePath { get; set; }
- [BsonDefaultValue("")]
- public string localCertFilePath { get; set; }
- [BsonDefaultValue("")]
- public string peerCertFilePath { get; set; }
+ public bool autoCreateTags { get; set; } = true;
+ public int giInterval { get; set; } = 0;
+ public double deadBand { get; set; } = 0;
+ public double hoursShift { get; set; } = 0;
+ public double autoCreateTagPublishingInterval { get; set; } = 2.5;
+ public double autoCreateTagSamplingInterval { get; set; } = 0.0;
+ public double autoCreateTagQueueSize { get; set; } = 5.0;
+ public double timeoutMs { get; set; } = 20000.0;
+ public string username { get; set; } = string.Empty;
+ public string password { get; set; } = string.Empty;
+ public bool useSecurity { get; set; } = false;
+ public string localCertFilePath { get; set; } = string.Empty;
+ public string peerCertFilePath { get; set; } = string.Empty;
//[BsonDefaultValue("")]
//public string privateKeyFilePath { get; set; }
//[BsonDefaultValue("")]
@@ -130,7 +112,7 @@ public class
//[BsonDefaultValue("")]
//public string cipherList { get; set; }
- public Double LastNewKeyCreated = -1;
+ public double LastNewKeyCreated = -1;
public SortedSet InsertedTags = new SortedSet();
public SortedSet InsertedAddresses = new SortedSet();
// public static Dictionary MapNameToHandler = new Dictionary();
@@ -138,7 +120,8 @@ public class
public Dictionary MapHandlerToConnName = new Dictionary();
// public Dictionary MapItemNameToBranch = new Dictionary();
public SortedSet branches = new SortedSet(); // branches to scan
- public TsCDaServer connection = null;
+ public TsCDaServer connection = null; // opc da connection to a server
+ public List subscriptions = new List(); // data subscriptions on a server
public Thread thrOPCStack = null;
public int cntConnectRetries = 0;
}
@@ -190,24 +173,24 @@ public class rtFilt
public class rtCommand
{
public BsonObjectId id { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceConnectionNumber { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceCommonAddress { get; set; }
[BsonDefaultValue("")]
public BsonString protocolSourceObjectAddress { get; set; }
[BsonDefaultValue("")]
public BsonString protocolSourceASDU { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceCommandDuration { get; set; }
[BsonDefaultValue(false)]
public BsonBoolean protocolSourceCommandUseSBO { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble pointKey { get; set; }
[BsonDefaultValue("")]
public BsonString tag { get; set; }
public BsonDateTime timeTag { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble value { get; set; }
[BsonDefaultValue("")]
public BsonString valueString { get; set; }
@@ -221,24 +204,24 @@ public class rtCommand
public class rtCommandNoAck
{
public BsonObjectId id { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceConnectionNumber { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceCommonAddress { get; set; }
[BsonDefaultValue("")]
public BsonString protocolSourceObjectAddress { get; set; }
[BsonDefaultValue("")]
public BsonString protocolSourceASDU { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolSourceCommandDuration { get; set; }
[BsonDefaultValue(false)]
public BsonBoolean protocolSourceCommandUseSBO { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble pointKey { get; set; }
[BsonDefaultValue("")]
public BsonString tag { get; set; }
public BsonDateTime timeTag { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble value { get; set; }
[BsonDefaultValue("")]
public BsonString valueString { get; set; }
@@ -251,25 +234,25 @@ public class rtCommandNoAck
[BsonIgnoreExtraElements]
public class rtDataProtocDest
{
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationConnectionNumber { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationCommonAddress { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationObjectAddress { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationASDU { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationCommandDuration { get; set; }
[BsonDefaultValue(false)]
public BsonBoolean protocolDestinationCommandUseSBO { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationGroup { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(1)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(1.0)]
public BsonDouble protocolDestinationKConv1 { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationKConv2 { get; set; }
- [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0)]
+ [BsonSerializer(typeof(BsonDoubleSerializer)), BsonDefaultValue(0.0)]
public BsonDouble protocolDestinationHoursShift { get; set; }
}
@@ -305,13 +288,13 @@ static void Log(string str, int level = 1)
}
}
- static void Log(System.Exception e, int level = 1)
+ static void Log(Exception e, int level = 1)
{
Log(e.ToString(), level);
}
public class BsonDoubleSerializer : SerializerBase // generic permissive numeric deserializer resulting double
{ // read most types as double, write to double
- public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, BsonDouble dval)
+ public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BsonDouble dval)
{
context.Writer.WriteDouble(dval.ToDouble());
}
@@ -358,10 +341,10 @@ public override BsonDouble Deserialize(BsonDeserializationContext context, BsonD
}
break;
case BsonType.Decimal128:
- dval = System.Convert.ToDouble(context.Reader.ReadDecimal128());
+ dval = Convert.ToDouble(context.Reader.ReadDecimal128());
break;
case BsonType.Boolean:
- dval = System.Convert.ToDouble(context.Reader.ReadBoolean());
+ dval = Convert.ToDouble(context.Reader.ReadBoolean());
break;
case BsonType.Int32:
dval = context.Reader.ReadInt32();
@@ -376,7 +359,7 @@ public override BsonDouble Deserialize(BsonDeserializationContext context, BsonD
public class BsonIntSerializer : SerializerBase // generic permissive numeric deserializer resulting int
{ // read most types as int but write to double
- public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, BsonInt32 ival)
+ public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BsonInt32 ival)
{
context.Writer.WriteDouble(ival.ToDouble());
}
@@ -429,13 +412,13 @@ public override BsonInt32 Deserialize(BsonDeserializationContext context, BsonDe
}
break;
case BsonType.Decimal128:
- dval = System.Convert.ToDouble(context.Reader.ReadDecimal128());
+ dval = Convert.ToDouble(context.Reader.ReadDecimal128());
break;
case BsonType.Boolean:
- dval = System.Convert.ToDouble(context.Reader.ReadBoolean());
+ dval = Convert.ToDouble(context.Reader.ReadBoolean());
break;
}
- return System.Convert.ToInt32(dval);
+ return Convert.ToInt32(dval);
}
}
static byte[] GetBytesFromPEM(string pemString, string section)
@@ -457,26 +440,26 @@ static byte[] GetBytesFromPEM(string pemString, string section)
}
private static bool CertificateValidationCallBack(
object sender,
- System.Security.Cryptography.X509Certificates.X509Certificate certificate,
- System.Security.Cryptography.X509Certificates.X509Chain chain,
- System.Net.Security.SslPolicyErrors sslPolicyErrors)
+ X509Certificate certificate,
+ X509Chain chain,
+ SslPolicyErrors sslPolicyErrors)
{
// If the certificate is a valid, signed certificate, return true.
- if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
+ if (sslPolicyErrors == SslPolicyErrors.None)
{
Console.WriteLine("It's ok");
return true;
}
// If there are errors in the certificate chain, look at each error to determine the cause.
- if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
+ if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
- foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
+ foreach (X509ChainStatus status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) &&
- (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
+ (status.Status == X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates with an untrusted root are valid.
Console.WriteLine("Untrusted root certificate");
@@ -496,7 +479,7 @@ private static bool CertificateValidationCallBack(
return true;
}
- if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
+ if (status.Status != X509ChainStatusFlags.NoError)
{
Console.WriteLine(status.StatusInformation);
diff --git a/src/OPC-DA-Client/MongoUpdate.cs b/src/OPC-DA-Client/MongoUpdate.cs
index ba86be02..37a0a524 100644
--- a/src/OPC-DA-Client/MongoUpdate.cs
+++ b/src/OPC-DA-Client/MongoUpdate.cs
@@ -103,67 +103,6 @@ static public async void ProcessMongo(JSONSCADAConfig jsConfig)
}
}
- /*
- if (iv.selfPublish)
- {
- // find the json-scada connection for this received value
- int conn_index = 0;
- for (int index = 0; index < OPCDAconns.Count; index++)
- {
- if (OPCDAconns[index].protocolConnectionNumber == iv.conn_number)
- conn_index = index;
- }
-
- string tag = TagFromOPCParameters(iv);
- if (!OPCDAconns[conn_index].InsertedTags.Contains(tag))
- { // tag not yet inserted
- // put the tag in the list of inserted, then insert it
-
- OPCDAconns[conn_index].InsertedTags.Add(tag);
-
- Log(iv.conn_name + " - INSERT NEW TAG: " + tag + " - Addr:" + iv.address);
-
- // find a new freee _id key based on the connection number
- if (OPCDAconns[conn_index].LastNewKeyCreated == 0)
- {
- Double AutoKeyId = iv.conn_number * AutoKeyMultiplier;
- var results = collectionId.Find(new BsonDocument {
- { "_id", new BsonDocument{
- { "$gt", AutoKeyId },
- { "$lt", ( iv.conn_number + 1) * AutoKeyMultiplier }
- }
- }
- }).Sort(Builders.Sort.Descending("_id"))
- .Limit(1)
- .ToList();
-
- if (results.Count > 0)
- {
- OPCDAconns[conn_index].LastNewKeyCreated = results[0]._id.ToDouble() + 1;
- }
- else
- {
- OPCDAconns[conn_index].LastNewKeyCreated = AutoKeyId;
- }
- }
- else
- OPCDAconns[conn_index].LastNewKeyCreated = OPCDAconns[conn_index].LastNewKeyCreated + 1;
-
- var id = OPCDAconns[conn_index].LastNewKeyCreated;
-
- // will enqueue to insert the new tag into mongo DB
- var insert = newRealtimeDoc(iv, id);
- insert.protocolSourcePublishingInterval = OPCDAconns[conn_index].autoCreateTagPublishingInterval;
- insert.protocolSourceSamplingInterval = OPCDAconns[conn_index].autoCreateTagSamplingInterval;
- insert.protocolSourceQueueSize = OPCDAconns[conn_index].autoCreateTagQueueSize;
- listWrites
- .Add(new InsertOneModel(insert));
-
- // will imediatelly be followed by an update below (to the same tag)
- }
- }
- */
-
// update one existing document with received tag value (realtimeData)
var update =
new BsonDocument {
diff --git a/src/OPC-DA-Client/Program.cs b/src/OPC-DA-Client/Program.cs
index 1ede88e9..b45638ab 100644
--- a/src/OPC-DA-Client/Program.cs
+++ b/src/OPC-DA-Client/Program.cs
@@ -22,15 +22,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
-using System.Net.Http;
-using System.Reflection.Metadata.Ecma335;
using System.Text.Json;
using System.Threading;
-using System.Threading.Tasks;
using Technosoftware.DaAeHdaClient;
using Technosoftware.DaAeHdaClient.Da;
-using static MongoDB.Driver.WriteConcern;
-using static System.Runtime.InteropServices.JavaScript.JSType;
namespace OPCDAClientDriver
{
@@ -397,84 +392,18 @@ public static void Main(string[] args)
// Synchronous Read with server read function (DA 3.0) without a group
Log($"{srv.name} - Reading {itemsForGroup.Count} items...");
var itemValues = daServer.Read(itemsForGroup.ToArray());
- for (var i = 0; i < itemValues.Length; i++)
- {
- if (itemValues[i].Result.IsError())
- {
- Log($"{srv.name} - Item {itemValues[i].ItemPath} {itemValues[i].ItemName} could not be read");
- }
- else
- {
- double value = 0;
- string valueJson = string.Empty;
- string valueString = string.Empty;
- bool isGood = true;
- bool isDigital = false;
- convertItemValue(itemValues[i].Value, out value, out valueString, out valueJson, out isGood, out isDigital);
- 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,
- selfPublish = true,
- address = itemValues[i].ItemName,
- asdu = itemValues[i].Value.GetType().Name,
- isDigital = isDigital,
- isArray = itemValues[i].Value.GetType().IsArray,
- value = value,
- valueString = valueString,
- cot = 20,
- serverTimestamp = DateTime.Now,
- sourceTimestamp = DateTime.MinValue,
- hasSourceTimestamp = false,
- isGood = isGood,
- conn_number = srv.protocolConnectionNumber,
- conn_name = srv.name,
- common_address = common_address,
- display_name = itemValues[i].ItemName,
- group1 = srv.name,
- group2 = group2,
- group3 = group3,
- ungroupedDescription = ungroupedDescription,
- };
-
- if (srv.autoCreateTags && !srv.InsertedAddresses.Contains(itemValues[i].ItemName))
- {
- var id = srv.LastNewKeyCreated + 1;
- srv.LastNewKeyCreated = id;
-
- // will enqueue to insert the new tag into mongo DB
- var rtDtIns = newRealtimeDoc(ov, id);
- AutoCreateTag(in rtDtIns, in collRtData, in srv);
- }
- OPCDataQueue.Enqueue(ov);
- }
- }
+ processValueResults(ref srv, ref itemValues, ref collRtData, true);
// Add a group with default values Active = true and UpdateRate = 500ms
- var subscrState = new TsCDaSubscriptionState { Name = "MyGroup", UpdateRate = 1000 };
+ var subscrState = new TsCDaSubscriptionState
+ {
+ Name = "JsonScadaGroup1",
+ UpdateRate = (int)srv.autoCreateTagSamplingInterval,
+ Deadband = (float)srv.deadBand,
+ // TimeBias = (int)(srv.hoursShift * 60)
+ // KeepAlive = (int)srv.giInterval/2,
+ };
var subscr = (TsCDaSubscription)daServer.CreateSubscription(subscrState);
-
var itemResults = subscr.AddItems(itemsForGroup.ToArray());
for (var i = 0; i < itemResults.Length; i++)
@@ -489,6 +418,7 @@ public static void Main(string[] args)
{
OnDataChangeEvent(subscriptionHandle, requestHandle, values, ref srv);
};
+ srv.subscriptions.Add(subscr);
}
catch (OpcResultException e)
{
diff --git a/src/OPC-DA-Client/TagsCreation.cs b/src/OPC-DA-Client/TagsCreation.cs
index 53b083aa..6a4077c3 100644
--- a/src/OPC-DA-Client/TagsCreation.cs
+++ b/src/OPC-DA-Client/TagsCreation.cs
@@ -17,7 +17,6 @@
*/
using System;
-using System.Text.RegularExpressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;