root/galaxy-central/scripts/galaxy_messaging/client/amqp_publisher.py

リビジョン 2, 3.0 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1'''
2This script gets barcode data from a barcode scanner using serial communication
3and sends the state representated by the barcode scanner & the barcode string
4to the Galaxy LIMS RabbitMQ server. The message is sent in XML which has 2 tags,
5barcode & state. The state of the scanner should be set in the galaxy_amq.ini
6file as a configuration variable.
7'''
8
9from amqplib import client_0_8 as amqp
10import ConfigParser
11import sys, os
12import serial
13import array
14import time
15import optparse
16
17
18xml = \
19''' <sample>
20        <barcode>%(BARCODE)s</barcode>
21        <state>%(STATE)s</state>
22    </sample>'''
23
24
25def 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
48def 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       
58def 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   
88if __name__ == '__main__':
89    main()
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。