/// <summary cref="IEncodeable.IsEqual(IEncodeable)" /> public virtual bool IsEqual(IEncodeable encodeable) { if (Object.ReferenceEquals(this, encodeable)) { return(true); } WeatherData value = encodeable as WeatherData; if (value == null) { return(false); } if (!Utils.IsEqual(m_temperature, value.m_temperature)) { return(false); } if (!Utils.IsEqual(m_maxTemperature, value.m_maxTemperature)) { return(false); } if (!Utils.IsEqual(m_minTemperature, value.m_minTemperature)) { return(false); } if (!Utils.IsEqual(m_pressure, value.m_pressure)) { return(false); } if (!Utils.IsEqual(m_cityName, value.m_cityName)) { return(false); } return(true); }
/// <summary cref="IEncodeable.IsEqual(IEncodeable)" /> public virtual bool IsEqual(IEncodeable encodeable) { if (Object.ReferenceEquals(this, encodeable)) { return(true); } AxisInformation value = encodeable as AxisInformation; if (value == null) { return(false); } if (!Utils.IsEqual(m_engineeringUnits, value.m_engineeringUnits)) { return(false); } if (!Utils.IsEqual(m_eURange, value.m_eURange)) { return(false); } if (!Utils.IsEqual(m_title, value.m_title)) { return(false); } if (!Utils.IsEqual(m_axisScaleType, value.m_axisScaleType)) { return(false); } if (!Utils.IsEqual(m_axisSteps, value.m_axisSteps)) { return(false); } return(true); }
/// <summary cref="IEncodeable.IsEqual(IEncodeable)" /> public virtual bool IsEqual(IEncodeable encodeable) { if (Object.ReferenceEquals(this, encodeable)) { return(true); } RegisteredNode value = encodeable as RegisteredNode; if (value == null) { return(false); } if (!Utils.IsEqual(m_nodeStatus, value.m_nodeStatus)) { return(false); } if (!Utils.IsEqual(m_onlineContextNodeId, value.m_onlineContextNodeId)) { return(false); } if (!Utils.IsEqual(m_onlineDeviceNodeId, value.m_onlineDeviceNodeId)) { return(false); } if (!Utils.IsEqual(m_offlineContextNodeId, value.m_offlineContextNodeId)) { return(false); } if (!Utils.IsEqual(m_offlineDeviceNodeId, value.m_offlineDeviceNodeId)) { return(false); } return(true); }
/// <summary> /// Return true if system Type is IEncodeable. /// </summary> protected static bool IsEncodeableType(System.Type systemType) { if (systemType == null) { return(false); } var systemTypeInfo = systemType.GetTypeInfo(); if (systemTypeInfo.IsAbstract || !typeof(IEncodeable).GetTypeInfo().IsAssignableFrom(systemTypeInfo)) { return(false); } IEncodeable encodeable = Activator.CreateInstance(systemType) as IEncodeable; if (encodeable == null) { return(false); } return(true); }
/// <summary> /// Secures the message using the security token. /// </summary> protected BufferCollection WriteSymmetricMessage( uint messageType, uint requestId, ChannelToken token, object messageBody, bool isRequest, out bool limitsExceeded) { limitsExceeded = false; bool success = false; BufferCollection chunksToProcess = null; try { // calculate chunk sizes. int maxCipherTextSize = SendBufferSize - TcpMessageLimits.SymmetricHeaderSize; int maxCipherBlocks = maxCipherTextSize / EncryptionBlockSize; int maxPlainTextSize = maxCipherBlocks * EncryptionBlockSize; int maxPayloadSize = maxPlainTextSize - SymmetricSignatureSize - 1 - TcpMessageLimits.SequenceHeaderSize; int headerSize = TcpMessageLimits.SymmetricHeaderSize + TcpMessageLimits.SequenceHeaderSize; // write the body to stream. ArraySegmentStream ostrm = new ArraySegmentStream( BufferManager, SendBufferSize, headerSize, maxPayloadSize); // check for encodeable body. IEncodeable encodeable = messageBody as IEncodeable; if (encodeable != null) { // debug code used to verify that message aborts are handled correctly. // int maxMessageSize = Quotas.MessageContext.MaxMessageSize; // Quotas.MessageContext.MaxMessageSize = Int32.MaxValue; BinaryEncoder.EncodeMessage(encodeable, ostrm, Quotas.MessageContext); // Quotas.MessageContext.MaxMessageSize = maxMessageSize; } // check for raw bytes. ArraySegment <byte>?rawBytes = messageBody as ArraySegment <byte>?; if (rawBytes != null) { BinaryEncoder encoder = new BinaryEncoder(ostrm, Quotas.MessageContext); encoder.WriteRawBytes(rawBytes.Value.Array, rawBytes.Value.Offset, rawBytes.Value.Count); encoder.Close(); } chunksToProcess = ostrm.GetBuffers("WriteSymmetricMessage"); // ensure there is at least one chunk. if (chunksToProcess.Count == 0) { byte[] buffer = BufferManager.TakeBuffer(SendBufferSize, "WriteSymmetricMessage"); chunksToProcess.Add(new ArraySegment <byte>(buffer, 0, 0)); } BufferCollection chunksToSend = new BufferCollection(chunksToProcess.Capacity); int messageSize = 0; for (int ii = 0; ii < chunksToProcess.Count; ii++) { ArraySegment <byte> chunkToProcess = chunksToProcess[ii]; // nothing more to do if limits exceeded. if (limitsExceeded) { BufferManager.ReturnBuffer(chunkToProcess.Array, "WriteSymmetricMessage"); continue; } MemoryStream strm = new MemoryStream(chunkToProcess.Array, 0, SendBufferSize); BinaryEncoder encoder = new BinaryEncoder(strm, Quotas.MessageContext); // check if the message needs to be aborted. if (MessageLimitsExceeded(isRequest, messageSize + chunkToProcess.Count - headerSize, ii + 1)) { encoder.WriteUInt32(null, messageType | TcpMessageType.Abort); // replace the body in the chunk with an error message. BinaryEncoder errorEncoder = new BinaryEncoder( chunkToProcess.Array, chunkToProcess.Offset, chunkToProcess.Count, Quotas.MessageContext); WriteErrorMessageBody(errorEncoder, (isRequest) ? StatusCodes.BadRequestTooLarge : StatusCodes.BadResponseTooLarge); int size = errorEncoder.Close(); chunkToProcess = new ArraySegment <byte>(chunkToProcess.Array, chunkToProcess.Offset, size); limitsExceeded = true; } // check if the message is complete. else if (ii == chunksToProcess.Count - 1) { encoder.WriteUInt32(null, messageType | TcpMessageType.Final); } // more chunks to follow. else { encoder.WriteUInt32(null, messageType | TcpMessageType.Intermediate); } int count = 0; count += TcpMessageLimits.SequenceHeaderSize; count += chunkToProcess.Count; count += SymmetricSignatureSize; // calculate the padding. int padding = 0; if (SecurityMode == MessageSecurityMode.SignAndEncrypt) { // reserve one byte for the padding size. count++; if (count % EncryptionBlockSize != 0) { padding = EncryptionBlockSize - (count % EncryptionBlockSize); } count += padding; } count += TcpMessageLimits.SymmetricHeaderSize; encoder.WriteUInt32(null, (uint)count); encoder.WriteUInt32(null, ChannelId); encoder.WriteUInt32(null, token.TokenId); uint sequenceNumber = GetNewSequenceNumber(); encoder.WriteUInt32(null, sequenceNumber); encoder.WriteUInt32(null, requestId); // skip body. strm.Seek(chunkToProcess.Count, SeekOrigin.Current); // update message size count. messageSize += chunkToProcess.Count; // write padding. if (SecurityMode == MessageSecurityMode.SignAndEncrypt) { for (int jj = 0; jj <= padding; jj++) { encoder.WriteByte(null, (byte)padding); } } if (SecurityMode != MessageSecurityMode.None) { // calculate and write signature. byte[] signature = Sign(token, new ArraySegment <byte>(chunkToProcess.Array, 0, encoder.Position), isRequest); if (signature != null) { encoder.WriteRawBytes(signature, 0, signature.Length); } } if (SecurityMode == MessageSecurityMode.SignAndEncrypt) { // encrypt the data. ArraySegment <byte> dataToEncrypt = new ArraySegment <byte>(chunkToProcess.Array, TcpMessageLimits.SymmetricHeaderSize, encoder.Position - TcpMessageLimits.SymmetricHeaderSize); Encrypt(token, dataToEncrypt, isRequest); } // add the header into chunk. chunksToSend.Add(new ArraySegment <byte>(chunkToProcess.Array, 0, encoder.Position)); } // ensure the buffers don't get cleaned up on exit. success = true; return(chunksToSend); } finally { if (!success) { if (chunksToProcess != null) { chunksToProcess.Release(BufferManager, "WriteSymmetricMessage"); } } } }
/// <summary> /// Applies the data encoding to the value. /// </summary> public static ServiceResult ApplyDataEncoding(ServiceMessageContext context, QualifiedName dataEncoding, ref object value) { // check if nothing to do. if (QualifiedName.IsNull(dataEncoding) || value == null) { return(ServiceResult.Good); } // check for supported encoding type. if (dataEncoding.NamespaceIndex != 0) { return(StatusCodes.BadDataEncodingUnsupported); } bool useXml = dataEncoding.Name == DefaultXml; if (!useXml && dataEncoding.Name != DefaultBinary) { return(StatusCodes.BadDataEncodingUnsupported); } try { // check for array of encodeables. IList <IEncodeable> encodeables = value as IList <IEncodeable>; if (encodeables == null) { // check for array of extension objects. IList <ExtensionObject> extensions = value as IList <ExtensionObject>; if (extensions != null) { // convert extension objects to encodeables. encodeables = new IEncodeable[extensions.Count]; for (int ii = 0; ii < encodeables.Count; ii++) { if (ExtensionObject.IsNull(extensions[ii])) { encodeables[ii] = null; continue; } IEncodeable element = extensions[ii].Body as IEncodeable; if (element == null) { return(StatusCodes.BadTypeMismatch); } encodeables[ii] = element; } } } // apply data encoding to the array. if (encodeables != null) { ExtensionObject[] extensions = new ExtensionObject[encodeables.Count]; for (int ii = 0; ii < extensions.Length; ii++) { extensions[ii] = Encode(context, encodeables[ii], useXml); } value = extensions; return(ServiceResult.Good); } // check for scalar value. IEncodeable encodeable = value as IEncodeable; if (encodeable == null) { ExtensionObject extension = value as ExtensionObject; if (extension == null) { return(StatusCodes.BadDataEncodingUnsupported); } encodeable = extension.Body as IEncodeable; } if (encodeable == null) { return(StatusCodes.BadDataEncodingUnsupported); } // do conversion. value = Encode(context, encodeable, useXml); return(ServiceResult.Good); } catch (Exception e) { return(ServiceResult.Create(e, StatusCodes.BadTypeMismatch, "Could not convert value to requested format.")); } }
/// <summary> /// Shows a value in control. /// </summary> private async Task ShowValue(int index, bool overwrite, object value) { if (value == null) { return; } // show monitored items. MonitoredItem monitoredItem = value as MonitoredItem; if (monitoredItem != null) { m_monitoredItem = monitoredItem; ShowValue(ref index, ref overwrite, monitoredItem.LastValue.ToString()); return; } // show data changes MonitoredItemNotification datachange = value as MonitoredItemNotification; if (datachange != null) { ShowValue(ref index, ref overwrite, datachange.Value.ToString()); return; } // show events EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { for (int ii = 0; ii < eventFields.EventFields.Count; ii++) { ShowValue(ref index, ref overwrite, eventFields, ii); } return; } // show extension bodies. ExtensionObject extension = value as ExtensionObject; if (extension != null) { ShowValue(ref index, ref overwrite, extension.Body.ToString()); return; } // show encodeables. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { PropertyInfo[] properties = encodeable.GetType().GetProperties(); foreach (PropertyInfo property in properties) { ShowValue(ref index, ref overwrite, encodeable, property); } return; } // show bytes. byte[] bytes = value as byte[]; if (bytes != null) { bool result = await PromptOnLongList(bytes.Length / 16); if (!result) { return; } for (int ii = 0; ii < bytes.Length; ii += 16) { ShowValue(ref index, ref overwrite, bytes, ii); } return; } // show arrays Array array = value as Array; if (array != null) { bool result = await PromptOnLongList(array.Length); if (!result) { return; } for (int ii = 0; ii < array.Length; ii++) { ShowValue(ref index, ref overwrite, array, ii); } return; } // show lists IList list = value as IList; if (list != null) { bool result = await PromptOnLongList(list.Count); if (!result) { return; } for (int ii = 0; ii < list.Count; ii++) { ShowValue(ref index, ref overwrite, list, ii); } return; } // show xml elements XmlElement xml = value as XmlElement; if (xml != null) { bool result = await PromptOnLongList(xml.ChildNodes.Count); if (!result) { return; } for (int ii = 0; ii < xml.ChildNodes.Count; ii++) { ShowValue(ref index, ref overwrite, xml, ii); } return; } // show data value. DataValue datavalue = value as DataValue; if (datavalue != null) { ShowValue(ref index, ref overwrite, datavalue, 0); ShowValue(ref index, ref overwrite, datavalue, 1); ShowValue(ref index, ref overwrite, datavalue, 2); ShowValue(ref index, ref overwrite, datavalue, 3); return; } // show node id value. NodeId nodeId = value as NodeId; if (nodeId != null) { ShowValue(ref index, ref overwrite, nodeId, 0); ShowValue(ref index, ref overwrite, nodeId, 1); ShowValue(ref index, ref overwrite, nodeId, 2); return; } // show expanded node id value. ExpandedNodeId expandedNodeId = value as ExpandedNodeId; if (expandedNodeId != null) { ShowValue(ref index, ref overwrite, expandedNodeId, 0); ShowValue(ref index, ref overwrite, expandedNodeId, 1); ShowValue(ref index, ref overwrite, expandedNodeId, 2); ShowValue(ref index, ref overwrite, expandedNodeId, 3); return; } // show qualified name value. QualifiedName qualifiedName = value as QualifiedName; if (qualifiedName != null) { ShowValue(ref index, ref overwrite, qualifiedName, 0); ShowValue(ref index, ref overwrite, qualifiedName, 1); return; } // show qualified name value. LocalizedText localizedText = value as LocalizedText; if (localizedText != null) { ShowValue(ref index, ref overwrite, localizedText, 0); ShowValue(ref index, ref overwrite, localizedText, 1); return; } // show variant. Variant?variant = value as Variant?; if (variant != null) { ShowValue(ref index, ref overwrite, variant.Value.Value.ToString()); return; } // show unknown types as strings. ShowValue(ref index, ref overwrite, String.Format("{0}", value)); }
/// <summary> /// Formats a value for display in the control. /// </summary> private string GetValueText(object value) { // check for null. if (value == null) { return("(null)"); } // format bytes. byte[] bytes = value as byte[]; if (bytes != null) { StringBuilder buffer = new StringBuilder(); for (int ii = 0; ii < bytes.Length; ii++) { if (ii != 0 && ii % 16 == 0) { buffer.Append(" "); } buffer.AppendFormat("{0:X2} ", bytes[ii]); } return(buffer.ToString()); } // format xml element. XmlElement xml = value as XmlElement; if (xml != null) { // return the entire element if not expandable. if (!IsExpandableType(xml)) { return(xml.OuterXml); } // show only the start tag. string text = xml.OuterXml; int index = text.IndexOf('>'); if (index != -1) { text = text.Substring(0, index); } return(text); } // format array. Array array = value as Array; if (array != null) { return(Utils.Format("{1}[{0}]", array.Length, value.GetType().GetElementType().Name)); } // format list. IList list = value as IList; if (list != null) { string type = value.GetType().Name; if (type.EndsWith("Collection")) { type = type.Substring(0, type.Length - "Collection".Length); } else { type = "Object"; } return(Utils.Format("{1}[{0}]", list.Count, type)); } // format encodeable object. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { return(encodeable.GetType().Name); } // format extension object. ExtensionObject extension = value as ExtensionObject; if (extension != null) { return(GetValueText(extension.Body)); } // check for event value. EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { if (m_monitoredItem != null) { return(String.Format("{0}", m_monitoredItem.GetEventType(eventFields))); } return(eventFields.GetType().Name); } // check for data value. DataValue dataValue = value as DataValue; if (dataValue != null) { if (StatusCode.IsBad(dataValue.StatusCode)) { return(String.Format("{0}", dataValue.StatusCode)); } return(String.Format("{0}", dataValue.Value)); } // use default formatting. return(Utils.Format("{0}", value)); }
/// <summary> /// The service result for a field in an notification (the field must contain a Status object). /// </summary> public static ServiceResult GetServiceResult(IEncodeable notification, int index) { EventFieldList eventFields = notification as EventFieldList; if (eventFields == null) { return null; } NotificationMessage message = eventFields.Message; if (message != null) { return null; } if (index < 0 || index >= eventFields.EventFields.Count) { return null; } StatusResult status = ExtensionObject.ToEncodeable(eventFields.EventFields[index].Value as ExtensionObject) as StatusResult; if (status == null) { return null; } return new ServiceResult(status.StatusCode, status.DiagnosticInfo, message.StringTable); }
public EncodableWrapper(IDecoder decoder, IEncodeable wrapped) { _decoder = decoder; _wrapped = wrapped; }
/// <inheritdoc /> public bool IsEqual(IEncodeable encodeable) => _wrapped.IsEqual(encodeable);
/// <summary> /// Encodes the object in XML. /// </summary> public static XmlElement EncodeXml(IEncodeable encodeable, ServiceMessageContext context) { // create encoder. XmlEncoder encoder = new XmlEncoder(context); // write body. encoder.WriteExtensionObjectBody(encodeable); // create document from encoder. XmlDocument document = new XmlDocument(); document.InnerXml = encoder.Close(); // return root element. return document.DocumentElement; }
public EncodableWrapper(IEncoder encoder, IEncodeable wrapped) { _encoder = encoder; _wrapped = wrapped; }
/// <inheritdoc /> public void WriteEncodeable(string fieldName, IEncodeable value, Type systemType) => _wrapped.WriteEncodeable( fieldName, new EncodableWrapper(this, value), systemType);
/// <summary> /// Applies the data encoding to the value. /// </summary> public static ServiceResult ApplyDataEncoding(ServiceMessageContext context, QualifiedName dataEncoding, ref object value) { // check if nothing to do. if (QualifiedName.IsNull(dataEncoding) || value == null) { return ServiceResult.Good; } // check for supported encoding type. if (dataEncoding.NamespaceIndex != 0) { return StatusCodes.BadDataEncodingUnsupported; } bool useXml = dataEncoding.Name == DefaultXml; if (!useXml && dataEncoding.Name != DefaultBinary) { return StatusCodes.BadDataEncodingUnsupported; } try { // check for array of encodeables. IList<IEncodeable> encodeables = value as IList<IEncodeable>; if (encodeables == null) { // check for array of extension objects. IList<ExtensionObject> extensions = value as IList<ExtensionObject>; if (extensions != null) { // convert extension objects to encodeables. encodeables = new IEncodeable[extensions.Count]; for (int ii = 0; ii < encodeables.Count; ii++) { if (ExtensionObject.IsNull(extensions[ii])) { encodeables[ii] = null; continue; } IEncodeable element = extensions[ii].Body as IEncodeable; if (element == null) { return StatusCodes.BadTypeMismatch; } encodeables[ii] = element; } } } // apply data encoding to the array. if (encodeables != null) { ExtensionObject[] extensions = new ExtensionObject[encodeables.Count]; for (int ii = 0; ii < extensions.Length; ii++) { extensions[ii] = Encode(context, encodeables[ii], useXml); } value = extensions; return ServiceResult.Good; } // check for scalar value. IEncodeable encodeable = value as IEncodeable; if (encodeable == null) { ExtensionObject extension = value as ExtensionObject; if (extension == null) { return StatusCodes.BadDataEncodingUnsupported; } encodeable = extension.Body as IEncodeable; } if (encodeable == null) { return StatusCodes.BadDataEncodingUnsupported; } // do conversion. value = Encode(context, encodeable, useXml); return ServiceResult.Good; } catch (Exception e) { return ServiceResult.Create(e, StatusCodes.BadTypeMismatch, "Could not convert value to requested format."); } }
/// <summary> /// Checks if the value has changed. /// </summary> public virtual bool IsEqual(IEncodeable encodeable) { throw new NotImplementedException("Subclass must implement this method."); }
/// <summary> /// Encodes the object in binary /// </summary> public static byte[] EncodeBinary(IEncodeable encodeable, ServiceMessageContext context) { BinaryEncoder encoder = new BinaryEncoder(context); encoder.WriteEncodeable(null, encodeable, null); return encoder.CloseAndReturnBuffer(); }
/// <summary> /// Shows a value in control. /// </summary> private void ShowValue(ref int index, ref bool overwrite, object value) { if (value == null) { return; } // show monitored items. MonitoredItem monitoredItem = value as MonitoredItem; if (monitoredItem != null) { m_monitoredItem = monitoredItem; ShowValue(ref index, ref overwrite, monitoredItem.LastValue); return; } // show data changes MonitoredItemNotification datachange = value as MonitoredItemNotification; if (datachange != null) { ShowValue(ref index, ref overwrite, datachange.Value); return; } // show write value with IndexRange WriteValue writevalue = value as WriteValue; if (writevalue != null) { // check if the value is an array Array arrayvalue = writevalue.Value.Value as Array; if (arrayvalue != null) { NumericRange indexRange; ServiceResult result = NumericRange.Validate(writevalue.IndexRange, out indexRange); if (ServiceResult.IsGood(result) && indexRange != NumericRange.Empty) { for (int ii = 0; ii < arrayvalue.Length; ii++) { bool enabled = ((indexRange.Begin <= ii && indexRange.End >= ii) || (indexRange.End < 0 && indexRange.Begin == ii)); ShowValue(ref index, ref overwrite, arrayvalue, ii, enabled); } return; } } } // show events EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { for (int ii = 0; ii < eventFields.EventFields.Count; ii++) { ShowValue(ref index, ref overwrite, eventFields, ii); } return; } // show extension bodies. ExtensionObject extension = value as ExtensionObject; if (extension != null) { ShowValue(ref index, ref overwrite, extension.Body); return; } // show encodeables. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { PropertyInfo[] properties = encodeable.GetType().GetProperties(); foreach (PropertyInfo property in properties) { ShowValue(ref index, ref overwrite, encodeable, property); } return; } // show bytes. byte[] bytes = value as byte[]; if (bytes != null) { if (!PromptOnLongList(bytes.Length / 16)) { return; } for (int ii = 0; ii < bytes.Length; ii += 16) { ShowValue(ref index, ref overwrite, bytes, ii); } return; } // show arrays Array array = value as Array; if (array == null) { Matrix matrix = value as Matrix; if (matrix != null) { array = matrix.ToArray(); } } if (array != null) { if (!PromptOnLongList(array.GetLength(0))) { return; } for (int ii = 0; ii < array.GetLength(0); ii++) { ShowValue(ref index, ref overwrite, array, ii); } return; } // show lists IList list = value as IList; if (list != null) { if (!PromptOnLongList(list.Count)) { return; } for (int ii = 0; ii < list.Count; ii++) { ShowValue(ref index, ref overwrite, list, ii); } return; } // show xml elements XmlElement xml = value as XmlElement; if (xml != null) { if (!PromptOnLongList(xml.ChildNodes.Count)) { return; } for (int ii = 0; ii < xml.ChildNodes.Count; ii++) { ShowValue(ref index, ref overwrite, xml, ii); } return; } // show data value. DataValue datavalue = value as DataValue; if (datavalue != null) { ShowValue(ref index, ref overwrite, datavalue, 0); ShowValue(ref index, ref overwrite, datavalue, 1); ShowValue(ref index, ref overwrite, datavalue, 2); ShowValue(ref index, ref overwrite, datavalue, 3); return; } // show node id value. NodeId nodeId = value as NodeId; if (nodeId != null) { ShowValue(ref index, ref overwrite, nodeId, 0); ShowValue(ref index, ref overwrite, nodeId, 1); ShowValue(ref index, ref overwrite, nodeId, 2); return; } // show expanded node id value. ExpandedNodeId expandedNodeId = value as ExpandedNodeId; if (expandedNodeId != null) { ShowValue(ref index, ref overwrite, expandedNodeId, 0); ShowValue(ref index, ref overwrite, expandedNodeId, 1); ShowValue(ref index, ref overwrite, expandedNodeId, 2); ShowValue(ref index, ref overwrite, expandedNodeId, 3); return; } // show qualified name value. QualifiedName qualifiedName = value as QualifiedName; if (qualifiedName != null) { ShowValue(ref index, ref overwrite, qualifiedName, 0); ShowValue(ref index, ref overwrite, qualifiedName, 1); return; } // show qualified name value. LocalizedText localizedText = value as LocalizedText; if (localizedText != null) { ShowValue(ref index, ref overwrite, localizedText, 0); ShowValue(ref index, ref overwrite, localizedText, 1); return; } // show variant. Variant?variant = value as Variant?; if (variant != null) { ShowValue(ref index, ref overwrite, variant.Value.Value); return; } // show unknown types as strings. ShowValue(ref index, ref overwrite, String.Format("{0}", value)); }
/// <summary> /// Saves a data change or event in the cache. /// </summary> public void SaveValueInCache(IEncodeable newValue) { lock (m_cache) { m_lastNotification = newValue; if (m_dataCache != null) { MonitoredItemNotification datachange = newValue as MonitoredItemNotification; if (datachange != null) { Utils.Trace( "SaveValueInCache: ServerHandle={0}, Value={1}, StatusCode={2}", this.ClientHandle, datachange.Value.WrappedValue, datachange.Value.StatusCode); m_dataCache.OnNotification(datachange); } } if (m_eventCache != null) { EventFieldList eventchange = newValue as EventFieldList; if (m_eventCache != null) { m_eventCache.OnNotification(eventchange); } } if (m_Notification != null) { m_Notification(this, new MonitoredItemNotificationEventArgs(newValue)); } } }
/// <summary> /// Writes an encodeable object to the stream. /// </summary> public void WriteEncodeable(string fieldName, IEncodeable value, System.Type systemType) { if (value == null) { WriteSimpleField(fieldName, null, false); return; } PushStructure(fieldName); if (value != null) { value.Encode(this); } PopStructure(); }
/// <summary> /// Encodes the object in XML or Binary /// </summary> public static ExtensionObject Encode(ServiceMessageContext context, IEncodeable encodeable, bool useXml) { if (useXml) { XmlElement body = EncodeableObject.EncodeXml(encodeable, context); return new ExtensionObject(encodeable.XmlEncodingId, body); } else { byte[] body = EncodeableObject.EncodeBinary(encodeable, context); return new ExtensionObject(encodeable.BinaryEncodingId, body); } }
/// <summary> /// Creates a new instance. /// </summary> internal MonitoredItemNotificationEventArgs(IEncodeable notificationValue) { m_notificationValue = notificationValue; }
public bool IsEqual(IEncodeable encodeable) { throw new NotImplementedException(); }
/// <summary> /// The service result for a data change notification. /// </summary> public static ServiceResult GetServiceResult(IEncodeable notification) { MonitoredItemNotification datachange = notification as MonitoredItemNotification; if (datachange == null) { return null; } NotificationMessage message = datachange.Message; if (message != null) { return null; } return new ServiceResult(datachange.Value.StatusCode, datachange.DiagnosticInfo, message.StringTable); }
/// <summary> /// /// </summary> /// <param name="lvi"></param> /// <param name="dv"></param> /// <param name="session"></param> protected virtual void UpdateValueInListView(ListViewItem lvi, DataValue dv, Session session) { if (dv.Value != null) { ExtensionObject extension = dv.Value as ExtensionObject; Array array = dv.Value as Array; if (array != null) { lvi.SubItems[(int)SubItemIndexes.Value].Text = String.Format("{0}[{1}]", dv.Value.GetType().GetElementType().Name, array.Length); ExtensionObject extension1 = array.GetValue(0) as ExtensionObject; if (extension1 != null) { // This returns "Opc.Ua.ArrayType" where arraytype is the correct array type name, should use this for the type column lvi.SubItems[(int)SubItemIndexes.Value].Text = extension1.ToString(); //int i = 0; foreach (object o in array) { ExtensionObject extension2 = o as ExtensionObject; if (extension1 != null) { IEncodeable encodeable1 = extension2.Body as IEncodeable; if (encodeable1 != null) { string name = encodeable1.GetType().Name; //Utils.Trace("encodeable1.GetType().Name: {0}, array[{1}]", name, i++); PropertyInfo[] properties = encodeable1.GetType().GetProperties(); foreach (PropertyInfo property in properties) { //Utils.Trace("property.Name: {0}, property.GetValue: {1}", property.Name, property.GetValue(encodeable1, null) != null ? property.GetValue(encodeable1, null).ToString():"null"); //Utils.Trace("property.Name: {0}, ", property.Name); } } } } } else { StringBuilder sb = new StringBuilder(); int i = 1; sb.Append("{"); foreach (object o in array) { sb.Append(o.ToString()); if (i++ < array.Length) { sb.Append(", "); } } sb.Append("}"); lvi.SubItems[(int)SubItemIndexes.Value].Text = sb.ToString(); } } else if (extension != null) { IEncodeable encodeable = extension.Body as IEncodeable; if (encodeable != null) { lvi.SubItems[(int)SubItemIndexes.Value].Text = extension.ToString();; } else { lvi.SubItems[(int)SubItemIndexes.Value].Text = extension.Body.ToString(); } } else { lvi.SubItems[(int)SubItemIndexes.Value].Text = dv.Value.ToString(); } lvi.ToolTipText = lvi.SubItems[(int)SubItemIndexes.Value].Text; } }
/// <summary> /// Converts a value to a string for display in the grid. /// </summary> private string ValueToString(object value, TypeInfo typeInfo) { if (value == null) { return(String.Empty); } if (value is Variant) { Variant variant = (Variant)value; value = variant.Value; if (value != null) { typeInfo = variant.TypeInfo; if (typeInfo == null) { typeInfo = TypeInfo.Construct(value); } } } if (typeInfo.ValueRank >= 0) { StringBuilder buffer = new StringBuilder(); System.Collections.IEnumerable enumerable = value as System.Collections.IEnumerable; if (enumerable != null) { buffer.Append("{ "); foreach (object element in enumerable) { if (buffer.Length > 2) { buffer.Append(" | "); } if (buffer.Length > MaxDisplayTextLength) { buffer.Append("..."); break; } buffer.Append(ValueToString(element, new TypeInfo(typeInfo.BuiltInType, ValueRanks.Scalar))); } buffer.Append(" }"); } return(buffer.ToString()); } switch (typeInfo.BuiltInType) { case BuiltInType.String: { string text = (string)value; if (text != null && text.Length > MaxDisplayTextLength) { return(text.Substring(0, MaxDisplayTextLength) + "..."); } return(text); } case BuiltInType.ByteString: { StringBuilder buffer = new StringBuilder(); byte[] bytes = (byte[])value; for (int ii = 0; ii < bytes.Length; ii++) { if (buffer.Length > MaxDisplayTextLength) { buffer.Append("..."); break; } buffer.AppendFormat("{0:X2}", bytes[ii]); } return(buffer.ToString()); } case BuiltInType.Enumeration: { return(((int)value).ToString()); } case BuiltInType.ExtensionObject: { string text = null; ExtensionObject extension = value as ExtensionObject; if (extension != null) { if (extension.Body is byte[]) { return(ValueToString(extension.Body, new TypeInfo(BuiltInType.ByteString, ValueRanks.Scalar))); } if (extension.Body is XmlElement) { return(ValueToString(extension.Body, new TypeInfo(BuiltInType.XmlElement, ValueRanks.Scalar))); } if (extension.Body is IEncodeable) { text = new Variant(extension).ToString(); } } if (text == null) { IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { text = new Variant(encodeable).ToString(); } } if (text != null && text.Length > MaxDisplayTextLength) { return(text.Substring(0, MaxDisplayTextLength) + "..."); } return(text); } } return((string)TypeInfo.Cast(value, BuiltInType.String)); }
/// <summary> /// Writes a message in SOAP/XML. /// </summary> public static void WriteSoapMessage( Stream ostrm, string typeName, IEncodeable message, ServiceMessageContext messageContext) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = System.Text.Encoding.UTF8; settings.OmitXmlDeclaration = false; XmlWriter writer = XmlWriter.Create(ostrm, settings); writer.WriteStartElement("soap12", "Envelope", "http://www.w3.org/2003/05/soap-envelope"); XmlEncoder encoder = new XmlEncoder( new XmlQualifiedName("Body", "http://www.w3.org/2003/05/soap-envelope"), writer, messageContext); encoder.PushNamespace(Namespaces.OpcUaXsd); encoder.WriteEncodeable( typeName, message, null); encoder.PopNamespace(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); }
/// <inheritdoc /> public bool IsEqual(IEncodeable encodeable) { return(_wrapped.IsEqual(encodeable)); }
/// <summary> /// Shows property of an encodeable object in the control. /// </summary> private void ShowValue(ref int index, ref bool overwrite, IEncodeable value, PropertyInfo property) { // get the name of the property. string name = Utils.GetDataMemberName(property); if (name == null) { return; } // get the property value. object propertyValue = null; MethodInfo[] accessors = property.GetAccessors(); for (int ii = 0; ii < accessors.Length; ii++) { if (accessors[ii].ReturnType == property.PropertyType) { propertyValue = accessors[ii].Invoke(value, null); break; } } if (propertyValue is Variant) { propertyValue = ((Variant)propertyValue).Value; } // update the list view. UpdateList( ref index, ref overwrite, value, propertyValue, property, name, property.PropertyType.Name); }
/// <summary> /// Returns true if the type can be expanded. /// </summary> private bool IsExpandableType(object value) { // check for null. if (value == null) { return(false); } // check for Variant. if (value is Variant) { return(IsExpandableType(((Variant)value).Value)); } // check for bytes. byte[] bytes = value as byte[]; if (bytes != null) { return(false); } // check for xml element. XmlElement xml = value as XmlElement; if (xml != null) { if (xml.ChildNodes.Count == 1 && xml.ChildNodes[0] is XmlText) { return(false); } return(xml.HasChildNodes); } // check for array. Array array = value as Array; if (array != null) { return(array.Length > 0); } // check for list. IList list = value as IList; if (list != null) { return(list.Count > 0); } // check for encodeable object. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { return(true); } // check for extension object. ExtensionObject extension = value as ExtensionObject; if (extension != null) { return(IsExpandableType(extension.Body)); } // check for data value. DataValue datavalue = value as DataValue; if (datavalue != null) { return(true); } // check for event value. EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { return(true); } // must be a simple value. return(false); }
/// <summary> /// Saves a data change or event in the cache. /// </summary> public void SaveValueInCache(IEncodeable newValue) { lock (m_cache) { m_lastNotification = newValue; if (m_dataCache != null) { MonitoredItemNotification datachange = newValue as MonitoredItemNotification; if (datachange != null) { // validate the ServerTimestamp of the notification. if (datachange.Value != null && datachange.Value.ServerTimestamp > DateTime.UtcNow) { Utils.Trace("Received ServerTimestamp {0} is in the future for MonitoredItemId {1}", datachange.Value.ServerTimestamp.ToLocalTime(), ClientHandle); } // validate SourceTimestamp of the notification. if (datachange.Value != null && datachange.Value.SourceTimestamp > DateTime.UtcNow) { Utils.Trace("Received SourceTimestamp {0} is in the future for MonitoredItemId {1}", datachange.Value.ServerTimestamp.ToLocalTime(), ClientHandle); } if (datachange.Value != null && datachange.Value.StatusCode.Overflow) { Utils.Trace("Overflow bit set for data change with ServerTimestamp {0} and value {1} for MonitoredItemId {2}", datachange.Value.ServerTimestamp.ToLocalTime(), datachange.Value.Value, ClientHandle); } //Utils.Trace( // "SaveValueInCache: ServerHandle={0}, Value={1}, StatusCode={2}", // this.ClientHandle, // datachange.Value.WrappedValue, // datachange.Value.StatusCode); m_dataCache.OnNotification(datachange); } } if (m_eventCache != null) { EventFieldList eventchange = newValue as EventFieldList; if (m_eventCache != null) { m_eventCache.OnNotification(eventchange); } } if (m_Notification != null) { m_Notification(this, new MonitoredItemNotificationEventArgs(newValue)); } } }
/// <summary> /// Writes an encodeable object to the stream. /// </summary> public void WriteEncodeable(string fieldName, IEncodeable value, System.Type systemType) { if (BeginField(fieldName, value == null, true)) { if (value != null) { value.Encode(this); } EndField(fieldName); } }
public bool IsEqual(IEncodeable encodeable) { return(encodeable.Equals(this)); }
/// <summary> /// Saves a data change or event in the cache. /// </summary> public void SaveValueInCache(IEncodeable newValue) { lock (m_cache) { // only validate timestamp on first sample bool validateTimestamp = m_lastNotification == null; m_lastNotification = newValue; if (m_dataCache != null) { MonitoredItemNotification datachange = newValue as MonitoredItemNotification; if (datachange != null) { if (datachange.Value != null) { if (validateTimestamp) { var now = DateTime.UtcNow; // validate the ServerTimestamp of the notification. if (datachange.Value.ServerTimestamp > now) { Utils.LogWarning("Received ServerTimestamp {0} is in the future for MonitoredItemId {1}", datachange.Value.ServerTimestamp.ToLocalTime(), ClientHandle); } // validate SourceTimestamp of the notification. if (datachange.Value.SourceTimestamp > now) { Utils.LogWarning("Received SourceTimestamp {0} is in the future for MonitoredItemId {1}", datachange.Value.SourceTimestamp.ToLocalTime(), ClientHandle); } } if (datachange.Value.StatusCode.Overflow) { Utils.LogWarning("Overflow bit set for data change with ServerTimestamp {0} and value {1} for MonitoredItemId {2}", datachange.Value.ServerTimestamp.ToLocalTime(), datachange.Value.Value, ClientHandle); } } m_dataCache.OnNotification(datachange); } } if (m_eventCache != null) { EventFieldList eventchange = newValue as EventFieldList; if (m_eventCache != null) { m_eventCache.OnNotification(eventchange); } } if (m_Notification != null) { m_Notification(this, new MonitoredItemNotificationEventArgs(newValue)); } } }
/// <summary> /// Handles requests arriving from a channel. /// </summary> private IAsyncResult BeginProcessRequest(Stream istrm, string action, string securityPolicyUri, object callbackData) { IAsyncResult result = null; try { if (m_callback != null) { string contentType = WebOperationContext.Current.IncomingRequest.ContentType; Uri uri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri; string scheme = uri.Scheme + ":"; EndpointDescription endpoint = null; for (int ii = 0; ii < m_descriptions.Count; ii++) { if (m_descriptions[ii].EndpointUrl.StartsWith(scheme)) { if (endpoint == null) { endpoint = m_descriptions[ii]; } if (m_descriptions[ii].SecurityPolicyUri == securityPolicyUri) { endpoint = m_descriptions[ii]; break; } } } IEncodeable request = null; if (String.IsNullOrEmpty(action)) { request = BinaryDecoder.DecodeMessage(istrm, null, this.m_quotas.MessageContext); } else { string requestType = "Opc.Ua." + action + "Request"; request = HttpsTransportChannel.ReadSoapMessage( istrm, action + "Request", Type.GetType(requestType), this.m_quotas.MessageContext); } result = m_callback.BeginProcessRequest( m_listenerId, endpoint, request as IServiceRequest, null, callbackData); } } catch (Exception e) { Utils.Trace(e, "HTTPSLISTENER - Unexpected error processing request."); } return(result); }
/// <summary> /// Formats a value for display in the control. /// </summary> private string GetValueText(object value) { // check for null. if (value == null) { return("(null)"); } // format bytes. byte[] bytes = value as byte[]; if (bytes != null) { StringBuilder buffer = new StringBuilder(); for (int ii = 0; ii < bytes.Length; ii++) { if (ii != 0 && ii % 16 == 0) { buffer.Append(" "); } buffer.AppendFormat("{0:X2} ", bytes[ii]); } return(buffer.ToString()); } // format xml element. XmlElement xml = value as XmlElement; if (xml != null) { // return the entire element if not expandable. if (!IsExpandableType(xml)) { return(xml.OuterXml); } // show only the start tag. string text = xml.OuterXml; int index = text.IndexOf('>'); if (index != -1) { text = text.Substring(0, index); } return(text); } // format array. Array array = value as Array; if (array != null) { if (array.Rank > 1) { int[] lenghts = new int[array.Rank]; for (int i = 0; i < array.Rank; ++i) { lenghts[i] = array.GetLength(i); } return(Utils.Format("{1}[{0}]", string.Join(",", lenghts), value.GetType().GetElementType().Name)); } else { return(Utils.Format("{1}[{0}]", array.Length, value.GetType().GetElementType().Name)); } } // format list. IList list = value as IList; if (list != null) { string type = value.GetType().Name; if (type.EndsWith("Collection")) { type = type.Substring(0, type.Length - "Collection".Length); } else { type = "Object"; } return(Utils.Format("{1}[{0}]", list.Count, type)); } // format encodeable object. IEncodeable encodeable = value as IEncodeable; if (encodeable != null) { return(encodeable.GetType().Name); } // format extension object. ExtensionObject extension = value as ExtensionObject; if (extension != null) { return(GetValueText(extension.Body)); } // check for event value. EventFieldList eventFields = value as EventFieldList; if (eventFields != null) { if (m_monitoredItem != null) { return(String.Format("{0}", m_monitoredItem.GetEventType(eventFields))); } return(eventFields.GetType().Name); } // check for data value. DataValue dataValue = value as DataValue; if (dataValue != null) { StringBuilder formattedValue = new StringBuilder(); if (!StatusCode.IsGood(dataValue.StatusCode)) { formattedValue.Append("["); formattedValue.AppendFormat("Q:{0}", dataValue.StatusCode); } DateTime now = DateTime.UtcNow; if ((dataValue != null) && ((dataValue.ServerTimestamp > now) || (dataValue.SourceTimestamp > now))) { if (formattedValue.ToString().Length > 0) { formattedValue.Append(", "); } else { formattedValue.Append("["); } formattedValue.Append("T:future"); } if (formattedValue.ToString().Length > 0) { formattedValue.Append("] "); } formattedValue.AppendFormat("{0}", dataValue.Value); return(formattedValue.ToString()); } // use default formatting. return(Utils.Format("{0}", value)); }
/// <summary cref="ICallable.Call" /> public virtual ServiceResult Call( OperationContext context, NodeId methodId, object methodHandle, NodeId objectId, IList <object> inputArguments, IList <ServiceResult> argumentErrors, IList <object> outputArguments) { // find the method to call for the object. CallbackParameters callbackParameters = null; lock (DataLock) { if (!m_executable || !m_userExecutable) { return(StatusCodes.BadUserAccessDenied); } if (!m_callbacks.TryGetValue(objectId, out callbackParameters)) { return(ServiceResult.Create(StatusCodes.BadInternalError, "No handler specified for the method.")); } } try { // dereference extension objects. List <object> convertedArguments = new List <object>(); foreach (object inputArgument in inputArguments) { ExtensionObject extension = inputArgument as ExtensionObject; if (extension != null) { IEncodeable encodeable = extension.Body as IEncodeable; if (encodeable != null) { convertedArguments.Add(encodeable); continue; } } convertedArguments.Add(inputArgument); } // invoke the method. Call( context, callbackParameters.Target, callbackParameters.Callback, convertedArguments, argumentErrors, outputArguments); // check for argument errors. foreach (ServiceResult argumentError in argumentErrors) { if (ServiceResult.IsBad(argumentError)) { return(StatusCodes.BadInvalidArgument); } } // everthing ok. return(ServiceResult.Good); } catch (Exception e) { return(new ServiceResult(e)); } }