1 | import sys, os
|
---|
2 | import serial
|
---|
3 | import array
|
---|
4 | import time
|
---|
5 | import optparse
|
---|
6 | import ConfigParser, logging
|
---|
7 | from scanner_interface import ScannerInterface
|
---|
8 |
|
---|
9 | logging.basicConfig(level=logging.DEBUG)
|
---|
10 | log = logging.getLogger( 'Scanner' )
|
---|
11 |
|
---|
12 | # command prefix: SYN M CR
|
---|
13 | cmd = [22, 77, 13]
|
---|
14 | response = { 6: 'ACK', 5: 'ENQ', 21: 'NAK' }
|
---|
15 | image_scanner_report = 'RPTSCN.'
|
---|
16 | get_prefix1 = 'PREBK2?.'
|
---|
17 | get_prefix2 = ':4820:PREBK2?.'
|
---|
18 | set_prefix = 'PREBK2995859.'
|
---|
19 | clear_prefix = 'PRECA2.'
|
---|
20 |
|
---|
21 | def get_prefix_cmd(name):
|
---|
22 | return ':' + name + ':' + 'PREBK2?.'
|
---|
23 |
|
---|
24 | def 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 |
|
---|
30 | def 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 |
|
---|
46 | def 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 |
|
---|
91 | if __name__ == "__main__":
|
---|
92 | main()
|
---|