private void ProcessQueue(object state)
        {
            if (m_InProcessValues)
            {
                return;
            }
            if (m_closing)
            {
                return;
            }
            m_InProcessValues = true;

            ClassTagValues[] arrTagValues      = new ClassTagValues[0];
            Int32            numberOfTagValues = 0;

            lock (m_DataValuesQueue.SyncRoot)
            {
                if (m_DataValuesQueue.Count < 1)
                {
                    m_InProcessValues = false;
                    return; // There is nothing to do
                }
                numberOfTagValues = m_DataValuesQueue.Count;
                arrTagValues      = new ClassTagValues[numberOfTagValues];
                m_DataValuesQueue.CopyTo(arrTagValues, 0);
                m_DataValuesQueue.Clear();
            }

            Int32          allTagValuesIndex = 0;
            ClassTagValues allTagValues      = null;

            string[]   tagNames      = null;
            object[]   tagValues     = null;
            bool[]     tagQualities  = null;
            DateTime[] tagTimeStamps = null;
            Int32      tagIndex      = 0;
            Int32      numberOfTags  = 0;

            System.Text.StringBuilder UpdateString = new System.Text.StringBuilder();
            double ValueDouble  = 0;
            int    ValueInteger = 0;
            bool   ValueBoolean = false;
            string ValueString  = null;

            ValueDouble  = 0;
            ValueInteger = 0;
            ValueBoolean = false;
            ValueString  = "";

            for (allTagValuesIndex = 0; allTagValuesIndex < numberOfTagValues; allTagValuesIndex++)
            {
                // Obtain arrays of tags and values from the tag values class previously stored in the OpcSystemsData_ValuesChangedAll event
                allTagValues  = arrTagValues[allTagValuesIndex];
                tagNames      = allTagValues.TagNames;
                tagValues     = allTagValues.Values;
                tagQualities  = allTagValues.Qualities;
                tagTimeStamps = allTagValues.TimeStamps;
                numberOfTags  = tagNames.GetLength(0);
                TagList tagList = new TagList();
                string  value   = null;
                for (tagIndex = 0; tagIndex < numberOfTags; tagIndex++)
                {
                    if (tagQualities[tagIndex])
                    {
                        if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(double)))
                        {
                            value = Convert.ToDouble(tagValues[tagIndex]).ToString();
                        }
                        else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(int)))
                        {
                            value = Convert.ToInt32(tagValues[tagIndex]).ToString();
                        }
                        else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(bool)))
                        {
                            value = Convert.ToBoolean(tagValues[tagIndex]).ToString();
                        }
                        else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(string)))
                        {
                            value = tagValues[tagIndex].ToString();
                        }
                        if (DataType == "Grid")
                        {
                            _hubContext.Clients.All.SendAsync("updateTagValue", new TagList {
                                TagId = tagNames[tagIndex], TagName = simplifyTagName(tagNames[tagIndex]), TimeStamp = tagTimeStamps[tagIndex], Value = value
                            });
                        }
                        if (tagNames[tagIndex] == "Random.Value")
                        {
                            _hubContext.Clients.All.SendAsync("insertChartData", new ChartData {
                                TagName = tagNames[tagIndex].Substring(0, tagNames[tagIndex].IndexOf(".")), TimeStamp = tagTimeStamps[tagIndex], Value = Convert.ToDouble(value)
                            });
                            _hubContext.Clients.All.SendAsync("updateGaugeValue", Convert.ToDouble(value));
                        }
                    }
                }

                lock (m_DataValuesHashtable.SyncRoot)
                {
                    for (tagIndex = 0; tagIndex < numberOfTags; tagIndex++)
                    {
                        try
                        {
                            UpdateString.Append(tagTimeStamps[tagIndex].ToString("HH:mm:ss.fff") + " ");
                        }
                        catch (Exception ex)
                        {
                            UpdateString.Append("Unknown Time ");
                        }
                        UpdateString.Append(tagNames[tagIndex] + " = ");

                        if (tagQualities[tagIndex]) // The Tag quality is good
                        {
                            // Store value to a hashtable so you can access it from your own routines
                            if (m_DataValuesHashtable.Contains(tagNames[tagIndex]))
                            {
                                m_DataValuesHashtable[tagNames[tagIndex]] = tagValues[tagIndex];
                            }
                            else
                            {
                                m_DataValuesHashtable.Add(tagNames[tagIndex], tagValues[tagIndex]);
                            }
                            try
                            {
                                if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(double)))
                                {
                                    ValueDouble = Convert.ToDouble(tagValues[tagIndex]);
                                    UpdateString.Append(ValueDouble.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(int)))
                                {
                                    ValueInteger = Convert.ToInt32(tagValues[tagIndex]);
                                    UpdateString.Append(ValueInteger.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(bool)))
                                {
                                    ValueBoolean = Convert.ToBoolean(tagValues[tagIndex]);
                                    UpdateString.Append(ValueBoolean.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(string)))
                                {
                                    ValueString = tagValues[tagIndex].ToString();
                                    UpdateString.Append(ValueString);
                                }
                            }
                            catch (Exception ex)
                            {
                                UpdateString.Append("Error");
                            }
                        }
                        else // The Tag quality is bad
                        {
                            // Remove the value from the hashtable so you know the value is bad from your own routines
                            if (m_DataValuesHashtable.Contains(tagNames[tagIndex]))
                            {
                                m_DataValuesHashtable.Remove(tagNames[tagIndex]);
                            }
                            UpdateString.Append("Bad Quality");
                        }
                        UpdateString.Append("\r" + "\n");
                        try
                        {
                            //foreach(var i in tagListModel)
                            //{
                            //    Debug.WriteLine("TagListModel tagName: " + i.TagName);
                            //}

                            //_hubContext.Clients.All.SendAsync("updateTagValues", tagListModel);

                            //Debug.WriteLine(UpdateString.ToString());
                            UpdateString.Remove(0, UpdateString.Length);
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            m_InProcessValues = false;
        }
