/scripts/galaxy_messaging/client/amqp_publisher.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 100 lines · 74 code · 13 blank · 13 comment · 9 complexity · 1ccc09850308db26b8220c14019a39fd MD5 · raw file

  1. '''
  2. This script gets barcode data from a barcode scanner using serial communication
  3. and sends the state representated by the barcode scanner & the barcode string
  4. to the Galaxy LIMS RabbitMQ server. The message is sent in XML which has 2 tags,
  5. barcode & state. The state of the scanner should be set in the galaxy_amq.ini
  6. file as a configuration variable.
  7. '''
  8. import array
  9. import ConfigParser
  10. import optparse
  11. import os
  12. import serial
  13. import sys
  14. import time
  15. from galaxy import eggs
  16. eggs.require("amqp")
  17. import amqp
  18. xml = \
  19. ''' <sample>
  20. <barcode>%(BARCODE)s</barcode>
  21. <state>%(STATE)s</state>
  22. <api_key>%(API_KEY)s</api_key>
  23. </sample>'''
  24. def handle_scan(states, amqp_config, barcode):
  25. if states.get(barcode[:2], None):
  26. values = dict( BARCODE=barcode[2:],
  27. STATE=states.get(barcode[:2]),
  28. API_KEY=amqp_config['api_key'] )
  29. print values
  30. data = xml % values
  31. print data
  32. conn = amqp.Connection(host=amqp_config['host']+":"+amqp_config['port'],
  33. userid=amqp_config['userid'],
  34. password=amqp_config['password'],
  35. virtual_host=amqp_config['virtual_host'],)
  36. chan = conn.channel()
  37. msg = amqp.Message(data,
  38. content_type='text/plain',
  39. application_headers={'msg_type': 'sample_state_update'})
  40. msg.properties["delivery_mode"] = 2
  41. chan.basic_publish(msg,
  42. exchange=amqp_config['exchange'],
  43. routing_key=amqp_config['routing_key'])
  44. chan.close()
  45. conn.close()
  46. def recv_data(states, amqp_config, s):
  47. while True:
  48. bytes = s.inWaiting()
  49. if bytes:
  50. print '%i bytes recvd' % bytes
  51. msg = s.read(bytes)
  52. print msg
  53. handle_scan(states, amqp_config, msg.strip())
  54. def main():
  55. parser = optparse.OptionParser()
  56. parser.add_option('-c', '--config-file', help='config file with all the AMQP config parameters',
  57. dest='config_file', action='store')
  58. parser.add_option('-p', '--port', help='Name of the port where the scanner is connected',
  59. dest='port', action='store')
  60. (opts, args) = parser.parse_args()
  61. config = ConfigParser.ConfigParser()
  62. config.read(opts.config_file)
  63. amqp_config = {}
  64. states = {}
  65. for option in config.options("galaxy:amqp"):
  66. amqp_config[option] = config.get("galaxy:amqp", option)
  67. # abort if api_key is not set in the config file
  68. if not amqp_config['api_key']:
  69. print 'Error: Set the api_key config variable in the config file before starting the amqp_publisher script.'
  70. sys.exit( 1 )
  71. count = 1
  72. while True:
  73. section = 'scanner%i' % count
  74. if config.has_section(section):
  75. states[config.get(section, 'prefix')] = config.get(section, 'state')
  76. count = count + 1
  77. else:
  78. break
  79. print amqp_config
  80. print states
  81. s = serial.Serial(int(opts.port))
  82. print 'Port %s is open: %s' %( opts.port, s.isOpen())
  83. recv_data(states, amqp_config, s)
  84. s.close()
  85. print 'Port %s is open: %s' %( opts.port, s.isOpen())
  86. if __name__ == '__main__':
  87. main()