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