9 Replies Latest reply on Dec 3, 2014 8:20 PM by jorgewong

    PI Web API - Post a tag of type Digital State

    jorgewong

      Hello,

       

       

       

      I'm trying to set a value for a PI tag that is a digital state.  I'm posting the following but getting a bad request error back.  It doesn't' give me much else to go on.  I'm assuming its probably my message that I'm posting.  I'm fine posting anything else that is not a digital state.  Any help is appreciated.

       

       

       

      Here are some of the details of my post:

       

       

       

      Path : https://server/piwebapi/streams/P0WvDSaT8pY0uGA4is91RAcQtlkDAAQ0dZTVNBUFNDMDA1RFxKRjFfQTMwMDBfV0FDX1BPTF9SRUdFTl9TSUdIVF9HTEFTUw/value?updateOption=Replace&bufferOption=DoNotBuffer

       

      Tag Value: { Timestamp = {01/12/2014 4:20:25 PM}, Value = {   "Name": "1",   "Value": "0" } } 

       

       

       

      Request Message:

       

      {Method: POST, RequestUri:

       

       

       

      'https://server/piwebapi/streams/P0WvDSaT8pY0uGA4is91RAcQtlkDAAQ0dZTVNBUFNDMDA1RFxKRjFfQTMwMDBfV0FDX1BPTF9SRUdFTl9TSUdIVF9HTEFTUw/value?updateOption=Replace&bufferOption=DoNotBuffer', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[<>f__AnonymousType5`2[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], PIWebWrapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:{  Accept: application/json  Content-Type: application/json; charset=utf-8  Content-Length: 74}}

       

       

       

       

       

      Error:

       

      {StatusCode: 400, ReasonPhrase:

       

       

       

       

       

       

       

      'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{  Date: Wed, 03 Dec 2014 17:26:36 GMT  Server: Microsoft-HTTPAPI/2.0  Content-Length: 71  Content-Type: application/json; charset=utf-8}}

       

       

       

       

       

      The digital set consists of 4 states:

       

      State | State Name

       

      0 | 1

       

      1 | 2

       

      2 | 3

       

      3 | 4

       

       

       

       

        • Re: PI Web API - Post a tag of type Digital State
          Marcos Vainer Loeff

          Hi Jorge,

           

          My suggestion for you is to try to send the value "Active" to the BA:ACTIVE.1 PI Point. Make sure that you are using Content-Type: application/json on the header.  

           

          I don't understand why you are using query strings on the URI. This is something that you would use for GET methods. For POST methods, data should be sent through the headers.

           

          If it doesn't work, please share with us your code snippet.

          1 of 1 people found this helpful
            • Re: PI Web API - Post a tag of type Digital State
              awoodall

              I couldn't write to a digital tag using the example request body you gave because it wasn't valid JSON. If I used the following it worked ok and returned 202.

               

              {

               

               "Timestamp": "01/12/2014 4:20:25 PM",

               

               "Value": "1"

               

              }

               

              Also note the body of the error response usually contains a description of what the issue is. For example if you were writing the digital state to a PI Point and you accidently included a UnitsAbbreviation parameter in the body (even if with no value), my 400 response would have the JSON message

               

              {"Errors":["Point streams cannot be associated with units of measure."]}

               

              For your sample request body I got the following error which hints at the JSON being malformed

               

              {"Errors":["Parameter 'value' is required but was not supplied.","Invalid character after parsing property name. Expected ':' but got: =. Path '', line 1, position 12."]}

               

              Arnold

              • Re: PI Web API - Post a tag of type Digital State
                jorgewong

                Yeah that works.  For some reason I was thinking I had to send some sort of object to the API.  This is pretty simple.  It seems as though the api will look at the value and look for a match with state name first and if that doesn't find a match it uses state.  Using my digital set above that was what occurred.  I'm happy with this as it simplifies what I've built and requires no code changes.

                 

                Is there a way to retrieve a list of the possible digital states in that set?

                  • Re: PI Web API - Post a tag of type Digital State
                    Marcos Vainer Loeff

                    Hi Jorge,

                     

                    PI Web API 2014 R2 does not support that feature. But I am not sure if it will be available on PI Web API 2015. I will ask the developers and let you know!

                      • Re: PI Web API - Post a tag of type Digital State
                        awoodall

                        I've heard that the next version of PI Web API will have Enumeration Set support and that Digital State sets should fall under this umbrella. We'll see if it has what you are looking for in the CTP of the 2015 release in the coming week or two.

                         

                        Arnold

                        • Re: PI Web API - Post a tag of type Digital State
                          Bhess

                          A few things I'd like to supplement to this thread:

                           

                          -- It's perfectly appropriate to use the updateOption and bufferOption URL query parameters on the POST calls to insert PI data.

                           

                          -- I'm a bit befuddled on how "Value": "1" works for you, since that's not really the designed behavior, and in my mind that's a bit of an anomaly.  :-/  I'd expect for either "Value": 1 to work (using a JSON number for the numeric value of the digital state/enumeration value), or "Value": "Active" (using a JSON string for the name of the digital state/enumeration value).  PI Web API may disallow that implicit conversion from string value to numeric value in a future release, to make this less ambiguous (i.e. what is the current behavior when you have an digital state named "2" whose value is 1?).

                           

                          -- We do plan to support listing all of the values of an AF enumeration set in PI Web API 2015.