Solution: WebId v2.0

Document created by jkim Employee on Jun 24, 2019
Version 1Show Document
  • View in full screen mode
  1. Generate a path-only WebId v2.0 programmatically for "\\PIAF\Nugreen\Nugreen\Wichita\Cracking Process\Equipment\P-723|Installation Date"

This can be done in a multitude of languages, this is an example of building a WebID from scratch in C#

 

using System;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Threading.Tasks;

using Newtonsoft.Json;

 

 

namespace PI_Web_API_2_0_WebIds

{

    class Program

    {

        // Create a client to make calls with

        static HttpClient client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });

 

        static void Main(string[] args)

        {

            // Call the method to get a point

            GetAttribute().GetAwaiter().GetResult();

 

            Console.ReadKey();

        }

 

        static async Task GetAttribute()

        {

            client.BaseAddress = new Uri("https://Hostname/piwebapi");

            client.DefaultRequestHeaders.Accept.Clear();

            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

 

            // Query for a tag using path only

            try

            {

                string type = "P"; // Specify the information in WebID as Path-only

                string version = "1"; // for WebID v2.0, this is always 1

                string marker = "Ab"; // Specify Object type as PIPoint

                string extraMarker = "E"; // Identifies the parent AFObject type of the Attribute as an Element

                string path = "PIAF1\\Nugreen\\Nugreen\\Wichita\\Cracking Process\\Equipment\\P-723|Installation Date";

 

                // Encode the path as base64

                byte[] value = System.Text.Encoding.UTF8.GetBytes(path.ToUpperInvariant());

                string encoded = System.Convert.ToBase64String(value);

 

                // Remove Special characters

               encoded =  encoded.TrimEnd(new char[] { '=' }).Replace('+', '-').Replace('/', '_');

 

                // Build the WebId and the URL

                string WebId = string.Format("{0}{1}{2}{3}{4}", type, version, marker, extraMarker, encoded);

                string URL = string.Format("https://Hostname/piwebapi/attributes/{0}", WebId);

 

                HttpResponseMessage response = await client.GetAsync(URL);

                if (response.IsSuccessStatusCode)

                {

                    string responsebody = await response.Content.ReadAsStringAsync();

                    Object deser = JsonConvert.DeserializeObject(responsebody);

                    Console.Write(deser);

                }

            }

            catch (AggregateException ex)

            {

                foreach (var e in ex.InnerExceptions)

                {

                    Console.WriteLine(e.Message);

                }

            }

        }

    }

}

Attachments

    Outcomes