Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import subprocess
  2. import re
  3. from itertools import izip
  4. import sys
  5. import time
  6. import os
  7.  
  8. # Path file will be saved
  9. file_path = "/root/"
  10. # if you want to start a gammu-smsd daemon after file creation
  11. start_deamon = True
  12. # if you want the file to be created
  13. create_file = True
  14. # prefix name of the configuration file
  15. file_name = ".gammu_nodo"
  16.  
  17. #TODO: create arguments to know the limit of USB modems to delete this ugly if end condition below
  18.  
  19. # Execute gammu detect to know all gammu valid modems
  20. p = subprocess.Popen(['gammu-detect'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  21.  
  22. out, error = p.communicate()
  23. config = out.splitlines()
  24.  
  25. # Lets group all lines in groups of 3 elements
  26. def pairwise(iterable):
  27. a = iter(iterable)
  28. return izip(a, a, a)
  29.  
  30. # valid regex modem list
  31. modems = []
  32. # return all /dev/ttyUSB (?:=|\G) ([^[0-9]+)
  33. # return all /dev/ttyUSB0 (?:=|\G) ([^[0-9]+\d) re.match('(?:=|\G) ([^[0-9]+)', lines, flags=0)
  34.  
  35. for lines in config:
  36. regular_expression = re.match('(?:device|\d) = ([^\n]+)', lines, flags=0)
  37. if regular_expression:
  38. modems.append(regular_expression.group(1))
  39.  
  40. ports = []
  41.  
  42. for port in modems:
  43. regex = re.match('(\/dev\/ttyUSB)', port, flags=0)
  44. if regex:
  45. #print port
  46. ports.append(port)
  47.  
  48. #print ports
  49.  
  50. # Create file with usb Settings from group results
  51. for path in pairwise(ports):
  52.  
  53. full_path = "{0}{1}{2}".format(file_path,file_name,path[1][-1:])
  54. print 'full_file_path: {0}'.format(full_path)
  55. print "port:{0}".format(path[1])
  56.  
  57. string = '''[gammu]
  58. port = {0}
  59. model = at
  60. connection = at
  61. synchronizetime = yes
  62. logfile = /root/.log_nodo{1}
  63. logformat = nothing
  64. use_locking = no
  65. gammuloc =
  66. [smsd]
  67. service = SQL
  68. driver = native_mysql
  69. logfile /root/.log_nodo{2}
  70. debuglevel = 255
  71. phoneid = nodo{3}
  72. maxretries=3
  73. user = root
  74. password = 18252245
  75. pc = localhost
  76. database = sms
  77. '''.format(path[1],path[1][-1:],path[1][-1:],path[1][-1:])
  78.  
  79.  
  80. # Write the file on gammu format
  81. print "file_name: {0}".format(file_name)
  82. print "file_path: {0}".format(file_path)
  83.  
  84. if create_file:
  85.  
  86. file = open(file_path+file_name+path[1][-1:],"w")
  87. file.write(string)
  88. file.close()
  89. # Let's give time to the folder to exists on folder
  90. # Before we go on the next loop modem
  91. time.sleep(1)
  92.  
  93.  
  94.  
  95. # Run Daemon for this Device
  96. command = "gammu-smsd -c {0} -d".format(full_path)
  97. # Debug print
  98. print 'command: {0}'.format(command)
  99. print '++++++++++++++++++++++++++++++++++++++++++'
  100.  
  101. if start_deamon:
  102. # Lets check the file exists before calling it
  103. if os.path.exists(full_path):
  104. # Run Gammu-smsd daemon
  105. c = subprocess.call(['gammu-smsd','-c',full_path,'-d'])
  106. else:
  107. print '{0} does not exist'.format(full_path)
  108.  
  109. # If you have more than 2 modems loop this way
  110. # 0,[1],2,3,[4],5,6,[7],8,9
  111. # if path[1] == "/dev/ttyUSB4":
  112. # sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement