schugh@nalco.com

Security settings to connect to PI WS server from desktop based windows form application.

Discussion created by schugh@nalco.com on Sep 9, 2011
Latest reply on Sep 9, 2011 by Ahmad Fattahi

We have developed a form based reporting applicaiton to report our measurement data. When we took it to our real test environment from our local desktop. its not working . the call to get data is not at all working. When we tried to execute the call using WCF Strom it gives proxy authentication errors. We added the below code to our app.config file of our form applicaiton but stiill in making a pi call it is not working.application goes into a halting state. we are doing button1 click in the code...what are the settings required in the code or app.config to pass proxy autentication.

 

here is our code and app.config. kindly advise..

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<?

 

 

 

xml version="1.0" encoding="utf-8"

?>

<

 

 

 

 

configuration

>

<

 

 

 

 

configSections

>

<

 

 

 

 

sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

>

<

 

 

 

 

section name="defaultProxy" type="System.Net.Configuration.DefaultProxySection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

/>

</

 

 

 

 

sectionGroup

>

</

 

 

 

 

configSections

>

<

 

 

 

 

system.net

>

<

 

 

 

 

defaultProxy useDefaultCredentials="true"

/>

</

 

 

 

 

system.net

>

 

<

 

 

 

system.serviceModel

>

<

 

 

 

 

behaviors

>

<

 

 

 

 

endpointBehaviors

>

<

 

 

 

 

behavior name="ImpersonationLevel"

>

<

 

 

 

 

clientCredentials

>

<

 

 

 

 

windows allowedImpersonationLevel="Impersonation"

/>

 

</

 

 

 

 

clientCredentials

>

</

 

 

 

 

behavior

>

</

 

 

 

 

endpointBehaviors

>

</

 

 

 

 

behaviors

>

 

  

<

 

 

 

bindings

>

<

 

 

 

 

wsHttpBinding

>

<

 

 

 

 

binding name="BasicEndpoint" closeTimeout="00:02:00" openTimeout="00:02:00

"

 

 

 

 

receiveTimeout="00:02:00" sendTimeout="00:02:00" bypassProxyOnLocal="false

"

 

 

 

 

transactionFlow="false" hostNameComparisonMode="StrongWildcard

"

 

 

 

 

maxBufferPoolSize="5242880" maxReceivedMessageSize="1310731110

"

 

 

 

 

messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true

"

 

 

 

 

allowCookies="false"

>

<

 

 

 

 

readerQuotas maxDepth="320" maxStringContentLength="81920" maxArrayLength="163840

"

 

 

 

 

maxBytesPerRead="8192" maxNameTableCharCount="16384"

/>

<

 

 

 

 

reliableSession ordered="true" inactivityTimeout="00:02:00

"

 

 

 

 

enabled="false"

/>

 

 

<

 

 

 

 

security mode="Message"

>

<

 

 

 

 

transport clientCredentialType="Windows" proxyCredentialType="Windows"

 

 

 

 

 

realm=""

/>

 

<

 

 

 

 

message clientCredentialType="Windows" negotiateServiceCredential="true

"

 

 

 

 

algorithmSuite="Default"

/>

</

 

 

 

 

security

>

</

 

 

 

 

binding

>

</

 

 

 

 

wsHttpBinding

>

</

 

 

 

 

bindings

>

<

 

 

 

 

client

>

<

 

 

 

 

endpoint address="http://usnap3daf30.nalco.one.net/PIWebServices/PITimeSeries.svc

"

 

 

 

 

binding="wsHttpBinding" bindingConfiguration="BasicEndpoint

"

 

 

 

 

contract="PIWebServiceRef.IPITimeSeries" name="BasicEndpoint"

>

<

 

 

 

 

identity

>

<

 

 

 

 

servicePrincipalName value="HOST/USNAP3DAF30"

/>

</

 

 

 

 

identity

>

</

 

 

 

 

endpoint

>

 

