Exemple #1
0
        /* Console application that makes GET request to a specified URL and display the response
         * as a string to the console. */
        static void Main(string[] args)
        {
            PIWebAPIClient piWebAPIClient = new PIWebAPIClient();
            do
            {
                try
                {
                    Console.Write("Enter URL: ");
                    string url = Console.ReadLine();
                    JObject jobj = piWebAPIClient.GetRequest(url);
                    Console.WriteLine(jobj.ToString());
                }
                catch (AggregateException ex)
                {
                    foreach (var e in ex.InnerExceptions)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                finally
                {
                    Console.WriteLine("Press any key to continue (esc to exit)...");
                }

            } while (Console.ReadKey().Key != ConsoleKey.Escape);
            piWebAPIClient.Dispose();
            Console.ReadKey();
        }
        /* This method takes the username and password specified to use basic authentication to connect to 
         * PI Web API. It then attempts to resolve the tag path provided and write to the tag. */
        private async void writeBtn_Click(object sender, RoutedEventArgs e)
        {
            string baseUrl = "https://dng-code.osisoft.int/piwebapi"; //change the baseUrl to your PI Web API service
            string userName = userNameTextBox.Text;
            string password = pwBox.Password;
            string tagPath = tagTextBox.Text;
            PIWebAPIClient piWebAPIClient = new PIWebAPIClient(userName, password);

            try
            {
                //Resolve tag path
                string requestUrl = baseUrl + "/points/?path=" + tagPath;
                Task<JObject> tget = piWebAPIClient.GetAsync(requestUrl);
                statusTextBlock.Text = "Processing...";

                //Attempt to write value to the tag
                Object payload = new
                {
                    value = valueTextBox.Text
                };
                string data = JsonConvert.SerializeObject(payload);
                JObject jobj = await tget;
                await piWebAPIClient.PostAsync(jobj["Links"]["Value"].ToString(), data);

                //Display final results if successful
                statusTextBlock.Text = "Write success!";
            }
            catch (HttpRequestException ex)
            {
                statusTextBlock.Text = ex.Message;
            }
            catch (Exception ex)
            {
                statusTextBlock.Text = ex.Message;
            }
            finally
            {
                //We are closing the HttpClient after every write in this simple example. This is not necessary.
                piWebAPIClient.Dispose();
            }
        }