Example #2
0
        private void TimerToProcessValues_Tick(object sender, System.EventArgs e)
        {
            if (m_InProcessValues)
            {
                return;
            }
            if (m_Closing)
            {
                return;
            }
            m_InProcessValues = true;

            ClassTagValues[] arrTagValues      = new ClassTagValues[0];
            Int32            numberOfTagValues = 0;

            lock (m_DataValuesQueue.SyncRoot)
            {
                if (m_DataValuesQueue.Count < 1)
                {
                    m_InProcessValues = false;
                    return; // There is nothing to do
                }
                numberOfTagValues = m_DataValuesQueue.Count;
                arrTagValues      = new ClassTagValues[numberOfTagValues];
                m_DataValuesQueue.CopyTo(arrTagValues, 0);
                m_DataValuesQueue.Clear();
            }

            Int32          allTagValuesIndex = 0;
            ClassTagValues allTagValues      = null;

            string[]   tagNames      = null;
            object[]   tagValues     = null;
            bool[]     tagQualities  = null;
            DateTime[] tagTimeStamps = null;
            Int32      tagIndex      = 0;
            Int32      numberOfTags  = 0;

            System.Text.StringBuilder UpdateString = new System.Text.StringBuilder();
            double ValueDouble  = 0;
            int    ValueInteger = 0;
            bool   ValueBoolean = false;
            string ValueString  = null;

            ValueDouble  = 0;
            ValueInteger = 0;
            ValueBoolean = false;
            ValueString  = "";

            for (allTagValuesIndex = 0; allTagValuesIndex < numberOfTagValues; allTagValuesIndex++)
            {
                // Obtain arrays of tags and values from the tag values class previously stored in the OpcSystemsData_ValuesChangedAll event
                allTagValues  = arrTagValues[allTagValuesIndex];
                tagNames      = allTagValues.TagNames;
                tagValues     = allTagValues.Values;
                tagQualities  = allTagValues.Qualities;
                tagTimeStamps = allTagValues.TimeStamps;
                numberOfTags  = tagNames.GetLength(0);
                lock (m_DataValuesHashtable.SyncRoot)
                {
                    for (tagIndex = 0; tagIndex < numberOfTags; tagIndex++)
                    {
                        try
                        {
                            UpdateString.Append(tagTimeStamps[tagIndex].ToString("HH:mm:ss.fff") + " ");
                        }
                        catch (Exception ex)
                        {
                            UpdateString.Append("Unknown Time ");
                        }
                        UpdateString.Append(tagNames[tagIndex] + " = ");

                        if (tagQualities[tagIndex]) // The Tag quality is good
                        {
                            // Store value to a hashtable so you can access it from your own routines
                            if (m_DataValuesHashtable.Contains(tagNames[tagIndex]))
                            {
                                m_DataValuesHashtable[tagNames[tagIndex]] = tagValues[tagIndex];
                            }
                            else
                            {
                                m_DataValuesHashtable.Add(tagNames[tagIndex], tagValues[tagIndex]);
                            }
                            try
                            {
                                if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(double)))
                                {
                                    ValueDouble = Convert.ToDouble(tagValues[tagIndex]);
                                    UpdateString.Append(ValueDouble.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(int)))
                                {
                                    ValueInteger = Convert.ToInt32(tagValues[tagIndex]);
                                    UpdateString.Append(ValueInteger.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(bool)))
                                {
                                    ValueBoolean = Convert.ToBoolean(tagValues[tagIndex]);
                                    UpdateString.Append(ValueBoolean.ToString());
                                }
                                else if (object.ReferenceEquals(tagValues[tagIndex].GetType(), typeof(string)))
                                {
                                    ValueString = tagValues[tagIndex].ToString();
                                    UpdateString.Append(ValueString);
                                }
                            }
                            catch (Exception ex)
                            {
                                UpdateString.Append("Error");
                            }
                        }
                        else // The Tag quality is bad
                        {
                            // Remove the value from the hashtable so you know the value is bad from your own routines
                            if (m_DataValuesHashtable.Contains(tagNames[tagIndex]))
                            {
                                m_DataValuesHashtable.Remove(tagNames[tagIndex]);
                            }
                            UpdateString.Append("Bad Quality");
                        }
                        UpdateString.Append("\r" + "\n");
                        try
                        {
                            if (!(m_Closing))
                            {
                                //if (TextBoxValues.Text.Length > (TextBoxValues.MaxLength - 100))
                                //{
                                //    TextBoxValues.Text = "";
                                //}
                                //TextBoxValues.AppendText(UpdateString.ToString());
                                UpdateString.Remove(0, UpdateString.Length);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            m_InProcessValues = false;
        }