public static rtData newRealtimeDoc(OPC_Value iv, double _id) { if (iv.asdu.ToLower() == "boolean") { return new rtData() { _id = _id, protocolSourceASDU = iv.asdu, protocolSourceCommonAddress = iv.common_address, protocolSourceConnectionNumber = iv.conn_number, protocolSourceObjectAddress = iv.address, protocolSourceCommandUseSBO = false, protocolSourceCommandDuration = 0.0, protocolSourcePublishingInterval = 5.0, protocolSourceSamplingInterval = 2.0, protocolSourceQueueSize = 10.0, protocolSourceDiscardOldest = true, alarmState = 2.0, description = "OPC-UA~" + iv.conn_name + "~" + iv.display_name, ungroupedDescription = iv.display_name, group1 = iv.conn_name, group2 = iv.common_address, group3 = "", stateTextFalse = "FALSE", stateTextTrue = "TRUE", eventTextFalse = "FALSE", eventTextTrue = "TRUE", origin = "supervised", tag = TagFromOPCParameters(iv), type = "digital", value = iv.value, valueString = "????", alarmDisabled = false, alerted = false, alarmed = false, alertedState = "", annotation = "", commandBlocked = false, commandOfSupervised = 0.0, commissioningRemarks = "", formula = 0.0, frozen = false, frozenDetectTimeout = 0.0, hiLimit = Double.MaxValue, hihiLimit = Double.MaxValue, hihihiLimit = Double.MaxValue, historianDeadBand = 0.0, historianPeriod = 0.0, hysteresis = 0.0, invalid = true, invalidDetectTimeout = 60000, isEvent = false, kconv1 = 1.0, kconv2 = 0.0, location = BsonNull.Value, loLimit = -Double.MaxValue, loloLimit = -Double.MaxValue, lololoLimit = -Double.MaxValue, notes = "", overflow = false, parcels = BsonNull.Value, priority = 0.0, protocolDestinations = BsonNull.Value, sourceDataUpdate = BsonNull.Value, supervisedOfCommand = 0.0, timeTag = BsonNull.Value, timeTagAlarm = BsonNull.Value, timeTagAtSource = BsonNull.Value, timeTagAtSourceOk = false, transient = false, unit = "", updatesCnt = 0, valueDefault = 0.0, zeroDeadband = 0.0 } } ; else if (iv.asdu.ToLower() == "string" || iv.asdu.ToLower() == "extensionobject") { return new rtData() { _id = _id, protocolSourceASDU = iv.asdu, protocolSourceCommonAddress = iv.common_address, protocolSourceConnectionNumber = iv.conn_number, protocolSourceObjectAddress = iv.address, protocolSourceCommandUseSBO = false, protocolSourceCommandDuration = 0.0, protocolSourcePublishingInterval = 5.0, protocolSourceSamplingInterval = 2.0, protocolSourceQueueSize = 10.0, protocolSourceDiscardOldest = true, alarmState = -1.0, description = "OPC-UA~" + iv.conn_name + "~" + iv.display_name, ungroupedDescription = iv.display_name, group1 = iv.conn_name, group2 = iv.common_address, group3 = "", stateTextFalse = "", stateTextTrue = "", eventTextFalse = "", eventTextTrue = "", origin = "supervised", tag = TagFromOPCParameters(iv), type = "string", value = 0.0, valueString = iv.valueString, alarmDisabled = false, alerted = false, alarmed = false, alertedState = "", annotation = "", commandBlocked = false, commandOfSupervised = 0.0, commissioningRemarks = "", formula = 0.0, frozen = false, frozenDetectTimeout = 0.0, hiLimit = Double.MaxValue, hihiLimit = Double.MaxValue, hihihiLimit = Double.MaxValue, historianDeadBand = 0.0, historianPeriod = 0.0, hysteresis = 0.0, invalid = true, invalidDetectTimeout = 60000, isEvent = false, kconv1 = 1.0, kconv2 = 0.0, location = BsonNull.Value, loLimit = -Double.MaxValue, loloLimit = -Double.MaxValue, lololoLimit = -Double.MaxValue, notes = "", overflow = false, parcels = BsonNull.Value, priority = 0.0, protocolDestinations = BsonNull.Value, sourceDataUpdate = BsonNull.Value, supervisedOfCommand = 0.0, timeTag = BsonNull.Value, timeTagAlarm = BsonNull.Value, timeTagAtSource = BsonNull.Value, timeTagAtSourceOk = false, transient = false, unit = "", updatesCnt = 0, valueDefault = 0.0, zeroDeadband = 0.0, } } ; return(new rtData() { _id = _id, protocolSourceASDU = iv.asdu, protocolSourceCommonAddress = iv.common_address, protocolSourceConnectionNumber = iv.conn_number, protocolSourceObjectAddress = iv.address, protocolSourceCommandUseSBO = false, protocolSourceCommandDuration = 0.0, protocolSourcePublishingInterval = 5.0, protocolSourceSamplingInterval = 2.0, protocolSourceQueueSize = 10.0, protocolSourceDiscardOldest = true, alarmState = -1.0, description = "OPC-UA~" + iv.conn_name + "~" + iv.display_name, ungroupedDescription = iv.display_name, group1 = iv.conn_name, group2 = iv.common_address, group3 = "", stateTextFalse = "", stateTextTrue = "", eventTextFalse = "", eventTextTrue = "", origin = "supervised", tag = TagFromOPCParameters(iv), type = "analog", value = iv.value, valueString = "????", alarmDisabled = false, alerted = false, alarmed = false, alertedState = "", annotation = "", commandBlocked = false, commandOfSupervised = 0.0, commissioningRemarks = "", formula = 0.0, frozen = false, frozenDetectTimeout = 0.0, hiLimit = Double.MaxValue, hihiLimit = Double.MaxValue, hihihiLimit = Double.MaxValue, historianDeadBand = 0.0, historianPeriod = 0.0, hysteresis = 0.0, invalid = true, invalidDetectTimeout = 60000, isEvent = false, kconv1 = 1.0, kconv2 = 0.0, location = BsonNull.Value, loLimit = -Double.MaxValue, loloLimit = -Double.MaxValue, lololoLimit = -Double.MaxValue, notes = "", overflow = false, parcels = BsonNull.Value, priority = 0.0, protocolDestinations = BsonNull.Value, sourceDataUpdate = BsonNull.Value, supervisedOfCommand = 0.0, timeTag = BsonNull.Value, timeTagAlarm = BsonNull.Value, timeTagAtSource = BsonNull.Value, timeTagAtSourceOk = false, transient = false, unit = "", updatesCnt = 0, valueDefault = 0.0, zeroDeadband = 0.0 }); } }
static string TagFromOPCParameters(OPC_Value ov) { return(ov.conn_name + ";" + ov.address); }
private async void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e) { //MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification; //Console.WriteLine("Notification Received for Variable \"{0}\" and Value = {1} type {2}.", item.DisplayName, notification.Value, notification.TypeId); foreach (var value in item.DequeueValues()) { if (value != null) { string tp = "unknown"; try { if (value.WrappedValue.TypeInfo != null) { tp = value.WrappedValue.TypeInfo.BuiltInType.ToString(); // Log(conn_name + " - " + item.ResolvedNodeId + "TYPE: " + tp, LogLevelDetailed); } else { Log(conn_name + " - " + item.ResolvedNodeId + " TYPE: ?????", LogLevelDetailed); } Log(conn_name + " - " + item.ResolvedNodeId + " " + item.DisplayName + " " + value.Value + " " + value.SourceTimestamp + " " + value.StatusCode, LogLevelDetailed); if (value.Value != null) { Double dblValue = 0.0; string strValue = ""; try { if (tp == "Variant") { try { dblValue = System.Convert.ToDouble(value.Value); } catch { try { dblValue = System.Convert.ToInt64(value.Value); } catch { try { dblValue = System.Convert.ToInt32(value.Value); } catch { dblValue = 0; try { var opt = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString }; strValue = JsonSerializer.Serialize(value.Value, opt); } catch { strValue = value.Value.ToString(); } } } } } else if (tp == "DateTime") { dblValue = ((DateTimeOffset)System.Convert.ToDateTime(value.Value)).ToUnixTimeMilliseconds(); strValue = System.Convert.ToDateTime(value.Value).ToString("o"); } else { dblValue = System.Convert.ToDouble(value.Value); strValue = value.Value.ToString(); } } catch (Exception excpt) { strValue = value.Value.ToString(); } var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString }; OPC_Value iv = new OPC_Value() { valueJson = JsonSerializer.Serialize(value, options), selfPublish = true, address = item.ResolvedNodeId.ToString(), asdu = tp, isDigital = true, value = dblValue, valueString = strValue, hasSourceTimestamp = value.SourceTimestamp != DateTime.MinValue, sourceTimestamp = value.SourceTimestamp, serverTimestamp = DateTime.Now, quality = StatusCode.IsGood(value.StatusCode), cot = 3, conn_number = conn_number, conn_name = conn_name, common_address = "", display_name = item.DisplayName }; OPCDataQueue.Enqueue(iv); } } catch (Exception excpt) { Log(conn_name + " - " + excpt.Message); Log(conn_name + " - " + "TYPE:" + tp); Log(conn_name + " - " + item.ResolvedNodeId + " " + item.DisplayName + " " + value.Value + " " + value.SourceTimestamp + " " + value.StatusCode); } } else { Log(conn_name + " - " + item.ResolvedNodeId + " " + item.DisplayName + " NULL VALUE!", LogLevelDetailed); } Thread.Yield(); Thread.Sleep(1); //if ((OPCDataQueue.Count % 50) == 0) //{ // await Task.Delay(200); //} } }