User Tools

Site Tools


This is an old revision of the document!

Writing clients in Python

RTS2 provides three external binding, which can be used to interact from clients with the RTS2 environment:

  1. RTS2 protocol
  2. XML/RPC protocl
  3. JSON

JSON, standing for JavaScript Object Notation, is probably the simplest to use. To demonstrate simplicity of its use:

# import library with RTS2 functions
import login
# for sleep function
import time
# sign on to JSON server
l = login.Login()
# this will pop out dialog box, asking user for credentials
# now set C0 temperature to -20
temp = float(l.jsonProxy().getValue('C0','CCD_TEMP'))
while abs(float(temp + 20) > 0.5:
    print 'temperature',temp,'air temperature',l.jsonProxy().getValue('C0','CCD_AIR')
    # refresh cache..can be avoided by having timer running update, similar to loop which can be found in rts2ui
    temp = float(l.jsonProxy().getValue('C0','CCD_TEMP'))
    # wait for 5 seconds
print 'Desired temperature reached',l.jsonProxy().getValue('C0','CCD_TEMP')

This code demonstrate how you can write a simple client in Python, which does things similar to C++ client provided in INDI tutorial. It illustrates extremely simplicity of outside API in JSON (in this case further masked by Python library to use it). After establishing connection, your client is exchanging messages over opened HTTP connection - so you don't pay extra price for opening and closing communication.

JSON is much more dense then XML, so messages are shorter, and parameters are passed as URL parameters - so you can get response from server using common Internet browser (or wget command), which is handy for debugging.

Errors are propagated as standard Python errors, and as they are not catch, they will terminate client and write its traceback to standard output. If you wish to catch possible errors, all you have to do is to add try-except blocks.

This also provides basic security, as you have to login to server. And if you decide to disallow external users to communicate with RTS2, you just kill rts2-xmlrpcd, which handles the communication.

code/client.1311029100.txt.gz · Last modified: 2011/07/18 00:00 (external edit)