Changes between Version 1 and Version 2 of UsingSensors

Changes between Version 1 and Version 2 of UsingSensors

Please note that these Trac pages are no longer being updated. Wiki contents/documentation have moved to GitHub.

Changes between Version 1 and Version 2 of UsingSensors

Please note that these Trac pages are no longer being updated. Wiki contents/documentation have moved to GitHub.

Changes between Version 1 and Version 2 of UsingSensors

Show
Ignore:
Timestamp:
07/24/12 07:17:21 (7 years ago)
Author:
justinc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • UsingSensors

    v1 v2  
    1111== Required API == 
    1212 
    13 All sensors must provide an XMLRPC interface bound to 127.0.0.1 on any port from 63090-63099.   This XMLRPC interface must support at least the following two calls:  {{{isSeattleSensor()}}} and {{{system.listMethods()}}}.   (See [http://docs.python.org/library/simplexmlrpcserver.html the SimpleXMLRPCAPI] for general details on Python, but similar libraries exist on other platforms.)   The {{{isSeattleSensor()}}} call must return True.   The {{{system.listMethods()}}} should list the available calls.    
     13All sensors must provide an XMLRPC interface bound to 127.0.0.1 on any port from 63090-63099.   This XMLRPC interface must support at least the following two calls:  {{{isSeattleSensor()}}}, {{{system.listMethods()}}}, and {{{system.methodSignature}}}.   (See [http://docs.python.org/library/simplexmlrpcserver.html the SimpleXMLRPCAPI] for general details on Python, but similar libraries exist on other platforms.)   The {{{isSeattleSensor()}}} call must return True.   The {{{system.listMethods()}}} should list the available calls.   The {{{system.methodSignature(callname)}}} must return the calling arguments for a function. 
    1414 
    15 It is also *recommended* that you provide calls for {{{system.methodHelp}}} and {{{system.methodSignature}}}. 
     15'''Note: it is completely fine for a sensor to hide calls it does not want to be called by general clients.'''   In other words, if you have a call that performs a potentially disruptive act, you need not list it.   Keep in mind that this is not a security mechanism.   It is just to prevent a simple and general client from calling it without understanding what it does. 
     16 
     17It is also *recommended* that you provide the call {{{system.methodHelp}}}.   This is not required and clients *must not* rely on their existence. 
    1618 
    1719== Example Use == 
     20 
     21Here is some example pseudo-code for building a sensor and client.    
     22 
     23Sensor code: 
     24 
     25{{{ 
     26import SimpleXMLRPCServer 
     27import socket 
     28import sys 
     29 
     30for sensorport in [63095, 63096, 63097, 63098, 63099, 63090, 63091, 63092, 63093, 63094]: 
     31  try: 
     32    server = SimpleXMLRPCServer(("localhost", sensorport), 
     33                            requestHandler=RequestHandler) 
     34    server.register_introspection_functions() 
     35  except socket.error: 
     36    print 'port:',sensorport,'failed.   Trying backup port...'  
     37    continue 
     38  break 
     39else: 
     40  print 'Could not locate a port!' 
     41  sys.exit(1) 
     42     
     43import iwlistlib 
     44 
     45 
     46def isSeattleSensor(): 
     47  return True 
     48 
     49# NOTE: the system.* calls are defined by the !SimpleXMLRPCServer in Python.   Your library's use may vary. 
     50 
     51def mySensorName(): 
     52  return "Hello World Sensor" 
     53 
     54def readSensor(): 
     55  return "Hello World!" 
     56 
     57def readSensorForLanguage(language): 
     58  if language == 'English': 
     59    return "Hello World!" 
     60  elif language == 'German': 
     61    return "Hallo Weld!" 
     62  elif language == 'Mandarin': 
     63    return "Ni Hao!" 
     64  else: 
     65    raise Exception("Unsupported language") 
     66 
     67 
     68server.register_introspection_functions() 
     69server.register_function(mySensorName) 
     70server.register_function(readSensor) 
     71server.register_function(readSensorForLanguage) 
     72server.register_function(isSeattleSensor) 
     73 
     74server.serve_forever() 
     75 
     76}}} 
     77 
     78 
     79Repy sensor reading code: 
     80{{{ 
     81import xmlrpclib 
     82import sys 
     83 
     84# I really should check multiple ports... 
     85s = xmlrpclib.ServerProxy('http://localhost:63095') 
     86 
     87# should check the error status... 
     88try:  
     89  if not s.isSeattleSensor(): 
     90    print 'something other than a Seattle sensor was bound to the port...' 
     91    sys.exit(1) 
     92except: 
     93    print 'Got an error when checking if this is a Seattle sensor...' 
     94    sys.exit(1) 
     95   
     96# Get the list of available methods 
     97methodlist = s.system.listMethods() 
     98 
     99for methodname in methodlist: 
     100  thissignature = s.system.methodSignature(methodname) 
     101  print 'Method:',methodname,'has the signature', thissignature 
     102 
     103  if thissignature == (): 
     104    print 'Empty call returns:',safe_eval("s."+methodname+"()") 
     105 
     106}}} 
     107 
     108 
     109Note: I didn't test the above code!   Beware of bugs!!!