Jump to content

Python Subscription example?


gadgetguru69

Recommended Posts

Hey guys I have been searching the forum trying to find some example python code for subscribing to the ISY for status updates.

I am currently using the rest functions to do this but its response is quite slow. The rest function will work fine for the data logging I want to do but for real time status when interacting with device via screen I would like better response times. From what I have been reading subscribing is the way to go.

Link to comment

I had not seen a python example on the wiki, but working my way through understanding Jeffrey's very fully done perl module now. Just starting to cobble together the code to do the subscription through his module and parse the results back for what I am after.

 

Be happy to share when I get it figured out, but my perl is a little rusty and taking some baby steps now.

Link to comment

Michael,

I'm not sure if you are familiar with python or not I figure you are though. So do you see anything that just stands out in this code that would cause it to respond slow? I am seeing it take around 10 seconds once I trigger the control section of code to give me back a status update of the device that was triggered. Seams like an awful long time for update back.

 

Thanks for your time guys

global app
import app
def connect():
#procedure is used to make connection to the ISY device
import urllib2, urllib, sys
username = 'admin'
password = 'admin'
topurl = 'http://192.168.2.13:81'
auth_handler = urllib2.HTTPPasswordMgrWithDefaultRealm()
auth_handler.add_password(None, uri=topurl,user=username,passwd=password)
handler = urllib2.HTTPBasicAuthHandler(auth_handler)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
return topurl

def control(deviceID,function,level):
#procedure used to control devices ie switch them on and off.
import urllib2, urllib, sys
topurl = app.isy.connect()
print "topurl is " + (topurl)
print "device id is" + (deviceID)
if function == 'DON' and level != "":
	theurl = topurl+'/rest/nodes/' + deviceID + '/cmd/'+function
else:
	theurl = topurl+'/rest/nodes/' + deviceID + '/cmd/'+function+'/'+level
#normalize the URL
theurl = urllib.quote(theurl, safe="%/:=&?~#+!$,;'@()*[]")
print "the url is " +theurl
pagehandle = urllib2.urlopen(theurl)
app.isy.status(deviceID,topurl)

def status(deviceID,topurl):
#procedure will get the status of the device that was just triggered and set the value in the ignition tag.
import urllib2,urllib
import sys, system
import xml.etree.ElementTree as ET

theurl = topurl+'/rest/status/' + deviceID
theurl = urllib.quote(theurl, safe="%/:=&?~#+!$,;'@()*[]")
response=urllib2.urlopen(theurl)
mytree = ET.parse(response)
value = mytree.find('property').get('value')
system.tag.write("ISY/"+deviceID,value)

Link to comment

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...