6 Replies Latest reply on Oct 25, 2018 3:09 PM by Eugene Lee

    Custom symbol PITime parser

    MDJ

      Hi All

      (PIVision Custom Symbols for PiVision2017 R2)

      Is there a way to reuse build in PITIme parser to parse/validate inserted time in my own javascript - Custom Symbol? Same time parser/validator that is used to parse start/end time of TimeBar?

      Create own regex to fully match the PiVision time parser is waste of time, and is not correct approach to do that, handle any kind of options that Osisoft intruduced/is introducing/will introduce, should be reusable - keep same behaving in whole PIVision.

      i.e. correctly parse to Time (Dae and Time) 3jan4-1d+2h+34m -> 1/2/2004 2:34:00 AM., 3jan4-y -> is today at 12:00:00 AM, but 3jan4-y-1h is 1/2/2004 11:00:00 PM

       

      Thanks

        • Re: Custom symbol PITime parser
          Eugene Lee

          I think the time parsing happens on the server side with the data service of PI Vision so there is no way to do it on the client.

          • Re: Custom symbol PITime parser
            Eugene Lee

            Confirmed that the time parsing happens on the server side with the development team.

             

            One possible workaround to this is to leverage on PI Web API to do the time parsing for you. The steps are

             

            1.Send a request to the GetValue method of the Stream controller with the timestring in the time parameter. E.g. "*-1h"

            2.Parse out the returned Timestamp field.

            3.Convert the UTC time to your local time if needed.

             

            Point of note: Remember to consider the timezone when you are trying to resolve the timestring. This is important if the client and the server are in different timezones. The timeZone parameter allows you to define the client time zone. If you do not define the timeZone parameter, the default is the server timezone. The timestamps returned by PI Web API are always in UTC. Check below for more information.

             

            GetValue GET streams/{webId}/value

            1 of 1 people found this helpful
              • Re: Custom symbol PITime parser
                MDJ

                Thanks for hints,

                I was suspecting that the time parsing is done on server, because none of the scripts from TimeBar of PIVision was containing any direct logic for it. I understand - keep it in privacy, but they should introduce for it some API.

                 

                To use piwebapi as a validator is also one way of workaround, to set time format and parse the answer. If the answer is correct, than the time is valid other wise is invalid.

                {

                  "Errors": [

                  "The specified timestamp is not valid. If more details are needed, please contact your PI Web API administrator for help in enabling debug mode."

                  ]

                }

                 

                But it is an input field validator of time, which should not do any trafic to server and back.

                 

                I wrote some more restrict regex for it to support validator directly on the client side: Relaive time, Fixied time, date format: MM/dd/YYYY hh:mm:ss (AM/PM)?, dd.MM.yyyy hh:mm:ss (24hours/12hours support).

                 

                ^\s*([+\-]|([ty*])|(mon|tue|wed|thu|fri|sat|sun)|(monday|tuesday|wednesday|thursday|friday|saturday|sunday)|(((((0?\d|[12]\d|3[01])[-]?)?(jan|feb|mar|apr|may|jun|jul|aug|sep|okt|nov|dec)[-]?(19[7-9]\d|2\d{3}|\d{1,2})?)|([1][0-2]|[0]?[1-9])\/([3][01]|[1-2]\d|[0]?[1-9])(\/((19[7-9]\d|2\d{3})|\d{1,2}))?|([3][01]|[1-2]\d|[0]?[1-9])[.]([1][0-2]|[0]?[1-9])([.]((19[7-9]\d|2\d{3})|\d{1,2}))?|([3][01]|[1-2]\d|[0]?[1-9]))(\s+(((0?\d|1[0-2]):(0?\d|[1-5]\d):(0?\d|[1-5]\d)\s+(AM|PM))|(((1\d|2[0-3]|0?\d):([1-5]\d|0?\d)):([1-5]\d|0?\d))))?))(\s*([+-])\s*\d+([.,]\d+)?(ms|mo|s|m|h|d|w|y))?(\s*([+-])?\s*\d+([.,]\d+)?\s*(ms|mo|s|m|h|d|w|y))*$

                 

                or more readable form

                 

                var flexible = '(\\s*([+-])\\s*\\d+([.,]\\d+)?(ms|mo|s|m|h|d|w|y))?(\\s*([+-])?\\s*\\d+([.,]\\d+)?\\s*(ms|mo|s|m|h|d|w|y))*';

                var hours12 = '((0?\\d|1[0-2]):(0?\\d|[1-5]\\d):(0?\\d|[1-5]\\d)\\s+(AM|PM))';

                var hours24 = '(((1\\d|2[0-3]|0?\\d):([1-5]\\d|0?\\d)):([1-5]\\d|0?\\d))';

                var time = '\\s+(' + hours12 + '|' + hours24 +')';

                var monthsName = '(jan|feb|mar|apr|may|jun|jul|aug|sep|okt|nov|dec)';

                var niceDate = '(((0?\\d|[12]\\d|3[01])[-]?)?' + monthsName + '[-]?(19[7-9]\\d|2\\d{3}|\\d{1,2})?)';

                var nameDays = '([ty*])|(mon|tue|wed|thu|fri|sat|sun)|(monday|tuesday|wednesday|thursday|friday|saturday|sunday)';

                var commonDate = '([1][0-2]|[0]?[1-9])\\/([3][01]|[1-2]\\d|[0]?[1-9])(\\/((19[7-9]\\d|2\\d{3})|\\d{1,2}))?|([3][01]|[1-2]\\d|[0]?[1-9])[.]([1][0-2]|[0]?[1-9])([.]((19[7-9]\\d|2\\d{3})|\\d{1,2}))?|([3][01]|[1-2]\\d|[0]?[1-9])';

                var dateTime = '((' + niceDate + '|' + commonDate + ')('+time+')?)';

                var commonDateTime = '\\s*([+\\-]|' + nameDays + '|' + dateTime + ')';

                var regexStr = '^' + commonDateTime +flexible + '$';

                var regex = new RegExp(regexStr, 'gmi');

                regex.test(value);

                 

                Hope will be helpful for some others as well.

                • Re: Custom symbol PITime parser
                  MDJ

                  Hi Eugen

                  I have played again with piWebAPI and streams and found, that even calculation is not working well

                  i.e.

                  - request: https://ourServer/piwebapi/calculation/times?expression=ParseTime("01-2h")&time=-1s

                  returns Calculation Failed

                  {

                    "Links": {},

                    "Items": [

                      {

                        "Timestamp": "2018-10-25T13:20:10.6664647Z",

                        "Value": {

                          "Name": "Calc Failed",

                          "Value": 249,

                          "IsSystem": true

                        },

                        "UnitsAbbreviation": "",

                        "Good": false,

                        "Questionable": false,

                        "Substituted": false

                      }

                    ],

                    "UnitsAbbreviation": ""

                  }

                   

                   

                  even clean flexible PiTime does not work here https://ourServer/piwebapi/calculation/times?expression=ParseTime("-2mo")&time=-1s. (expression=ParseTime("-2months")&time=-1s) It thinks it is a minute and not a months. Same problem with w as week The returned time is

                  {

                    "Links": {},

                    "Items": [

                      {

                        "Timestamp": "2018-10-25T13:25:30.6762176Z",

                        "Value": "2018-10-25T13:23:29Z",

                        "UnitsAbbreviation": "",

                        "Good": true,

                        "Questionable": false,

                        "Substituted": false

                      }

                    ],

                    "UnitsAbbreviation": ""

                  }

                  - returns

                  In AF the ParseTime works well.

                   

                  Even the Streams are not working quiet well with piTime: the https://ourServer/piwebapi/streams/webId/value?time=02/01-2h returns correct value in Zulu time (2018-01-31T02:00:00Z), but the https://ourServer/piwebapi/streams/webId/value?time=01/01+21h returns invalid time, which is the same time as previous request.

                  {

                    "Errors": [

                      "The specified timestamp is not valid. If more details are needed, please contact your PI Web API administrator for help in enabling debug mode."

                    ]

                  }

                   

                  and if you want to use it as a validator of time for the future (you want to validate PiTime due to some other calculation in the Performance Equations, and not just for simple Tag) it will also return

                   

                  {

                    "Errors": [

                      "The specified timestamp is not valid. If more details are needed, please contact your PI Web API administrator for help in enabling debug mode."

                    ]

                  }