Ejemplo n.º 1
0
        /// <see cref="BaseListCtrl.UpdateItem" />
        protected override void UpdateItem(ListViewItem listItem, object item)
        {
            MonitoredItem monitoredItem = item as MonitoredItem;

            if (monitoredItem == null)
            {
                base.UpdateItem(listItem, item);
                return;
            }

            listItem.SubItems[0].Text = String.Format("{0}", monitoredItem.Status.Id);
            listItem.SubItems[1].Text = String.Format("{0}", monitoredItem.DisplayName);
            listItem.SubItems[2].Text = String.Format("{0}", monitoredItem.NodeClass);
            listItem.SubItems[3].Text = String.Format("{0}", monitoredItem.Status.SamplingInterval);
            listItem.SubItems[4].Text = String.Format("{0}", monitoredItem.Status.QueueSize);
            listItem.SubItems[5].Text = String.Empty;
            listItem.SubItems[6].Text = String.Format("{0}", monitoredItem.Status.Error);
            listItem.SubItems[7].Text = String.Empty;

            IEncodeable value = monitoredItem.LastValue;

            if (value != null)
            {
                MonitoredItemNotification datachange = value as MonitoredItemNotification;

                if (datachange != null)
                {
                    listItem.SubItems[5].Text = String.Format("{0}", datachange.Value);

                    if (datachange.Value.SourceTimestamp != DateTime.MinValue)
                    {
                        listItem.SubItems[7].Text = String.Format("{0:HH:mm:ss.fff}",
                                                                  datachange.Value.SourceTimestamp.ToLocalTime());
                    }
                }

                EventFieldList eventFields = value as EventFieldList;

                if (eventFields != null)
                {
                    listItem.SubItems[5].Text = String.Format("{0}", monitoredItem.GetEventType(eventFields));
                    listItem.SubItems[7].Text = String.Format("{0:HH:mm:ss.fff}",
                                                              monitoredItem.GetEventTime(eventFields).ToLocalTime());
                }
            }

            listItem.Tag = item;
        }
        /// <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));
        }
Ejemplo n.º 3
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));
        }