4 Replies Latest reply on Sep 16, 2015 8:45 AM by pthivierge

    AF SDK and Python (not IronPython)

    wegnear

      A decent portion of our development is done via Python. We're looking to use the AF SDK with Python, but finding the documentation (or others doing something similar) to be non-existent. Has anyone used Python with the AFSDK?

       

      If so, do you have documentation you'd be willing to share? I am really looking for a very basic example of:

      • Connecting to an AF server
      • Very simple query

      Thanks

        • Re: AF SDK and Python (not IronPython)
          Marcos Vainer Loeff

          Hello,

           

          From my understanding, PI AF SDK is not compatible with Python since it is a pure .NET library. Your best option would be to use PI Web API which is a RESTful web service on top of the PI System that wraps PI AF SDK. Python should be able to make REST calls to PI Web API. You can download the beta version of this product in the vCampus Download Center.

            • Re: AF SDK and Python (not IronPython)
              Mahyar

              Hi,

              Marcos is probably right.

              Here is a sample I've made to test WEB-API with Python.

              It simply writes events into several PIPoints.

              Hope it can help.

               

              #!/usr/bin/python
              # -*- coding: iso-8859-15 -*-
              import os, sys
              import requests
              import logging
              import datetime
              import random
              import json
              import time
              
              
              logging.basicConfig(filename='piwebapi.log',level=logging.INFO)
              
              
              dns = "192.0.0.1"
              pathprefix = "\\\\WIN-X\Database1\WIN-X ModuleDB\Sample1"
              
              
              try:
                L = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
                starttime = datetime.datetime(2013, 11, 30, 0, 0, 0)
                endtime = datetime.datetime(2014, 11, 25, 0, 0, 0)
                delta = datetime.timedelta(days=1)
                newtime = starttime
                while newtime <= endtime:
                newtime = newtime + delta
                #logging.info("******************* Processing for date %s" % (newtime))
                for i in L:
                # find the related attribute from result
                geturl = "https://%s/piwebapi/attributes?path=%s%s|Sample" % (dns, pathprefix, i)
                response = requests.get(geturl, verify=False)
                data = response.json()
                webIdKey = u"WebId"
                linksKey = u"Links"
                if response.status_code == 200 and data.has_key(webIdKey):
                webIdAttribute = data[webIdKey]
                # set a value
                posturl = ("https://%s/piwebapi/streams/%s/value" % (dns, webIdAttribute))
                postheaders = {'content-type': 'application/json'}
                postbody = json.dumps({
                u"Timestamp": (u"%s" % newtime.isoformat()),
                u"Value": ("%s" % random.randint(10, 45)),
                #u"UnitsAbbreviation": "°C",
                u"Good": u"true",
                u"Questionable": u"false",
                })
                postparam = {'updateOption':'Replace', 'bufferOption':'DoNotBuffer'}
                responsepost = requests.post(url=posturl, verify=False, data=postbody, headers=postheaders, params=postparam)
                logstr = "posturl:%s statusget: %s, body: %s, statuspost: %s, response.json()" % (responsepost.url, response.status_code, postbody, responsepost.status_code)
                if(200<=responsepost.status_code<300):
                logging.debug(logstr)
                else:
                logging.error(logstr)
                else: 
                logging.error("status: %s, WebId NOT FOUND, json:%s" % (response.status_code, data))
                pass
                #time.sleep(2)
              except Exception, e:
                logging.error("error during procssing %s" %(e))
                raise
              finally:
                pass
              
              1 of 1 people found this helpful
            • Re: AF SDK and Python (not IronPython)
              pthivierge

              Edit 2015-09-16: Clarification for PI OLEDB Enterprise

               

              Hello Andy,

               

              You may also have a look here: Working with Pi Data from Python - Python version of Pi SDK?

               

              There is an example from Barry Shang that shows how use PI OLEDB with Python.  You could use PI OLEDB Enterprise instead, and run your simple queries from it.

              However, with PI OLEDB Enterprise it is not possible to write data.

               

              Hope this helps.

              2 of 2 people found this helpful