7 Replies Latest reply on May 17, 2010 11:54 PM by cescamilla

    Dynamically accessing PI with .NET 4.0

    MichaelvdV@Atos

      I started to play around with the DynamicObject class in .NET 4.0.

       

      I have the tendancy to explore new technologies with PI. So, my goal was to create a 'dynamic PISDK' (the term 'SDK' should be taken lightly, because the implementation is very limited).

       

      If you want to have some sense to how this works: the project is attached to this post (VS 2010 beta 2, .NET 4.0 beta). warning: For programmers not use to dynamic typed languages this can seem very strange and non-intuitive/unusable.

       

      Here is an example on how to use it:

      
      

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using DPISDK.CORE;

      namespace DPISDK
      {
          class Program
          {
              static void Main(string[] args)
              {
                  //Note: nlms025 is my local pi server. Change it to your PI servername.

                  //Creating the dynamic SDK
                  dynamic dpisdk = new DPISDK.CORE.DPISDK();
                  //Accessing the tag 'sinusoid' on Pi server 'nlms025', and getting the 'snapshot' member.
                  var snap = dpisdk.nlms025.sinusoid.Snapshot;
                  //This returns a dynamic object called 'DPiValue', which contains the value, timestamp and isgood. It also overrides the ToString() to
                  //print the formatted values
                  Console.WriteLine("Snapshot: " + snap);
           
                  //Accessing the tag 'sinusoidu' on Pi Server 'nlms025', and calling a dynamic function 'recordedvalues'.
                  var values = dpisdk.nlms025.sinusoidu.recordedvalues("*", "*-2h");
                  //This returns a List<DPiValue>. Print the list using the overridden ToString() method
                  foreach (DPiValue value in values)
                      Console.WriteLine(value);

                  //Accessing the tag 'sinusoid' on Pi Server 'nlms025', getting the 'descriptor' point attribute. Please note that getting a descriptor and
                  //the snapshot (property) is on the same level. Please view the implementation of DPiPoint's TryGetMember to get a better sense of what's happening.
                  Console.WriteLine(dpisdk.nlms025.sinusoid.descriptor);

                  //We want to see the results in Console, so wait for the enter key to exit
                  Console.ReadLine();

              }
          }
      }

       

       

        • Re: Dynamically accessing PI with .NET 4.0

          Wow, now that is weird!! (that's why the delay getting back to you on this one... needed to get my head around this and understand it a bit better... although I'm not sure I really understand it)

           

          Anyways, thanks for opening my eyes on this new technique - I'll read more on the topic and I yet have to find out where this can apply in my applications...

           

          With that said, I think I found a little glitch in the instantiation of the server: you cannot use "default" as the server name, since this is a reserved keyword (re: if (name.ToLower() == "default") in DServer.cs). You should change this to something else like "defaultserver".

           

          Also, how do you instantiate servers or points that have illegal characters? 2 examples are my server name, which has a dash in it, and the "ba:phase.1" tag.

            • Re: Dynamically accessing PI with .NET 4.0
              MichaelvdV@Atos

              Michael Gibson

              Wow, now that is weird!! (that's why the delay getting back to you on this one... needed to get my head around this and understand it a bit better... although I'm not sure I really understand it)

               

              It is kind of 'weird' for us static typed people . I'm really glad you took the time to look at it!

               

              Michael Gibson

              Anyways, thanks for opening my eyes on this new technique - I'll read more on the topic and I yet have to find out where this can apply in my applications...

               

              Dynamic Typing (and the DLR) is a great technique, but I think (at this time) there are only a few cases where you will really need it (where you really need dynamic typing, and can't solve the problem with static typing).

               

              Michael Gibson

              With that said, I think I found a little glitch in the instantiation of the server: you cannot use "default" as the server name, since this is a reserved keyword (re: if (name.ToLower() == "default") in DServer.cs). You should change this to something else like "defaultserver".

               

              You are absolutely right! This should be something like 'defaultserver' indeed! Thanks for bringing this to my attention!

               

              Michael Gibson

              Also, how do you instantiate servers or points that have illegal characters? 2 examples are my server name, which has a dash in it, and the "ba:phase.1" tag.

               

              Well, at this point... This is an issue with the dynamic typing. Your PI tagnames and servernames have to be compliant with .NET propertynames if you want to use it in the 'real dynamic typing way'.

               

              An option is:

              
              

               dynamic dServer = default(dynamic);

                          if (dpisdk.TryGetMember("some:illegal.server,name", out dServer))
                          {
                              Console.WriteLine(dServer.sinusoid.snapshot);
                          }

               

              This uses the overridden method from DynamicObject directly.

               

              So, it is possible, but it's not very pretty