/// <summary> /// Test our PI server /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTestPI_Click(object sender, EventArgs e) { if (Client == null) { MessageBox.Show(this, "Not connected!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop); } else { DateTime StartTime = DateTime.Now; Guid Resp = Client.QueryTags("W_BATESV.LN.*", DateTime.Now.AddDays(-7), DateTime.Now, 10); MM_Historic_Query.enumQueryState State = Client.CheckQueryStatus(Resp); while (State != MM_Historic_Query.enumQueryState.HadError && State != MM_Historic_Query.enumQueryState.Completed) { Application.DoEvents(); State = Client.CheckQueryStatus(Resp); } MM_Historic_Query Results = Client.RetrieveQueryResults(Resp); dataGridView1.DataSource = Results.DeserializeTable(); if (State == MM_Historic_Query.enumQueryState.HadError) { MessageBox.Show(this, "An error occurred retrieving PI data.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(this, "Historic data retrieved in " + (DateTime.Now - StartTime).ToString() + ".", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
/// <summary> /// Run a History query in its own thread /// </summary> /// <param name="state"></param> private static void RunQuery(object state) { MM_Historic_Query Query = (MM_Historic_Query)state; Query.UpdateState(MM_Historic_Query.enumQueryState.InProgress); //If our timing information is bad, don't query. if (Query.StartTime == default(DateTime) || Query.EndTime == default(DateTime)) { Query.UpdateState(MM_Historic_Query.enumQueryState.HadError); return; } //Start by retrieving all points that match our values try { DataTable OutTable = new DataTable(Query.Id.ToString()); MM_Historic_DataPoint[] InPoints = HistoryServer.GetPoints("Tag='" + Query.QueryText + "'"); //Start by building our table OutTable.PrimaryKey = new DataColumn[] { OutTable.Columns.Add("Date", typeof(DateTime)) }; foreach (MM_Historic_DataPoint pt in InPoints) { OutTable.Columns.Add(pt.Name, typeof(double)).DefaultValue = DBNull.Value; } //Now go through each point, and retrive the data foreach (MM_Historic_DataPoint pt in InPoints) { foreach (MM_Historic_DataValue Val in pt.RetrieveValues(Query.StartTime, Query.EndTime, Query.NumPoints)) { DataRow FoundRow; if ((FoundRow = OutTable.Rows.Find(Val.TimeStamp)) == null) { FoundRow = OutTable.Rows.Add(Val.TimeStamp); } else { FoundRow[pt.Name] = Convert.ToDouble(Val.Value); } } } //Now, update the query state to indicate completion Query.SerializeTable(OutTable); Query.UpdateState(MM_Historic_Query.enumQueryState.Completed); } catch (Exception ex) { DataTable OutTable = new DataTable("Error"); OutTable.Columns.Add("Error", typeof(String)); OutTable.Rows.Add(ex.ToString()); Query.SerializeTable(OutTable); Query.UpdateState(MM_Historic_Query.enumQueryState.HadError); } }
/// <summary> /// Initiate a query, and start it /// </summary> /// <param name="TagSQL"></param> /// <param name="EndTime"></param> /// <param name="NumPoints"></param> /// <param name="StartTime"></param> /// <returns></returns> public Guid QueryTags(string TagSQL, DateTime StartTime, DateTime EndTime, int NumPoints) { //First, build our new query MM_Historic_Query Query = new MM_Historic_Query(); Query.Id = Guid.NewGuid(); Query.State = MM_Historic_Query.enumQueryState.Queued; Query.QueryText = TagSQL; Query.LastUpdate = DateTime.Now; Query.StartTime = StartTime; Query.EndTime = EndTime; Query.NumPoints = NumPoints; //Now, add our query to the list, and trigger the MRE. MM_Historic_Reader.Queries.Add(Query.Id, Query); MM_Historic_Reader.mreQueryReady.Set(); User.LastReceivedMessage = DateTime.Now; return(Query.Id); }