</

 

 

 

 

client

>

</

 

 

 

 

system.serviceModel

>

  

 

 

 

 

</

 

 

 

configuration

>

 

 

 

code is below

 

 

 

using

 

 

System;

using

 

 

System.Collections.Generic;

using

 

 

System.ComponentModel;

using

 

 

System.Data;

using

 

 

System.Drawing;

using

 

 

System.Linq;

using

 

 

System.Text;

using

 

 

System.Windows.Forms;

using

 

 

System.IO;

using

 

 

System.Data.SqlClient;

using

 

 

System.Collections;

using

 

 

System.Data.SqlTypes;

using

 

 

System.Net;

namespace

 

 

WindowsFormsApplication2

{

 

 

public partial class Form1 : Form

{

 

 

DataSet ds = new DataSet("tagsdata");

 

 

public Form1()

{

InitializeComponent();

}

 

 

private void Form1_Load(object sender, EventArgs e)

 

 

 

{

 

 

  

}

 

 

private void label1_Click(object sender, EventArgs e)

{

 

}

 

 

private void button1_Click(object sender, EventArgs e)

{

 

 

int numberofparameters=0;

 

 

int i = 0;

 

 

var dataDictionary = new Dictionary<string, string>();

 

 

 

DataTable pData = new DataTable();

pData.TableName =

 

"pData";

pData.Columns.Add(

 

"Timestamp", typeof(string));

 

 

ds.Clear();

pData.Clear();

 

label4.Text =

 

DateTime.Now.ToString();

 

 

if (radioButton1.Checked == true)

{

databasecall(4);

numberofparameters = 4;

}

 

 

if (radioButton2.Checked == true)

{

databasecall(8);

numberofparameters = 8;

}

 

 

if (radioButton3.Checked == true)

{

databasecall(100);

numberofparameters = ds.Tables[0].Rows.Count;

}

 

 

PIWebServiceRef.

 

PITimeSeriesClient _Proxy = new PIWebServiceRef.PITimeSeriesClient("BasicEndpoint");

 

 

_Proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.

 

TokenImpersonationLevel.Delegation;

PIWebServiceRef.

 

TimeRange MyTimeRange = new PIWebServiceRef.TimeRange();

 

 

 

 

 

//PIWebServiceRef.PIArcDataRequest[] RequestsList = new PIWebServiceRef.PIArcDataRequest[ds.Tables[0].Rows.Count];

PIWebServiceRef.

 

PIArcDataRequest[] RequestsList = new PIWebServiceRef.PIArcDataRequest[ds.Tables[0].Rows.Count];

PIWebServiceRef.

 

PIArcManner SelectedManner = new PIWebServiceRef.PIArcManner();

 

 

// SelectedManner.Boundaries = PIWebServiceRef.PIArcMannerBoundaries.Interpolated;

SelectedManner.RetrievalType = PIWebServiceRef.

 

PIArcMannerRetrievalType.Interpolated;

 

 

if (radioButton4.Checked == true)

{

MyTimeRange.Start =

 

"t-131040m";

MyTimeRange.End =

 

"t-1440m";

SelectedManner.NumValues =12961;

}

 

 

if (radioButton5.Checked == true)

{

MyTimeRange.Start =

 

"t-260640m";

MyTimeRange.End =

 

"t-1440m";

SelectedManner.NumValues = 25921;

}

 

 

if (radioButton6.Checked == true)

{

MyTimeRange.Start =

 

"t-527040m";

MyTimeRange.End =

 

"t";

SelectedManner.NumValues = 52705;

 

 

//SelectedManner.NumValues = 35136;

}

SelectedManner.Updates =

 

false;

 

 

 

foreach (DataRow dr in ds.Tables[0].Rows)

{

 

PIWebServiceRef.

 

PIArcDataRequest Request = new PIWebServiceRef.PIArcDataRequest();

 

 

string servername = @"pi:\\usnap3dpi20" + @"\";

 

 

string tagname = dr["tagname"].ToString();

 

 

string fullpath = servername + tagname;

Request.Path = fullpath;

 

Request.TimeRange = MyTimeRange;

Request.PIArcManner = SelectedManner;

RequestsList = Request;

i = i + 1;

 

}

  

  

  

label14.Text =

 

DateTime.Now.ToString();

 

 

try

{

PIWebServiceRef.

 

TimeSeries[] MyResults = _Proxy.GetPIArchiveData(RequestsList);

 

  

label16.Text =

 

DateTime.Now.ToString();

TimeTakenPI.Text =

 

Convert.ToString(Convert.ToDateTime(label16.Text) - Convert.ToDateTime(label14.Text));

 

 

 

 

if (MyResults.Length >= 1)

{

 

 

//Extract the (expected) single time series.

 

 

//<Instructions to Remove to produce the template>

 

 

//ArrayList ListofTags = new ArrayList(ds.Tables[0].Rows.Count);

 

 

 

for (int J = 0; J < ds.Tables[0].Rows.Count; J++)

{

 

 

//ds.Tables[0].Rows.Count-1

PIWebServiceRef.

 

TimeSeries TS = MyResults[J];

 

 

 

 

//Read the status, the UOM and type returned.

 

 

int l = 1;

 

 

//Extract the values one by one.

 

 

foreach (PIWebServiceRef.TimedValue MyValue in TS.TimedValues)

{

 

 

 

string OutputString = string.Empty;

 

 

//Grab the values in the series and put them in the UI.

 

 

if (MyValue.Value != null)

{

 

 

DateTime valueTime = MyValue.Time.AddHours(-5);

 

 

try

{

dataDictionary.Add(J +

 

"^" + valueTime.AddDays(1).ToString("MM/dd/yyyy H:mm"), Convert.ToString(Math.Round(Convert.ToDecimal(MyValue.Value), 2)));

}

 

 

catch (Exception ex)

{

dataDictionary.Add(J +

 

"^" + valueTime.AddDays(1).ToString("MM/dd/yyyy H:mm"), Convert.ToString(MyValue.Value));

}

}

 

 

 

 

//Add the output string to the list.

 

}

}

  

  

 

 

 

for (int tag = 0; tag < ds.Tables[0].Rows.Count; tag++)

{

 

 

//pData.Columns.Add(ds.Tables[0].Columns["parametername"].ToString(), typeof(string));

pData.Columns.Add(ds.Tables[0].Rows[tag][0].ToString()+

 

" P" +tag, typeof(string));

}

  

PIWebServiceRef.

 

TimeSeries TS1 = MyResults[0];

 

 

int j=0;

 

 

foreach (PIWebServiceRef.TimedValue MyValue1 in TS1.TimedValues)

{

 

 

if (MyValue1.Value != null)

{

 

 

DateTime valueTime1 = MyValue1.Time.AddHours(-5);

 

 

string datetimestring = j + "^" + valueTime1.AddDays(1).ToString("MM/dd/yyyy H:mm");

pData.Rows.Add();

pData.Rows[j][0] = valueTime1.AddDays(1).ToString(

 

"MM/dd/yyyy H:mm");

  

 

 

for (int tag = 0; tag < ds.Tables[0].Rows.Count; tag++)

{

 

 

// ds.Tables[0].Rows.Count- 1

 

 

string value;

  

 

 

try

{

value = dataDictionary[tag +

 

"^" + valueTime1.AddDays(1).ToString("MM/dd/yyyy H:mm")];

}

 

 

catch

{

value =

 

"No Data";

}

pData.Rows[j][tag + 1] = value;

}

}

j = j + 1;

 

}

dataGridView1.DataSource = pData;

label8.Text =

 

DateTime.Now.ToString();

TimeTakenCode.Text =

 

Convert.ToString(Convert.ToDateTime(label8.Text) - Convert.ToDateTime(label4.Text));

 

 

 

  

  

 

}

  

  

}

 

 

catch (Exception ex)

{

label27.Text = ex.Message.ToString();

}

  

 

  

  

}

 

 

private void richTextBox1_TextChanged(object sender, EventArgs e)

{

}

 

 

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

 

 

private void label2_Click(object sender, EventArgs e)

{

}

 

 

private void uxStatusTextBox_TextChanged(object sender, EventArgs e)

{

}

 

 

private void databasecall(int numberofparameters)

{

 

 

SqlConnection conn = new SqlConnection("Data Source=usnapwpdb20;Initial Catalog=TRASAR3;User ID=TRASAR4;Password=TRASAR4web");

 

 

string commanndtext = "select top " + numberofparameters + " PN.ParameterName as pname ,M.PiTagName as tagname from Measurement M inner join ParameterNames PN on M.ParameterID=PN.ParaNameID where M.ControllerID=10734 order by m.CreatedOn";

  

 

 

SqlCommand cmd = new SqlCommand(commanndtext, conn);

  

 

 

SqlDataAdapter sqlda = new SqlDataAdapter(cmd);

sqlda.Fill(ds);

ds.WriteXml(

 

"d:\\tagsdata.xml");

}

 

 

private void button2_Click(object sender, EventArgs e)

{

 

 

//databasecall();

PIWebServiceRef.

 

PITimeSeriesClient _Proxy = new PIWebServiceRef.PITimeSeriesClient("BasicEndpoint");

_Proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.

 

TokenImpersonationLevel.Delegation;

PIWebServiceRef.

 

PISummaryDataRequest[] RequestsList = new PIWebServiceRef.PISummaryDataRequest[ds.Tables[0].Rows.Count];

 

PIWebServiceRef.

 

TimeRange MyTimeRange = new PIWebServiceRef.TimeRange();

MyTimeRange.Start =

 

"t-180d";

MyTimeRange.End =

 

"t";

PIWebServiceRef.

 

PISummaryManner SelectedManner = new PIWebServiceRef.PISummaryManner();

SelectedManner.WeightType = PIWebServiceRef.

 

PISummaryMannerWeightType.EventWeighted;

SelectedManner.SummaryValue = PIWebServiceRef.

 

PISummaryMannerSummaryValue.Average;

 

SelectedManner.Intervals = 90;

SelectedManner.UseStart =

 

false;

SelectedManner.Updates =

 

false;

 

 

int i = 0;

 

 

foreach (DataRow dr in ds.Tables[0].Rows)

{

PIWebServiceRef.

 

PISummaryDataRequest Singlerequest = new PIWebServiceRef.PISummaryDataRequest();

 

 

string servername = @"pi:\\usnap3dpi90" + @"\";

 

 

string tagname = dr["tagname"].ToString();

 

 

string fullpath = servername + tagname;

Singlerequest.Path = fullpath;

 

 

//label1.Text = fullpath;

Singlerequest.TimeRange = MyTimeRange;

Singlerequest.PISummaryManner = SelectedManner;

RequestsList = Singlerequest;

  

  

i = i + 1;

 

 

//if (i == 20) break;

}

  

 

 

// label8.Text = Convert.ToString(DateTime.Now);

PIWebServiceRef.

 

TimeSeries[] MyResults = _Proxy.GetPISummaryData(RequestsList);

 

 

//label9.Text = Convert.ToString(DateTime.Now);

  

  

  

 

 

if (MyResults.Length >= 1)

{

 

 

//Extract the (expected) single time series.

 

 

//<Instructions to Remove to produce the template>

 

 

//ArrayList ListofTags = new ArrayList(ds.Tables[0].Rows.Count);

 

 

var dataDictionary = new Dictionary<string, string>();

 

 

var totalTags = ds.Tables[0].Rows.Count;

 

 

for (int J = 0; J < ds.Tables[0].Rows.Count - 1; J++)

{

PIWebServiceRef.

 

TimeSeries TS = MyResults[J];

  

 

 

//Read the status, the UOM and type returned.

 

 

//Extract the values one by one.

 

 

foreach (PIWebServiceRef.TimedValue MyValue in TS.TimedValues)

{

 

 

string OutputString = string.Empty;

 

 

//Grab the values in the series and put them in the UI.

 

 

if (MyValue.Value != null)

{

 

 

DateTime valueTime = MyValue.Time.AddHours(-5);

  

dataDictionary.Add(J +

 

"^" + valueTime.ToString("dd/MM/yyyy H:mm"), MyValue.Value);

}

 

 

// tw.WriteLine(" Value = " + MyValue.Value + "Time = " + MyValue.Time + " Current Time =" + DateTime.Now);

  

 

 

// tagsdata.Rows.Add( MyValue.Time,MyValue.Value);

 

 

//Add the output string to the list.

  

}

}

 

 

DataTable pData = new DataTable();

pData.TableName =

 

"pData";

pData.Columns.Add(

 

"Timestamp", typeof(string));

 

 

for (int tag = 0; tag < ds.Tables[0].Rows.Count - 1; tag++)

{

 

 

//pData.Columns.Add(ds.Tables[0].Columns["parametername"].ToString(), typeof(string));

pData.Columns.Add(ds.Tables[0].Rows[tag][0].ToString() + tag,

 

typeof(string));

}

  

PIWebServiceRef.

 

TimeSeries TS1 = MyResults[0];

 

 

int k = 0;

 

 

foreach (PIWebServiceRef.TimedValue MyValue1 in TS1.TimedValues)

{

 

 

if (MyValue1.Value != null)

{

 

 

DateTime valueTime1 = MyValue1.Time.AddHours(-5);

 

 

string datetimestring = k + "^" + valueTime1.ToString("dd/MM/yyyy H:mm");

pData.Rows.Add();

pData.Rows[k][0] = valueTime1.ToString(

 

"dd/MM/yyyy H:mm");

  

 

 

for (int tag = 0; tag < ds.Tables[0].Rows.Count - 1; tag++)

{

 

 

string value;

  

 

 

try

{

value = dataDictionary[tag +

 

"^" + valueTime1.ToString("dd/MM/yyyy H:mm")];

}

 

 

catch

{

value =

 

"No Data";

}

pData.Rows[k][tag + 1] = value;

}

k = k + 1;

}

 

 

else

{

 

 

DateTime valueTime1 = MyValue1.Time.AddHours(-5);

 

 

string datetimestring = k + "^" + valueTime1.ToString("dd/MM/yyyy H:mm");

pData.Rows.Add();

pData.Rows[k][0] = valueTime1.ToString(

 

"dd/MM/yyyy H:mm");

 

 

for (int tag = 0; tag < ds.Tables[0].Rows.Count - 1; tag++)

{

 

 

string value;

  

 

 

try

{

value = dataDictionary[tag +

 

"^" + valueTime1.ToString("dd/MM/yyyy H:mm")];

}

 

 

catch

{

value =

 

"No Data";

}

pData.Rows[k][tag + 1] = value;

}

k = k + 1;

}

 

}

dataGridView2.DataSource = pData;

 

 

// secondtime.Text = DateTime.Now.ToString();

  

  

  

  

  

 

 

//</Instructions to Remove to produce the template>

}

  

 

 

 

  

  

  

 

  

}

 

 

private void button3_Click(object sender, EventArgs e)

{

dataGridView1.Visible =

 

false;

 

 

//firsttme.Text = DateTime.Now.ToString();

 

 

//databasecall();

 

  

  

  

 

 

 

int i = 0;

PIWebServiceRef.

 

PITimeSeriesClient _Proxy = new PIWebServiceRef.PITimeSeriesClient("BasicEndpoint");

_Proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.

 

TokenImpersonationLevel.Delegation;

 

 

string[] tags = new string[ds.Tables[0].Rows.Count];

 

 

 

foreach (DataRow dr in ds.Tables[0].Rows)

{

PIWebServiceRef.

 

PIArcDataRequest Request = new PIWebServiceRef.PIArcDataRequest();

 

 

string servername = @"pi:\\usnap3dpi90" + @"\";

 

 

string tagname = dr["tagname"].ToString();

 

 

string fullpath = servername + tagname;

 

tags = fullpath;

  

  

i = i + 1;

 

}

 

  

  

PIWebServiceRef.

 

TimeSeries[] MyResults = _Proxy.GetPISnapshotData(tags);

 

 

//endtimer.Text = DateTime.Now.ToString();

PIWebServiceRef.

 

TimeSeries TS1 = MyResults[0];

 

 

DataTable pData = ds.Tables[0];

pData.Columns.Add(

 

"Timestamp", typeof(string));

pData.Columns.Add(

 

"Value", typeof(string));

 

 

int k = 0;

 

 

foreach (PIWebServiceRef.TimedValue MyValue1 in TS1.TimedValues)

{

 

 

if (MyValue1.Value != null)

{

pData.Rows.Add();

pData.Rows[k][0] = MyValue1.Time;

pData.Rows[k][1] = MyValue1.Value;

 

 

  

}

  

}

dataGridView3.DataSource = pData;

  

  

  

}

 

 

private void button4_Click(object sender, EventArgs e)

{

 

 

int numberoftags=0;

label19.Text =

 

Convert.ToString(DateTime.Now);

 

 

 

DateTime dt=DateTime.Today.AddDays(-4);

 

 

DataSet ds1 = new DataSet();

  

 

 

 

SqlConnection conn = new SqlConnection("Data Source=usnapwpdb20;Initial Catalog=TRASAR3;User ID=TRASAR4;Password=TRASAR4web");

 

 

SqlCommand PI_GetMeasurementData = new SqlCommand

(

 

"clrperformance", conn);

PI_GetMeasurementData.CommandType =

 

CommandType.StoredProcedure;

PI_GetMeasurementData.CommandTimeout = 7200;

 

 

 

SqlParameter p1= new SqlParameter("@starttime",SqlDbType.DateTime);

p1.Direction =

 

ParameterDirection.Input;

 

 

if (radioButton4.Checked == true)

{

dt =

 

DateTime.Today.AddDays(-90);

}

 

 

else if (radioButton5.Checked == true)

{

dt =

 

DateTime.Today.AddDays(-180);

}

 

 

else if (radioButton6.Checked == true)

{

dt =

 

DateTime.Today.AddDays(-365);

}

p1.Value =

 

new SqlDateTime(dt);

 

 

 

//p1.Value = new SqlDateTime(""08/20/2011");

PI_GetMeasurementData.Parameters.Add(p1);

 

 

SqlParameter p2 = new SqlParameter("@endtime", SqlDbType.DateTime);

p2.Direction =

 

ParameterDirection.Input;

 

 

DateTime dt1 = DateTime.Today;

p2.Value =

 

new SqlDateTime(dt1);

PI_GetMeasurementData.Parameters.Add(p2);

 

 

SqlParameter p3 = new SqlParameter("@nooftags", SqlDbType.Int);

p3.Direction =

 

ParameterDirection.Input;

 

 

if (radioButton1.Checked == true)

{

p3.Value = 4;

numberoftags=4;

}

 

 

else if (radioButton2.Checked == true)

{

p3.Value = 8;

numberoftags=8;

}

 

 

else if (radioButton3.Checked == true)

{

p3.Value = 100;

numberoftags=100;

}

PI_GetMeasurementData.Parameters.Add(p3);

  

 

 

 

SqlDataAdapter da = new SqlDataAdapter(PI_GetMeasurementData);

label23.Text =

 

DateTime.Now.ToString();

  

da.Fill(ds1);

label25.Text =

 

DateTime.Now.ToString();

  

label27.Text =

 

Convert.ToString(Convert.ToDateTime(label25.Text) - Convert.ToDateTime(label23.Text));

  

ds1.WriteXml(

 

"d:\\firsttagdata1.xml");

 

 

DataTable pData = new DataTable();

pData.TableName =

 

"pData";

pData.Columns.Add(

 

"Timestamp", typeof(string));

 

 

for (int tag = 0; tag < numberoftags; tag++)

{

pData.Columns.Add(

 

"Tag" + tag, typeof(string));

}

 

 

//for (int tag = 0; tag < ds.Tables[0].Rows.Count - 1; tag++)

 

 

//{

 

 

// //pData.Columns.Add(ds.Tables[0].Columns["parametername"].ToString(), typeof(string));

 

 

// pData.Columns.Add(ds.Tables[0].Rows[tag][0].ToString() + tag, typeof(string));

 

 

//}

 

 

int k = 0;

 

 

int startIndex=0;

 

 

int endIndex=0;

pData.Rows.Clear();

 

 

foreach (DataRow dr1 in ds1.Tables[0].Rows)

{

 

pData.Rows.Add();

pData.Rows[k][0] = dr1[

 

"Time"];

 

 

string datavalue = dr1["Value"].ToString();

 

 

string value;

startIndex = 0;

endIndex = 0;

 

 

for (int tag = 0; tag < numberoftags; tag++)

{

endIndex = datavalue.IndexOf(

 

"^", startIndex);

 

 

if (endIndex == -1)

{

endIndex = datavalue.Length;

}

value = datavalue.Substring(startIndex, (endIndex - startIndex));

startIndex = endIndex+1;

pData.Rows[k][tag + 1] = value;

}

k = k + 1;

 

}

dataGridView2.DataSource = pData;

label22.Text =

 

DateTime.Now.ToString();

label29.Text =

 

Convert.ToString(Convert.ToDateTime(label22.Text) - Convert.ToDateTime(label19.Text));

 

 

  

}

 

 

private void chart1_Click(object sender, EventArgs e)

{

}

 

 

private void button5_Click(object sender, EventArgs e)

{

  

 

 

 

//databasecall();

 

  

  

 

 

 

int i = 0;

PIWebServiceRef.

 

PITimeSeriesClient _Proxy = new PIWebServiceRef.PITimeSeriesClient("BasicEndpoint");

_Proxy.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.

 

TokenImpersonationLevel.Delegation;

PIWebServiceRef.

 

TimeRange MyTimeRange = new PIWebServiceRef.TimeRange();

MyTimeRange.Start =

 

"t-180d";

MyTimeRange.End =

 

"t";

 

 

 

//PIWebServiceRef.PIArcDataRequest[] RequestsList = new PIWebServiceRef.PIArcDataRequest[ds.Tables[0].Rows.Count];

PIWebServiceRef.

 

PIArcDataRequest[] RequestsList = new PIWebServiceRef.PIArcDataRequest[5];

PIWebServiceRef.

 

PIArcManner SelectedManner = new PIWebServiceRef.PIArcManner();

 

 

// SelectedManner.Boundaries = PIWebServiceRef.PIArcMannerBoundaries.Inside;

SelectedManner.RetrievalType = PIWebServiceRef.

 

PIArcMannerRetrievalType.Compressed;

SelectedManner.NumValues = 17280;

SelectedManner.Updates =

 

false;

 

 

foreach (DataRow dr in ds.Tables[0].Rows)

{

 

 

if (i < 5)

{

PIWebServiceRef.

 

PIArcDataRequest Request = new PIWebServiceRef.PIArcDataRequest();

 

 

string servername = @"pi:\\usnap3dpi90" + @"\";

 

 

string tagname = dr["tagname"].ToString();

 

 

string fullpath = servername + tagname;

Request.Path = fullpath;

 

Request.TimeRange = MyTimeRange;

Request.PIArcManner = SelectedManner;

RequestsList = Request;

}

i = i + 1;

 

}

 

 

//starttimer.Text = DateTime.Now.ToString();

  

  

PIWebServiceRef.

 

TimeSeries[] MyResults = _Proxy.GetPIArchiveData(RequestsList);

 

 

///endtimer.Text = DateTime.Now.ToString();

 

  

  

  

  

  

  

 

 

 

if (MyResults.Length >= 1)

{

 

 

 

var dataDictionary = new Dictionary<string, string>();

 

 

var totalTags = ds.Tables[0].Rows.Count;

 

 

for (int J = 0; J <5; J++)

{

 

 

//ds.Tables[0].Rows.Count-1

PIWebServiceRef.

 

TimeSeries TS = MyResults[J];

  

 

 

//Read the status, the UOM and type returned.

 

 

//Extract the values one by one.

 

 

foreach (PIWebServiceRef.TimedValue MyValue in TS.TimedValues)

{

 

 

string OutputString = string.Empty;

 

 

//Grab the values in the series and put them in the UI.

 

 

if (MyValue.Value != null)

{

 

 

DateTime valueTime = MyValue.Time.AddHours(-6);

  

dataDictionary.Add(J +

 

"^" + valueTime.ToString("dd/MM/yyyy H:mm"), MyValue.Value);

}

 

 

// tw.WriteLine(" Value = " + MyValue.Value + "Time = " + MyValue.Time + " Current Time =" + DateTime.Now);

  

 

 

// tagsdata.Rows.Add( MyValue.Time,MyValue.Value);

 

 

//Add the output string to the list.

  

}

}

 

 

DataTable pData = new DataTable();

pData.TableName =

 

"pData";

pData.Columns.Add(

 

"Timestamp", typeof(string));

 

 

for (int tag = 0; tag < 5 ; tag++)

{

 

//ds.Tables[0].Rows.Count- 1

 

 

//pData.Columns.Add(ds.Tables[0].Columns["parametername"].ToString(), typeof(string));

pData.Columns.Add(ds.Tables[0].Rows[tag][0].ToString() + tag,

 

typeof(string));

}

  

PIWebServiceRef.

 

TimeSeries TS1 = MyResults[0];

 

 

int j = 0;

 

 

foreach (PIWebServiceRef.TimedValue MyValue1 in TS1.TimedValues)

{

 

 

if (MyValue1.Value != null)

{

 

 

// DateTime valueTime1 = MyValue1.Time.AddHours(-5);

 

 

string datetimestring = j + "^" + MyValue1.Time.ToString("dd/MM/yyyy H:mm");

pData.Rows.Add();

pData.Rows[j][0] = MyValue1.Time.AddHours(6).ToString(

 

"dd/MM/yyyy H:mm");

  

 

 

for (int tag = 0; tag < 5; tag++)

{

 

 

// ds.Tables[0].Rows.Count- 1

 

 

string value;

  

 

 

try

{

value = dataDictionary[tag +

 

"^" + MyValue1.Time.ToString("dd/MM/yyyy H:mm")];

}

 

 

catch

{

value =

 

"No Data";

}

pData.Rows[j][tag + 1] = value;

}

}

j = j + 1;

}

dataGridView1.DataSource = pData;

 

 

//secondtime.Text = DateTime.Now.ToString();

  

  

  

  

  

 

 

//</Instructions to Remove to produce the template>

}

 

 

//dataGridView1.DataSource = pData;

 

 

//uxStatusTextBox.Text = Convert.ToString(OutputData.Rows.Count);

 

}

 

 

private void radioButton6_CheckedChanged(object sender, EventArgs e)

{

}

 

 

private void label18_Click(object sender, EventArgs e)

{

}

 

 

private void label20_Click(object sender, EventArgs e)

{

}

 

 

private void label29_Click(object sender, EventArgs e)

{

}

 

  

  

  

  

  

  

}

}

Outcomes