[2] | 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 | |
---|
| 9 | from amqplib import client_0_8 as amqp |
---|
| 10 | import ConfigParser |
---|
| 11 | import sys, os |
---|
| 12 | import serial |
---|
| 13 | import array |
---|
| 14 | import time |
---|
| 15 | import optparse |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | xml = \ |
---|
| 19 | ''' <sample> |
---|
| 20 | <barcode>%(BARCODE)s</barcode> |
---|
| 21 | <state>%(STATE)s</state> |
---|
| 22 | </sample>''' |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | def handle_scan(states, amqp_config, barcode): |
---|
| 26 | if states.get(barcode[:2], None): |
---|
| 27 | values = dict( BARCODE=barcode[2:], |
---|
| 28 | STATE=states.get(barcode[:2]) ) |
---|
| 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 | insist=False) |
---|
| 37 | chan = conn.channel() |
---|
| 38 | msg = amqp.Message(data, |
---|
| 39 | content_type='text/plain', |
---|
| 40 | application_headers={'msg_type': 'sample_state_update'}) |
---|
| 41 | msg.properties["delivery_mode"] = 2 |
---|
| 42 | chan.basic_publish(msg, |
---|
| 43 | exchange=amqp_config['exchange'], |
---|
| 44 | routing_key=amqp_config['routing_key']) |
---|
| 45 | chan.close() |
---|
| 46 | conn.close() |
---|
| 47 | |
---|
| 48 | def recv_data(states, amqp_config, s): |
---|
| 49 | while True: |
---|
| 50 | bytes = s.inWaiting() |
---|
| 51 | if bytes: |
---|
| 52 | print '%i bytes recvd' % bytes |
---|
| 53 | msg = s.read(bytes) |
---|
| 54 | print msg |
---|
| 55 | handle_scan(states, amqp_config, msg.strip()) |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | def main(): |
---|
| 59 | parser = optparse.OptionParser() |
---|
| 60 | parser.add_option('-c', '--config-file', help='config file with all the AMQP config parameters', |
---|
| 61 | dest='config_file', action='store') |
---|
| 62 | parser.add_option('-p', '--port', help='Name of the port where the scanner is connected', |
---|
| 63 | dest='port', action='store') |
---|
| 64 | (opts, args) = parser.parse_args() |
---|
| 65 | config = ConfigParser.ConfigParser() |
---|
| 66 | config.read(opts.config_file) |
---|
| 67 | amqp_config = {} |
---|
| 68 | states = {} |
---|
| 69 | for option in config.options("galaxy:amqp"): |
---|
| 70 | amqp_config[option] = config.get("galaxy:amqp", option) |
---|
| 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 | |
---|
| 87 | |
---|
| 88 | if __name__ == '__main__': |
---|
| 89 | main() |
---|