PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/galaxy_messaging/client/scanner.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 92 lines | 87 code | 4 blank | 1 comment | 1 complexity | d67f2201e7cc3b043ecf8ffba63c1cef MD5 | raw file
 1import sys, os
 2import serial
 3import array
 4import time
 5import optparse
 6import ConfigParser, logging
 7from scanner_interface import ScannerInterface
 8
 9logging.basicConfig(level=logging.DEBUG)
10log = logging.getLogger( 'Scanner' )
11
12# command prefix: SYN M CR
13cmd = [22, 77, 13]
14response = { 6: 'ACK', 5: 'ENQ', 21: 'NAK' }
15image_scanner_report = 'RPTSCN.'
16get_prefix1 = 'PREBK2?.'
17get_prefix2 = ':4820:PREBK2?.'
18set_prefix = 'PREBK2995859.'
19clear_prefix = 'PRECA2.'
20
21def get_prefix_cmd(name):
22    return ':' + name + ':' + 'PREBK2?.'
23
24def set_prefix_cmd(name, prefix):
25    prefix_str = ''
26    for c in prefix:
27        prefix_str = prefix_str + hex(ord(c))[2:]
28    return ':' + name + ':' + 'PREBK299' + prefix_str + '!'
29
30def read_config_file(config_file):
31    config = ConfigParser.ConfigParser()
32    config.read(config_file)
33    count = 1
34    scanners_list = []
35    while True:
36        section = 'scanner%i' % count
37        if config.has_section(section):
38            scanner = dict(name=config.get(section, 'name'),
39                           prefix=config.get(section, 'prefix'),
40                           state=config.get(section, 'state'))
41            scanners_list.append(scanner)
42            count = count + 1
43        else:
44            return scanners_list
45
46def main():
47    usage = "python %s -p PORT -c CONFIG_FILE [ OPTION ]" % sys.argv[0]
48    parser = optparse.OptionParser(usage=usage)
49    parser.add_option('-p', '--port', help='Name of the port where the scanner is connected', 
50                      dest='port', action='store')
51    parser.add_option('-c', '--config-file', help='config file with all the AMQP config parameters', 
52                      dest='config_file', action='store')
53    parser.add_option('-r', '--report', help='scanner report', 
54                      dest='report', action='store_true', default=False)
55    parser.add_option('-i', '--install', help='install the scanners', 
56                      dest='install', action='store_true', default=False)
57    (opts, args) = parser.parse_args()
58    # validate 
59    if not opts.port:
60        parser.print_help()
61        sys.exit(0)
62    if ( opts.report or opts.install ) and not opts.config_file:
63        parser.print_help()
64        sys.exit(0)
65
66    # create the scanner interface
67    si = ScannerInterface(opts.port)
68    if opts.install:
69        scanners_list = read_config_file(opts.config_file)
70        for scanner in scanners_list:
71            msg = set_prefix_cmd(scanner['name'], scanner['prefix'])
72            si.send(msg)
73            response = si.recv()
74            if not response:
75                log.error("Scanner %s could not be installed." % scanner['name'])
76    elif opts.report:
77        si.send(image_scanner_report)
78        rep = si.recv()
79        log.info(rep)
80        scanners_list = read_config_file(opts.config_file)
81        for scanner in scanners_list:
82            msg = get_prefix_cmd(scanner['name'])
83            si.send(msg)
84            response = si.recv()
85            if response:
86                log.info('PREFIX for scanner %s: %s' % (scanner['name'],  chr(int(response[8:12][:2], 16))+chr(int(response[8:12][2:], 16))   ))
87    si.close()
88    
89    
90
91if __name__ == "__main__":
92    main()