def test_api_get_webapi_with_urls(url): if url == 'BLANK': with pytest.raises(TypeError) as e: osisoftpy.webapi() e.match('argument') else: with pytest.raises(requests.exceptions.MissingSchema) as e: osisoftpy.webapi(url) e.match('Invalid')
def webapi(url, authtype, username, password, verifyssl, hostname_override): if usekerberos: return osisoftpy.webapi(url, authtype=authtype, verifyssl=False, hostname_override=hostname_override) else: return osisoftpy.webapi(url, authtype=authtype, username='******', password='******', verifyssl=False, hostname_override=hostname_override)
def test_api_get_webapi_with_valid_credentials( url, authtype, verifyssl, hostname_override): webapi = osisoftpy.webapi( url, authtype=authtype, verifyssl=verifyssl, hostname_override=hostname_override) assert isinstance(webapi, osisoftpy.WebAPI) assert webapi.links.get('Self').startswith(url)
import logging # To disable logging spam import osisoftpy # main package import numpy # stats import arrow # date formatting from osisoftpy.Points import current # Disable log spam - from DEBUG => INFO\ loglevel = logging.DEBUG log = logging.getLogger(osisoftpy.__name__) log.setLevel(loglevel) for h in log.handlers[:]: h.setLevel(loglevel) # Connect and instantiate the webapi object webapi = osisoftpy.webapi('https://sbb03.eecs.berkeley.edu/piwebapi', authtype='basic', username='******', password='******') print('Connected to {}'.format(webapi.links.get('Self'))) # send the Web API an Indexed Search query for tags named SINU* points = webapi.points(query='name:sinu*', count=1000) current(points, 'values', webapi=webapi) # for each point returned... for point in points: # let's print out it's current value and timestamp... print('Name: {}, current: {}, timestamp: {}'.format( point.name, point.current.value, point.current.timestamp))
# Import library import osisoftpy # Authenticate webapi = osisoftpy.webapi('https://dev.dstcontrols.com/piwebapi/') # Get AF elements afelements = webapi.elements(query='name:Attributes') for element in afelements: print('Element Name: {0}'.format(element.name)) afattributesdict = element.attributes for attribute_key in afattributesdict: valueobj = element[attribute_key].current() print('Attribute Name: {0} | Attribute Value: {1}'.format( attribute_key, valueobj.value)) # Get AF servers servers = webapi.assetservers for server in servers: print('AF Server Name: {0}'.format(server.name)) if (server.name == 'GOLD'): server_to_use = server # Get AF databases databases = server_to_use.get_databases() for database in databases: print('Database Name: {0}'.format(database.name))
print('on_message: {}'.format(message)) def on_error(ws, error): print('on_error: {}'.format(error)) def on_close(ws): print("### closed ###") if __name__ == "__main__": websocket.enableTrace(True) if len(sys.argv) < 2: host = "wss://dev.dstcontrols.com/piwebapi/streams/P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/channel" else: host = sys.argv[1] link = 'https://gold.dstcontrols.local/piwebapi' webapi = osisoftpy.webapi(link) # webapi2 = osisoftpy.webapi('wss://dev.dstcontrols.com/piwebapi/streams/P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/channel') # webapi.session.auth response = requests.Response() response.url = link response.headers = {'www-authenticate': 'negotiate token'} host2 = urlparse(response.url).hostname auth = requests_kerberos.HTTPKerberosAuth() x = auth.generate_request_header(response, host2) ws = websocket.create_connection(host, sslopt={"cert_reqs": ssl.CERT_NONE}, header = {'Authorization': x}) result = ws.recv() print(result) # ws = websocket.WebSocketApp(host, # on_message=on_message, # on_error=on_error,
# Subscribing to the 'current' and 'end' method of SINU* points import osisoftpy import time # for time delay webapi = osisoftpy.webapi('https://localhost/piwebapi', authtype='kerberos') # <OSIsoft PI Web API [https://localhost/piwebapi]> points = webapi.points(query='name:SINU*', count=100) # <osisoftpy.points.Points object at 0x00000000039C1F60> def callback_current(sender): print('CALLBACK: Current Value of {} changed to {} at {}'.format(sender.name, sender.current_value.value, sender.current_value.timestamp)) def callback_end(sender): print('CALLBACK: End Value of {} changed to {} at {}'.format(sender.name, sender.end_value.value, sender.end_value.timestamp)) webapi.subscribe(points, 'current', callback=callback_current) webapi.subscribe(points, 'end', callback=callback_end) # {u'P0xvVoXJ7fokikNJDlYulSjgAgAAAAR09MRFxTSU5VU09JRFU/current/': <blinker.base.NamedSignal object at 0x00000000043D8EB8; u'P0xvVoXJ7fokikNJDlYulSjgAgAAAAR09MRFxTSU5VU09JRFU/current/'> # , u'P0xvVoXJ7fokikNJDlYulSjgL4gAAAR09MRFxTSU5VU09JRC5GT1JFQ0FTVA/current/': <blinker.base.NamedSignal object at 0x00000000043D8E48; u'P0xvVoXJ7fokikNJDlYulSjgL4gAAAR09MRFxTSU5VU09JRC5GT1JFQ0FTVA/current/'> # , u'P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/current/': <blinker.base.NamedSignal object at 0x0000000002D650B8; u'P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/current/'> # , u'P0xvVoXJ7fokikNJDlYulSjgAgAAAAR09MRFxTSU5VU09JRFU/end/': <blinker.base.NamedSignal object at 0x00000000043D8F98; u'P0xvVoXJ7fokikNJDlYulSjgAgAAAAR09MRFxTSU5VU09JRFU/end/'> # , u'P0xvVoXJ7fokikNJDlYulSjgL4gAAAR09MRFxTSU5VU09JRC5GT1JFQ0FTVA/end/': <blinker.base.NamedSignal object at 0x00000000043D8EF0; u'P0xvVoXJ7fokikNJDlYulSjgL4gAAAR09MRFxTSU5VU09JRC5GT1JFQ0FTVA/end/'> # , u'P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/end/': <blinker.base.NamedSignal object at 0x00000000043D8E10; u'P0xvVoXJ7fokikNJDlYulSjgAQAAAAR09MRFxTSU5VU09JRA/end/'>} for point in points: value = point.current()
# Import library import osisoftpy # Authenticate webapi = osisoftpy.webapi( 'https://dev.dstcontrols.com/piwebapi/' ) #, authtype='basic', username='******', password='******') # Get points point_list = webapi.points(query='name:SINU*') point_list2 = webapi.points(query='name:CDT*') for individual_point in point_list2: point_list.append(individual_point) # Read latest value for i in range(1000): for individual_point in point_list: if individual_point.name.lower() == 'sinusoid': individual_point.webid = individual_point.webid[:-1] valueobj = individual_point.current(error_action='Continue') if hasattr(valueobj, 'value'): print('Latest value of {} is {} at time {}'.format( individual_point.name, valueobj.value, valueobj.timestamp))
def cnnt_message(): try: #osisoftpy.webapi(qa,authtype='basic', username=lanid,password=pwd) osisoftpy.webapi(dev, authtype='kerberos') except Exception: #catch any general exception# return t
# #''' delay = 60 #raw_input('Preferred deplay time: ') recnnt_max = 3 #raw_input('Max Reconnection: ') i = cnnt_message() k = 1 while i == 'error': #try 'recnnt_max' times only# #print ('Try Connection:',dt.now().strftime("%Y-%m-%d %H:%M:%S"),k) k += 1 i = cnnt_message() time.sleep(float(delay)) if k == float(recnnt_max): #print ("Connection Failed",k) break else: webapi = osisoftpy.webapi(dev, authtype='kerberos') ##################################################################################reenable these 1 lines for logfile #logfile.write('Connection Successful'+ str(dt.now().strftime("%Y-%m-%d %H:%M:%S"))+str(k)) print('Connection Successful' + str(dt.now().strftime("%Y-%m-%d %H:%M:%S")) + str(k)) #additional analysis code here# # create function for getting value updates ############################################################################################################################ df = pd.DataFrame(columns=['tag name', 'DateTime', 'Pressure']) #dfcopy = pd.DataFrame(columns=['tag name','DateTime', 'Pressure']) #df = df.append({'tag':1, 'time':2, 'value':3}, ignore_index=True) def callback_current(sender): global df