User Tools

Site Tools


code:client

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

code:client [2011/07/19 00:45]
pkubanek tutorial to write Python client
code:client [2012/02/08 00:00]
Line 1: Line 1:
-====== Writing clients in Python ====== 
  
-RTS2 provides three external binding, which can be used to interact from clients with the RTS2 environment:​ 
- 
-  - RTS2 protocol 
-  - XML/RPC protocl 
-  - JSON 
- 
-JSON, standing for [[http://​json.org|JavaScript Object Notation]], is probably the simplest to use. To demonstrate simplicity of its use: 
- 
-<code python> 
-# 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 
-l.signon() 
-# now set C0 temperature to -20 
-l.jsonProxy().setValue('​C0','​CCD_SET',​-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 
-    l.jsonProxy().executeCommand('​C0','​info'​) 
-    l.refresh_cache() 
-    temp = float(l.jsonProxy().getValue('​C0','​CCD_TEMP'​)) 
-    # wait for 5 seconds 
-    time.sleep(5) 
-print '​Desired temperature reached',​l.jsonProxy().getValue('​C0','​CCD_TEMP'​) 
-</​code>​ 
- 
-This code demonstrate how you can write a simple client in Python, which does things similar to C++ client provided in [[http://​indilib.org/​index.php?​title=Developing_INDI_clients|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.txt ยท Last modified: 2012/02/08 00:00 (external edit)