コード例 #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lvi"></param>
 /// <param name="dv"></param>
 /// <param name="session"></param>
 protected virtual void UpdateTypeInListView(ListViewItem lvi, DataValue dv, Session session)
 {
     if (dv.Value != null)
     {
         Array array = dv.Value as Array;
         if (array != null)
         {
             ExtensionObject extension1 = array.GetValue(0) as ExtensionObject;
             if (extension1 != null)
             {
                 IEncodeable encodeable = extension1.Body as IEncodeable;
                 if (encodeable != null)
                 {
                     string name = encodeable.GetType().Name;
                     lvi.SubItems[(int)SubItemIndexes.Type].Text = String.Format("{0}[{1}]", name, array.Length);
                 }
             }
             else
             {
                 lvi.SubItems[(int)SubItemIndexes.Type].Text = String.Format("{0}[{1}]", dv.Value.GetType().GetElementType().ToString(), array.Length);
             }
         }
         else
         {
             ExpandedNodeId  datatypeId = null;
             ExtensionObject extension  = dv.Value as ExtensionObject;
             if (extension != null)
             {
                 IEncodeable encodeable = extension.Body as IEncodeable;
                 if (encodeable != null)
                 {
                     datatypeId = encodeable.TypeId;
                 }
                 else
                 {
                     datatypeId = DataTypes.GetDataTypeId(dv.Value);
                 }
             }
             else
             {
                 datatypeId = DataTypes.GetDataTypeId(dv.Value);
             }
             Node datatype = session.NodeCache.Find(datatypeId) as Node;
             if (datatype != null)
             {
                 lvi.SubItems[(int)SubItemIndexes.Type].Text = datatype.ToString();
             }
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Encodes a message in a stream.
        /// </summary>
        private static void EncodeBinaryMessage(IEncodeable message, Stream stream,
                                                ServiceMessageContext context)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            MemoryStream buffer = null;

            try {
                //
                // Binary encoder unfortunately seeks (to keep track of
                // position). Therefore we need to wrap a non seeking stream.
                //
                var output = stream.CanSeek ? stream : buffer = new MemoryStream();
                using (var encoder = new BinaryEncoder(output, context)) {
                    // convert the namespace uri to an index.
                    var typeId = ExpandedNodeId.ToNodeId(message.BinaryEncodingId,
                                                         context.NamespaceUris);
                    // write the type id.
                    encoder.WriteNodeId(null, typeId);
                    // write the message.
                    encoder.WriteEncodeable(null, message, message.GetType());
                }
            }
            finally {
                if (buffer != null)
                {
                    stream.Write(buffer.ToArray());
                    buffer.Dispose();
                }
            }
        }
        /// <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>
        /// 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));
        }
コード例 #5
0
 /// <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;
     }
 }
コード例 #6
0
        /// <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));
        }
コード例 #7
0
        /// <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));
        }